-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
97 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 23 additions & 12 deletions
35
src/main/java/g0401_0500/s0416_partition_equal_subset_sum/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
package g0401_0500.s0416_partition_equal_subset_sum; | ||
|
||
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming | ||
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2022_12_29_Time_27_ms_(94.53%)_Space_41.8_MB_(95.29%) | ||
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_11_17_Time_5_ms_(99.88%)_Space_42.2_MB_(85.79%) | ||
|
||
public class Solution { | ||
public boolean canPartition(int[] nums) { | ||
int sums = 0; | ||
for (int num : nums) { | ||
sums += num; | ||
int sum = 0; | ||
for (int val : nums) { | ||
sum += val; | ||
} | ||
if (sums % 2 == 1) { | ||
if (sum % 2 != 0) { | ||
return false; | ||
} | ||
sums /= 2; | ||
boolean[] dp = new boolean[sums + 1]; | ||
dp[0] = true; | ||
for (int num : nums) { | ||
for (int sum = sums; sum >= num; sum--) { | ||
dp[sum] = dp[sum] || dp[sum - num]; | ||
sum /= 2; | ||
boolean[] set = new boolean[sum + 1]; | ||
int[] arr = new int[sum + 2]; | ||
int top = 0; | ||
for (int val : nums) { | ||
if (val == sum) { | ||
return true; | ||
} | ||
for (int i = top; i > -1; i--) { | ||
int tempSum = val + arr[i]; | ||
if (tempSum <= sum && !set[tempSum]) { | ||
if (tempSum == sum) { | ||
return true; | ||
} | ||
set[tempSum] = true; | ||
arr[++top] = tempSum; | ||
} | ||
} | ||
} | ||
return dp[sums]; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.