Skip to content

Commit aeab1d7

Browse files
committed
feat: add solutions to lc problem: No.0473
No.0473.Matchsticks to Square
1 parent f435d7f commit aeab1d7

File tree

6 files changed

+316
-19
lines changed

6 files changed

+316
-19
lines changed

solution/0400-0499/0473.Matchsticks to Square/README.md

+118-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,139 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:排序 + 回溯**
47+
48+
用 $edges[i]$ 记录正方形每条边当前的长度,对于第 $u$ 根火柴,尝试把它加到 $edges[i]$ 每条边,若加入后 $edges[i]$ 不超过正方形期望长度 $x$,则继续往下递归 $u+1$ 根火柴。若所有火柴都能被加入,说明满足拼成正方形的要求。
49+
50+
这里对 $matchsticks$ 从大到小排序,可以减少搜索次数。
51+
52+
时间复杂度 $O(4^n)$,其中 $n$ 表示 $matchsticks$ 的长度。每根火柴可以被放入正方形的 $4$ 条边,共有 $n$ 根火柴。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
4957

5058
<!-- 这里可写当前语言的特殊实现逻辑 -->
5159

5260
```python
53-
61+
class Solution:
62+
def makesquare(self, matchsticks: List[int]) -> bool:
63+
def dfs(u):
64+
if u == len(matchsticks):
65+
return True
66+
for i in range(4):
67+
edges[i] += matchsticks[u]
68+
if edges[i] <= x and dfs(u + 1):
69+
return True
70+
edges[i] -= matchsticks[u]
71+
return False
72+
73+
x, mod = divmod(sum(matchsticks), 4)
74+
if mod or x < max(matchsticks):
75+
return False
76+
edges = [0] * 4
77+
matchsticks.sort(reverse=True)
78+
return dfs(0)
5479
```
5580

5681
### **Java**
5782

5883
<!-- 这里可写当前语言的特殊实现逻辑 -->
5984

6085
```java
86+
class Solution {
87+
public boolean makesquare(int[] matchsticks) {
88+
int s = 0, mx = 0;
89+
for (int v : matchsticks) {
90+
s += v;
91+
mx = Math.max(mx, v);
92+
}
93+
int x = s / 4, mod = s % 4;
94+
if (mod != 0 || x < mx) {
95+
return false;
96+
}
97+
Arrays.sort(matchsticks);
98+
int[] edges = new int[4];
99+
return dfs(matchsticks.length - 1, x, matchsticks, edges);
100+
}
101+
102+
private boolean dfs(int u, int x, int[] matchsticks, int[] edges) {
103+
if (u < 0) {
104+
return true;
105+
}
106+
for (int i = 0; i < 4; ++i) {
107+
edges[i] += matchsticks[u];
108+
if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) {
109+
return true;
110+
}
111+
edges[i] -= matchsticks[u];
112+
}
113+
return false;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
bool makesquare(vector<int>& matchsticks) {
124+
int s = 0, mx = 0;
125+
for (int& v : matchsticks)
126+
{
127+
s += v;
128+
mx = max(mx, v);
129+
}
130+
int x = s / 4, mod = s % 4;
131+
if (mod != 0 || x < mx) return false;
132+
sort(matchsticks.begin(), matchsticks.end(), greater<int>());
133+
vector<int> edges(4);
134+
return dfs(0, x, matchsticks, edges);
135+
}
136+
137+
bool dfs(int u, int x, vector<int>& matchsticks, vector<int>& edges) {
138+
if (u == matchsticks.size()) return true;
139+
for (int i = 0; i < 4; ++i)
140+
{
141+
edges[i] += matchsticks[u];
142+
if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true;
143+
edges[i] -= matchsticks[u];
144+
}
145+
return false;
146+
}
147+
};
148+
```
61149

