Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7db53e

Browse files
committed
feat: update solutions to lc problems
* No.0473.Matchsticks to Square * No.0698.Partition to K Equal Sum Subsets
1 parent 74f8ed2 commit d7db53e

File tree

6 files changed

+127
-2
lines changed

6 files changed

+127
-2
lines changed

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
- [迷宫](/solution/0400-0499/0490.The%20Maze/README.md) - `DFS``连通性模型``Flood Fill 算法`
9494
- [单词搜索](/solution/0000-0099/0079.Word%20Search/README.md) - `DFS``搜索顺序``回溯`
9595
- [黄金矿工](/solution/1200-1299/1219.Path%20with%20Maximum%20Gold/README.md) - `DFS``搜索顺序``回溯`
96+
- [火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) - `DFS``回溯``剪枝`
97+
- [划分为 k 个相等的子集](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README.md) - `DFS``回溯``剪枝`
98+
- [完成所有工作的最短时间](/solution/1700-1799/1723.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs/README.md) - `DFS``回溯``剪枝`
99+
- [公平分发饼干](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README.md) - `DFS``回溯``剪枝`
100+
96101
<!-- DFS 待补充 -->
97102

98103
### 4. 动态规划(DP)

‎README_EN.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
4444
- [Range Sum Query - Immutable](/solution/0300-0399/0303.Range%20Sum%20Query%20-%20Immutable/README_EN.md) - `Prefix sum`
4545
- [Range Sum Query 2D - Immutable](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README_EN.md) - `Prefix sum`
4646
- [Range Addition](/solution/0300-0399/0370.Range%20Addition/README_EN.md) - `Prefix sum`, `Difference array`
47-
- [Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - `Prefix sum`, `Difference array`
47+
- [Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - `Prefix sum`, `Difference array`
4848
- [Longest Substring Without Repeating Characters](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md) - `Two pointers`, `Hash table`
4949
- [Subarray Product Less Than K](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README_EN.md) - `Two pointers`
5050
- [Number of 1 Bits](/solution/0100-0199/0191.Number%20of%201%20Bits/README_EN.md) - `Bit manipulation`, `Lowbit`
@@ -91,6 +91,10 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
9191
- [The Maze](/solution/0400-0499/0490.The%20Maze/README_EN.md) - `DFS, Flood fill`
9292
- [Word Search](/solution/0000-0099/0079.Word%20Search/README_EN.md) - `DFS`, `Backtracking`
9393
- [Path with Maximum Gold](/solution/1200-1299/1219.Path%20with%20Maximum%20Gold/README_EN.md) - `DFS`, `Backtracking`
94+
- [Matchsticks to Square](/solution/0400-0499/0473.Matchsticks%20to%20Square/README_EN.md) - `DFS`, `Backtracking`
95+
- [Partition to K Equal Sum Subsets](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README_EN.md) - `DFS`, `Backtracking`
96+
- [Find Minimum Time to Finish All Jobs](/solution/1700-1799/1723.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs/README_EN.md) - `DFS`, `Backtracking`
97+
- [Fair Distribution of Cookies](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README_EN.md) - `DFS`, `Backtracking`
9498

9599
### 4. Dynamic Programming(DP)
96100

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@
5151

5252
时间复杂度 $O(4^n),ドル其中 $n$ 表示 $matchsticks$ 的长度。每根火柴可以被放入正方形的 4ドル$ 条边,共有 $n$ 根火柴。
5353

54+
**方法二:状态压缩 + 记忆化搜索**
55+
56+
记当前火柴被划分的情况为 $state$。对于第 $i$ 个数,若 $state \ \& \ (1<<i)=0,ドル说明第 $i$ 个火柴棒未被划分。我们的目标是从全部数字中凑出 $k$ 个和为 $s$ 的子集。
57+
58+
记当前子集的和为 $t$。在未划分第 $i$ 个火柴棒时:
59+
60+
- 若 $t+matchsticks[i]>s,ドル说明第 $i$ 个火柴棒不能被添加到当前子集中,由于我们对 $matchsticks$ 数组进行升序排列,因此从 $matchsticks$ 从第 $i$ 个火柴棒开始的所有数字都不能被添加到当前子集,直接返回 $false$。
61+
- 否则,将第 $i$ 个火柴棒添加到当前子集中,状态变为 $state \ |\ (1<<i),ドル继续对未划分的数字进行搜索。
62+
63+
注:若 $t+matchsticks[i]==s,ドル说明恰好可以得到一个和为 $s$ 的子集,下一步将 $t$ 归零(可以通过 $(t+matchsticks[i]) \%s$ 实现),并继续划分下一个子集。
64+
5465
<!-- tabs:start -->
5566

5667
### **Python3**
@@ -80,6 +91,29 @@ class Solution:
8091
return dfs(0)
8192
```
8293

94+
```python
95+
class Solution:
96+
def makesquare(self, matchsticks: List[int]) -> bool:
97+
@cache
98+
def dfs(state, t):
99+
if state == (1 << len(matchsticks)) - 1:
100+
return True
101+
for i, v in enumerate(matchsticks):
102+
if (state & (1 << i)):
103+
continue
104+
if t + v > s:
105+
break
106+
if dfs(state | (1 << i), (t + v) % s):
107+
return True
108+
return False
109+
110+
s, mod = divmod(sum(matchsticks), 4)
111+
matchsticks.sort()
112+
if mod:
113+
return False
114+
return dfs(0, 0)
115+
```
116+
83117
### **Java**
84118

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

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ class Solution:
6262
return dfs(0)
6363
```
6464

65+
```python
66+
class Solution:
67+
def makesquare(self, matchsticks: List[int]) -> bool:
68+
@cache
69+
def dfs(state, t):
70+
if state == (1 << len(matchsticks)) - 1:
71+
return True
72+
for i, v in enumerate(matchsticks):
73+
if (state & (1 << i)):
74+
continue
75+
if t + v > s:
76+
break
77+
if dfs(state | (1 << i), (t + v) % s):
78+
return True
79+
return False
80+
81+
s, mod = divmod(sum(matchsticks), 4)
82+
matchsticks.sort()
83+
if mod:
84+
return False
85+
return dfs(0, 0)
86+
```
87+
6588
### **Java**
6689

6790
```java

‎solution/0600-0699/0698.Partition to K Equal Sum Subsets/README.md‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40-
解法和 [473. 火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) 相同
40+
**方法一:DFS + 剪枝**
41+
42+
解法和 [473. 火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) 相同。
43+
44+
**方法二:状态压缩 + 记忆化搜索**
45+
46+
记当前数字被划分的情况为 $state$。对于第 $i$ 个数,若 $state \ \& \ (1<<i)=0,ドル说明第 $i$ 个数字未被划分。我们的目标是从全部数字中凑出 $k$ 个和为 $s$ 的子集。
47+
48+
记当前子集的和为 $t$。在未划分第 $i$ 个数字时:
49+
50+
- 若 $t+nums[i]>s,ドル说明第 $i$ 个数字不能被添加到当前子集中,由于我们对 $nums$ 数组进行升序排列,因此从 $nums$ 从第 $i$ 个数字开始的所有数字都不能被添加到当前子集,直接返回 $false$。
51+
- 否则,将第 $i$ 个数字添加到当前子集中,状态变为 $state \ |\ (1<<i),ドル继续对未划分的数字进行搜索。
52+
53+
注:若 $t+nums[i]==s,ドル说明恰好可以得到一个和为 $s$ 的子集,下一步将 $t$ 归零(可以通过 $(t+nums[i]) \%s$ 实现),并继续划分下一个子集。
4154

4255
<!-- tabs:start -->
4356

@@ -72,6 +85,29 @@ class Solution:
7285
return dfs(0)
7386
```
7487

88+
```python
89+
class Solution:
90+
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
91+
@cache
92+
def dfs(state, t):
93+
if state == (1 << len(nums)) - 1:
94+
return True
95+
for i, v in enumerate(nums):
96+
if (state & (1 << i)):
97+
continue
98+
if t + v > s:
99+
break
100+
if dfs(state | (1 << i), (t + v) % s):
101+
return True
102+
return False
103+
104+
s, mod = divmod(sum(nums), k)
105+
nums.sort()
106+
if mod:
107+
return False
108+
return dfs(0, 0)
109+
```
110+
75111
### **Java**
76112

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

‎solution/0600-0699/0698.Partition to K Equal Sum Subsets/README_EN.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ class Solution:
6464
return dfs(0)
6565
```
6666

67+
```python
68+
class Solution:
69+
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
70+
@cache
71+
def dfs(state, t):
72+
if state == (1 << len(nums)) - 1:
73+
return True
74+
for i, v in enumerate(nums):
75+
if (state & (1 << i)):
76+
continue
77+
if t + v > s:
78+
break
79+
if dfs(state | (1 << i), (t + v) % s):
80+
return True
81+
return False
82+
83+
s, mod = divmod(sum(nums), k)
84+
nums.sort()
85+
if mod:
86+
return False
87+
return dfs(0, 0)
88+
```
89+
6790
### **Java**
6891

6992
```java

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /