Skip to content

Commit 9d0a09a

Browse files
committed
Apr 07
1 parent 6b7682b commit 9d0a09a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def canPartition(self, nums: list[int]) -> bool:
6+
total = sum(nums)
7+
if total & 1 != 0:
8+
return False
9+
else:
10+
total = total // 2
11+
12+
@cache
13+
def dfs(index: int, target: int) -> bool:
14+
if target < 0:
15+
return False
16+
if target == 0:
17+
return True
18+
19+
for i, num in enumerate(nums[index:], index):
20+
if dfs(i+1, target-num):
21+
return True
22+
return False
23+
24+
nums.sort(reverse=True)
25+
return dfs(0, total)
26+
27+
28+
def main():
29+
nums = [1, 5, 11, 5]
30+
assert Solution().canPartition(nums) is True
31+
32+
nums = [1, 2, 3, 5]
33+
assert Solution().canPartition(nums) is False
34+
35+
36+
if __name__ == '__main__':
37+
main()

2025-04-April-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| April 04 | [1123. Lowest Common Ancestor of Deepest Leaves](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/) | Medium | Solved |
1111
| April 05 | [1863. Sum of All Subset XOR Totals](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) | Easy | Solved |
1212
| April 06 | [368. Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | Medium | Unsolved |
13-
| April 07 | []() | | |
13+
| April 07 | [416. Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | Medium | Solved |
1414
| April 08 | []() | | |
1515
| April 09 | []() | | |
1616
| April 10 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 2 | 2 | 0 |
43-
| Medium | 4 | 3 | 1 |
43+
| Medium | 5 | 4 | 1 |
4444
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)