Skip to content

Commit ee69172

Browse files
committed
add: partition-equal-subset-sum
1 parent bd7d310 commit ee69172

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LeetCode
22

3-
![](https://img.shields.io/badge/solved-225-337ab7?style=for-the-badge&logo=appveyor.svg) 
3+
![](https://img.shields.io/badge/solved-226-337ab7?style=for-the-badge&logo=appveyor.svg) 
44
![](https://img.shields.io/badge/language-Java-yellow?style=for-the-badge&logo=appveyor.svg)
55

66
LeetCode Solution in Java
@@ -379,6 +379,7 @@ LeetCode Solution in Java
379379
|322|[Coin Change (Classic)](https://leetcode.com/problems/coin-change/)|[Java](dynamic-programming/coin-change/README.md)|Medium|
380380
|343|[Integer Break](https://leetcode.com/problems/integer-break/)|[Java](dynamic-programming/integer-break/README.md)|Medium|
381381
|368|[Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/)|[Java](dynamic-programming/largest-divisible-subset/README.md)|Medium|
382+
|416|[Partition Equal Subset Sum (Classic)](https://leetcode.com/problems/partition-equal-subset-sum/)|[Java](dynamic-programming/partition-equal-subset-sum/README.md)|Medium|
382383
|486|[Predict the Winner (Classic)](https://leetcode.com/problems/predict-the-winner/)|[Java](dynamic-programming/predict-the-winner/README.md)|Medium|
383384
|516|[Longest Palindromic Subsequence (Classic)](https://leetcode.com/problems/longest-palindromic-subsequence/)|[Java](dynamic-programming/longest-palindromic-subsequence/README.md)|Medium|
384385
|518|[Coin Change 2 (Classic)](https://leetcode.com/problems/coin-change-2/)|[Java](dynamic-programming/coin-change-2/README.md)|Medium|
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Partition Equal Subset Sum
2+
3+
## Solution 1
4+
5+
DFS
6+
7+
```java
8+
/**
9+
* Question : 416. Partition Equal Subset Sum
10+
* Complexity : Time: O(2^n) ; Space: O(n)
11+
* Topics : DP
12+
*/
13+
class Solution {
14+
public boolean canPartition(int[] nums) {
15+
int totalSum = 0;
16+
for (int num : nums) {
17+
totalSum += num;
18+
}
19+
20+
if (totalSum % 2 != 0) {
21+
return false;
22+
}
23+
24+
return canPartitionUtil(nums, totalSum / 2, nums.length);
25+
}
26+
27+
private boolean canPartitionUtil(int[] nums, int sum, int n) {
28+
if (sum == 0) {
29+
return true;
30+
}
31+
if (sum < 0 || n == 0) {
32+
return false;
33+
}
34+
35+
if (nums[n - 1] > sum) {
36+
return canPartitionUtil(nums, sum, n - 1);
37+
}
38+
return canPartitionUtil(nums, sum - nums[n - 1], n - 1) || canPartitionUtil(nums, sum, n - 1);
39+
}
40+
}
41+
```
42+
43+
## Solution 2
44+
45+
DP
46+
47+
```java
48+
/**
49+
* Question : 416. Partition Equal Subset Sum
50+
* Complexity : Time: O(n * amount) ; Space: O(n * amount)
51+
* Topics : DP
52+
*/
53+
class Solution {
54+
public boolean canPartition(int[] nums) {
55+
int totalSum = 0;
56+
for (int num : nums) {
57+
totalSum += num;
58+
}
59+
60+
if (totalSum % 2 != 0) {
61+
return false;
62+
}
63+
64+
int sum = totalSum / 2;
65+
int n = nums.length;
66+
67+
// dp[i][j]: if we can generate j amount using i numbers.
68+
boolean[][] dp = new boolean[n + 1][sum + 1];
69+
for (int i = 0; i <= n; i++) {
70+
dp[i][0] = true;
71+
}
72+
73+
for (int i = 1; i <= n; i++) {
74+
for (int currSum = 1; currSum <= sum; currSum++) {
75+
if (currSum >= nums[i - 1]) {
76+
dp[i][currSum] = dp[i - 1][currSum] || dp[i - 1][currSum - nums[i - 1]];
77+
} else {
78+
dp[i][currSum] = dp[i - 1][currSum];
79+
}
80+
}
81+
}
82+
83+
return dp[n][sum];
84+
}
85+
}
86+
```

0 commit comments

Comments
 (0)