Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #473
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Sep 15, 2023
1 parent 121326f commit be449ed
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions content/leetcode/2023/9.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,50 @@ title: 2023.9
draft: false
---

# 2023.9.15

```python
#
# @lc app=leetcode.cn id=473 lang=python3
#
# [473] 火柴拼正方形
#

# @lc code=start
from typing import List


class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
if len(matchsticks) < 4:
return False

perimeter = sum(matchsticks)
side = perimeter // 4
if side * 4 != perimeter:
return False

matchsticks.sort(reverse=True)
sums = [0 for _ in range(4)]

def dfs(index):
if index == len(matchsticks):
return sums[0] == sums[1] == sums[2] == side

for i in range(4):
if sums[i] + matchsticks[index] <= side:
sums[i] += matchsticks[index]
if dfs(index + 1):
return True
sums[i] -= matchsticks[index]
return False

return dfs(0)


# @lc code=end
```

# 2023.9.14

```python
Expand Down

0 comments on commit be449ed

Please sign in to comment.