150+
### **Go**
151+
152+
```go
153+
func makesquare(matchsticks []int) bool {
154+
s := 0
155+
for _, v := range matchsticks {
156+
s += v
157+
}
158+
if s%4 != 0 {
159+
return false
160+
}
161+
sort.Ints(matchsticks)
162+
edges := make([]int, 4)
163+
var dfs func(u, x int) bool
164+
dfs = func(u, x int) bool {
165+
if u < 0 {
166+
return true
167+
}
168+
for i := 0; i < 4; i++ {
169+
edges[i] += matchsticks[u]
170+
if edges[i] <= x && dfs(u-1, x) {
171+
return true
172+
}
173+
edges[i] -= matchsticks[u]
174+
}
175+
return false
176+
}
177+
return dfs(len(matchsticks)-1, s/4)
178+
}
62179
```
63180

64181
### **...**

solution/0400-0499/0473.Matchsticks to Square/README_EN.md

+110-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,122 @@
4040
### **Python3**
4141

4242
```python
43-
43+
class Solution:
44+
def makesquare(self, matchsticks: List[int]) -> bool:
45+
def dfs(u):
46+
if u == len(matchsticks):
47+
return True
48+
for i in range(4):
49+
edges[i] += matchsticks[u]
50+
if edges[i] <= x and dfs(u + 1):
51+
return True
52+
edges[i] -= matchsticks[u]
53+
return False
54+
55+
x, mod = divmod(sum(matchsticks), 4)
56+
if mod or x < max(matchsticks):
57+
return False
58+
edges = [0] * 4
59+
matchsticks.sort(reverse=True)
60+
return dfs(0)
4461
```
4562

4663
### **Java**
4764

4865
```java
66+
class Solution {
67+
public boolean makesquare(int[] matchsticks) {
68+
int s = 0, mx = 0;
69+
for (int v : matchsticks) {
70+
s += v;
71+
mx = Math.max(mx, v);
72+
}
73+
int x = s / 4, mod = s % 4;
74+
if (mod != 0 || x < mx) {
75+
return false;
76+
}
77+
Arrays.sort(matchsticks);
78+
int[] edges = new int[4];
79+
return dfs(matchsticks.length - 1, x, matchsticks, edges);
80+
}
81+
82+
private boolean dfs(int u, int x, int[] matchsticks, int[] edges) {
83+
if (u < 0) {
84+
return true;
85+
}
86+
for (int i = 0; i < 4; ++i) {
87+
edges[i] += matchsticks[u];
88+
if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) {
89+
return true;
90+
}
91+
edges[i] -= matchsticks[u];
92+
}
93+
return false;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
bool makesquare(vector<int>& matchsticks) {
104+
int s = 0, mx = 0;
105+
for (int& v : matchsticks)
106+
{
107+
s += v;
108+
mx = max(mx, v);
109+
}
110+
int x = s / 4, mod = s % 4;
111+
if (mod != 0 || x < mx) return false;
112+
sort(matchsticks.begin(), matchsticks.end(), greater<int>());
113+
vector<int> edges(4);
114+
return dfs(0, x, matchsticks, edges);
115+
}
116+
117+
bool dfs(int u, int x, vector<int>& matchsticks, vector<int>& edges) {
118+
if (u == matchsticks.size()) return true;
119+
for (int i = 0; i < 4; ++i)
120+
{
121+
edges[i] += matchsticks[u];
122+
if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true;
123+
edges[i] -= matchsticks[u];
124+
}
125+
return false;
126+
}
127+
};
128+
```
49129

130+
### **Go**
131+
132+
```go
133+
func makesquare(matchsticks []int) bool {
134+
s := 0
135+
for _, v := range matchsticks {
136+
s += v
137+
}
138+
if s%4 != 0 {
139+
return false
140+
}
141+
sort.Ints(matchsticks)
142+
edges := make([]int, 4)
143+
var dfs func(u, x int) bool
144+
dfs = func(u, x int) bool {
145+
if u < 0 {
146+
return true
147+
}
148+
for i := 0; i < 4; i++ {
149+
edges[i] += matchsticks[u]
150+
if edges[i] <= x && dfs(u-1, x) {
151+
return true
152+
}
153+
edges[i] -= matchsticks[u]
154+
}
155+
return false
156+
}
157+
return dfs(len(matchsticks)-1, s/4)
158+
}
50159
```
51160

52161
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
bool makesquare(vector<int>& matchsticks) {
4+
int s = 0, mx = 0;
5+
for (int& v : matchsticks)
6+
{
7+
s += v;
8+
mx = max(mx, v);
9+
}
10+
int x = s / 4, mod = s % 4;
11+
if (mod != 0 || x < mx) return false;
12+
sort(matchsticks.begin(), matchsticks.end(), greater<int>());
13+
vector<int> edges(4);
14+
return dfs(0, x, matchsticks, edges);
15+
}
16+
17+
bool dfs(int u, int x, vector<int>& matchsticks, vector<int>& edges) {
18+
if (u == matchsticks.size()) return true;
19+
for (int i = 0; i < 4; ++i)
20+
{
21+
edges[i] += matchsticks[u];
22+
if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true;
23+
edges[i] -= matchsticks[u];
24+
}
25+
return false;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func makesquare(matchsticks []int) bool {
2+
s := 0
3+
for _, v := range matchsticks {
4+
s += v
5+
}
6+
if s%4 != 0 {
7+
return false
8+
}
9+
sort.Ints(matchsticks)
10+
edges := make([]int, 4)
11+
var dfs func(u, x int) bool
12+
dfs = func(u, x int) bool {
13+
if u < 0 {
14+
return true
15+
}
16+
for i := 0; i < 4; i++ {
17+
edges[i] += matchsticks[u]
18+
if edges[i] <= x && dfs(u-1, x) {
19+
return true
20+
}
21+
edges[i] -= matchsticks[u]
22+
}
23+
return false
24+
}
25+
return dfs(len(matchsticks)-1, s/4)
26+
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
class Solution {
2-
public boolean makesquare(int[] nums) {
3-
if (nums == null || nums.length == 0) {
4-
return false;
2+
public boolean makesquare(int[] matchsticks) {
3+
int s = 0, mx = 0;
4+
for (int v : matchsticks) {
5+
s += v;
6+
mx = Math.max(mx, v);
57
}
6-
int sum = Arrays.stream(nums).sum();
7-
if (sum % 4 != 0) {
8+
int x = s / 4, mod = s % 4;
9+
if (mod != 0 || x < mx) {
810
return false;
911
}
10-
Arrays.sort(nums);
11-
int[] lens = new int[4];
12-
return dfs(nums, nums.length - 1, lens, sum / 4);
12+
Arrays.sort(matchsticks);
13+
int[] edges = new int[4];
14+
return dfs(matchsticks.length - 1, x, matchsticks, edges);
1315
}
1416

15-
private boolean dfs(int[] nums, int index, int[] lens, int len) {
16-
if (lens[0] == len && lens[1] == len && lens[2] == len) {
17+
private boolean dfs(int u, int x, int[] matchsticks, int[] edges) {
18+
if (u < 0) {
1719
return true;
1820
}
1921
for (int i = 0; i < 4; ++i) {
20-
if (lens[i] + nums[index] <= len) {
21-
lens[i] += nums[index];
22-
if (dfs(nums, index - 1, lens, len)) {
23-
return true;
24-
}
25-
lens[i] -= nums[index];
22+
edges[i] += matchsticks[u];
23+
if (edges[i] <= x && dfs(u - 1, x, matchsticks, edges)) {
24+
return true;
2625
}
26+
edges[i] -= matchsticks[u];
2727
}
2828
return false;
2929
}
30-
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def makesquare(self, matchsticks: List[int]) -> bool:
3+
def dfs(u):
4+
if u == len(matchsticks):
5+
return True
6+
for i in range(4):
7+
edges[i] += matchsticks[u]
8+
if edges[i] <= x and dfs(u + 1):
9+
return True
10+
edges[i] -= matchsticks[u]
11+
return False
12+
13+
x, mod = divmod(sum(matchsticks), 4)
14+
if mod or x < max(matchsticks):
15+
return False
16+
edges = [0] * 4
17+
matchsticks.sort(reverse=True)
18+
return dfs(0)

0 commit comments

Comments
 (0)