mirror of https://github.com/doocs/leetcode.git
feat: update solutions to lc problems
* No.0473.Matchsticks to Square * No.0698.Partition to K Equal Sum Subsets
This commit is contained in:
parent
74f8ed2512
commit
d7db53e716
|
|
@ -93,6 +93,11 @@
|
|||
- [迷宫](/solution/0400-0499/0490.The%20Maze/README.md) - `DFS`、`连通性模型`、`Flood Fill 算法`
|
||||
- [单词搜索](/solution/0000-0099/0079.Word%20Search/README.md) - `DFS`、`搜索顺序`、`回溯`
|
||||
- [黄金矿工](/solution/1200-1299/1219.Path%20with%20Maximum%20Gold/README.md) - `DFS`、`搜索顺序`、`回溯`
|
||||
- [火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) - `DFS`、`回溯`、`剪枝`
|
||||
- [划分为 k 个相等的子集](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README.md) - `DFS`、`回溯`、`剪枝`
|
||||
- [完成所有工作的最短时间](/solution/1700-1799/1723.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs/README.md) - `DFS`、`回溯`、`剪枝`
|
||||
- [公平分发饼干](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README.md) - `DFS`、`回溯`、`剪枝`
|
||||
|
||||
<!-- DFS 待补充 -->
|
||||
|
||||
### 4. 动态规划(DP)
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
|
|||
- [Range Sum Query - Immutable](/solution/0300-0399/0303.Range%20Sum%20Query%20-%20Immutable/README_EN.md) - `Prefix sum`
|
||||
- [Range Sum Query 2D - Immutable](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README_EN.md) - `Prefix sum`
|
||||
- [Range Addition](/solution/0300-0399/0370.Range%20Addition/README_EN.md) - `Prefix sum`, `Difference array`
|
||||
- [ Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - `Prefix sum`, `Difference array`
|
||||
- [Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - `Prefix sum`, `Difference array`
|
||||
- [Longest Substring Without Repeating Characters](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md) - `Two pointers`, `Hash table`
|
||||
- [Subarray Product Less Than K](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README_EN.md) - `Two pointers`
|
||||
- [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
|
|||
- [The Maze](/solution/0400-0499/0490.The%20Maze/README_EN.md) - `DFS, Flood fill`
|
||||
- [Word Search](/solution/0000-0099/0079.Word%20Search/README_EN.md) - `DFS`, `Backtracking`
|
||||
- [Path with Maximum Gold](/solution/1200-1299/1219.Path%20with%20Maximum%20Gold/README_EN.md) - `DFS`, `Backtracking`
|
||||
- [Matchsticks to Square](/solution/0400-0499/0473.Matchsticks%20to%20Square/README_EN.md) - `DFS`, `Backtracking`
|
||||
- [Partition to K Equal Sum Subsets](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README_EN.md) - `DFS`, `Backtracking`
|
||||
- [Find Minimum Time to Finish All Jobs](/solution/1700-1799/1723.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs/README_EN.md) - `DFS`, `Backtracking`
|
||||
- [Fair Distribution of Cookies](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README_EN.md) - `DFS`, `Backtracking`
|
||||
|
||||
### 4. Dynamic Programming(DP)
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,17 @@
|
|||
|
||||
时间复杂度 $O(4^n)$,其中 $n$ 表示 $matchsticks$ 的长度。每根火柴可以被放入正方形的 $4$ 条边,共有 $n$ 根火柴。
|
||||
|
||||
**方法二:状态压缩 + 记忆化搜索**
|
||||
|
||||
记当前火柴被划分的情况为 $state$。对于第 $i$ 个数,若 $state \ \& \ (1<<i)=0$,说明第 $i$ 个火柴棒未被划分。我们的目标是从全部数字中凑出 $k$ 个和为 $s$ 的子集。
|
||||
|
||||
记当前子集的和为 $t$。在未划分第 $i$ 个火柴棒时:
|
||||
|
||||
- 若 $t+matchsticks[i]>s$,说明第 $i$ 个火柴棒不能被添加到当前子集中,由于我们对 $matchsticks$ 数组进行升序排列,因此从 $matchsticks$ 从第 $i$ 个火柴棒开始的所有数字都不能被添加到当前子集,直接返回 $false$。
|
||||
- 否则,将第 $i$ 个火柴棒添加到当前子集中,状态变为 $state \ |\ (1<<i)$,继续对未划分的数字进行搜索。
|
||||
|
||||
注:若 $t+matchsticks[i]==s$,说明恰好可以得到一个和为 $s$ 的子集,下一步将 $t$ 归零(可以通过 $(t+matchsticks[i]) \%s$ 实现),并继续划分下一个子集。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
|
@ -80,6 +91,29 @@ class Solution:
|
|||
return dfs(0)
|
||||
```
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def makesquare(self, matchsticks: List[int]) -> bool:
|
||||
@cache
|
||||
def dfs(state, t):
|
||||
if state == (1 << len(matchsticks)) - 1:
|
||||
return True
|
||||
for i, v in enumerate(matchsticks):
|
||||
if (state & (1 << i)):
|
||||
continue
|
||||
if t + v > s:
|
||||
break
|
||||
if dfs(state | (1 << i), (t + v) % s):
|
||||
return True
|
||||
return False
|
||||
|
||||
s, mod = divmod(sum(matchsticks), 4)
|
||||
matchsticks.sort()
|
||||
if mod:
|
||||
return False
|
||||
return dfs(0, 0)
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
|
|
|||
|
|
@ -62,6 +62,29 @@ class Solution:
|
|||
return dfs(0)
|
||||
```
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def makesquare(self, matchsticks: List[int]) -> bool:
|
||||
@cache
|
||||
def dfs(state, t):
|
||||
if state == (1 << len(matchsticks)) - 1:
|
||||
return True
|
||||
for i, v in enumerate(matchsticks):
|
||||
if (state & (1 << i)):
|
||||
continue
|
||||
if t + v > s:
|
||||
break
|
||||
if dfs(state | (1 << i), (t + v) % s):
|
||||
return True
|
||||
return False
|
||||
|
||||
s, mod = divmod(sum(matchsticks), 4)
|
||||
matchsticks.sort()
|
||||
if mod:
|
||||
return False
|
||||
return dfs(0, 0)
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
|
|
|
|||
|
|
@ -37,7 +37,20 @@
|
|||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
解法和 [473. 火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) 相同
|
||||
**方法一:DFS + 剪枝**
|
||||
|
||||
解法和 [473. 火柴拼正方形](/solution/0400-0499/0473.Matchsticks%20to%20Square/README.md) 相同。
|
||||
|
||||
**方法二:状态压缩 + 记忆化搜索**
|
||||
|
||||
记当前数字被划分的情况为 $state$。对于第 $i$ 个数,若 $state \ \& \ (1<<i)=0$,说明第 $i$ 个数字未被划分。我们的目标是从全部数字中凑出 $k$ 个和为 $s$ 的子集。
|
||||
|
||||
记当前子集的和为 $t$。在未划分第 $i$ 个数字时:
|
||||
|
||||
- 若 $t+nums[i]>s$,说明第 $i$ 个数字不能被添加到当前子集中,由于我们对 $nums$ 数组进行升序排列,因此从 $nums$ 从第 $i$ 个数字开始的所有数字都不能被添加到当前子集,直接返回 $false$。
|
||||
- 否则,将第 $i$ 个数字添加到当前子集中,状态变为 $state \ |\ (1<<i)$,继续对未划分的数字进行搜索。
|
||||
|
||||
注:若 $t+nums[i]==s$,说明恰好可以得到一个和为 $s$ 的子集,下一步将 $t$ 归零(可以通过 $(t+nums[i]) \%s$ 实现),并继续划分下一个子集。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -72,6 +85,29 @@ class Solution:
|
|||
return dfs(0)
|
||||
```
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
|
||||
@cache
|
||||
def dfs(state, t):
|
||||
if state == (1 << len(nums)) - 1:
|
||||
return True
|
||||
for i, v in enumerate(nums):
|
||||
if (state & (1 << i)):
|
||||
continue
|
||||
if t + v > s:
|
||||
break
|
||||
if dfs(state | (1 << i), (t + v) % s):
|
||||
return True
|
||||
return False
|
||||
|
||||
s, mod = divmod(sum(nums), k)
|
||||
nums.sort()
|
||||
if mod:
|
||||
return False
|
||||
return dfs(0, 0)
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
|
|
|||
|
|
@ -64,6 +64,29 @@ class Solution:
|
|||
return dfs(0)
|
||||
```
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
|
||||
@cache
|
||||
def dfs(state, t):
|
||||
if state == (1 << len(nums)) - 1:
|
||||
return True
|
||||
for i, v in enumerate(nums):
|
||||
if (state & (1 << i)):
|
||||
continue
|
||||
if t + v > s:
|
||||
break
|
||||
if dfs(state | (1 << i), (t + v) % s):
|
||||
return True
|
||||
return False
|
||||
|
||||
s, mod = divmod(sum(nums), k)
|
||||
nums.sort()
|
||||
if mod:
|
||||
return False
|
||||
return dfs(0, 0)
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
|
|
|
|||
Loading…
Reference in New Issue