|
| 1 | +package solutions; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +// [Problem] https://leetcode.com/problems/partition-equal-subset-sum |
| 9 | +class PartitionEqualSubsetSum { |
| 10 | + // Top-down DP with memo |
| 11 | + // O(n * s) time, O(n * s) space |
| 12 | + // where n = input length, s = subset sum |
| 13 | + public boolean canPartition(int[] nums) { |
| 14 | + int numLen = nums.length; |
| 15 | + int sum = 0; |
| 16 | + for (int num : nums) { |
| 17 | + sum += num; |
| 18 | + } |
| 19 | + if (sum % 2 != 0) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + int subsetSum = sum / 2; |
| 23 | + Boolean[][] memo = new Boolean[numLen][subsetSum + 1]; |
| 24 | + return canPartitionRecursive(memo, nums, 0, subsetSum); |
| 25 | + } |
| 26 | + |
| 27 | + private boolean canPartitionRecursive(Boolean[][] memo, int[] nums, int currentIndex, int currentSum) { |
| 28 | + if (currentSum == 0) { |
| 29 | + return true; |
| 30 | + } |
| 31 | + if (currentIndex >= nums.length) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + if (memo[currentIndex][currentSum] == null) { |
| 35 | + if (nums[currentIndex] <= currentSum |
| 36 | + && canPartitionRecursive(memo, nums, currentIndex + 1, currentSum - nums[currentIndex])) { |
| 37 | + memo[currentIndex][currentSum] = true; |
| 38 | + return true; |
| 39 | + } |
| 40 | + memo[currentIndex][currentSum] = canPartitionRecursive(memo, nums, currentIndex + 1, currentSum); |
| 41 | + } |
| 42 | + return memo[currentIndex][currentSum]; |
| 43 | + } |
| 44 | + |
| 45 | + // Bottom-up DP |
| 46 | + // O(n * s) time, O(n * s) space |
| 47 | + public boolean canPartitionBottomUp(int[] nums) { |
| 48 | + int numLen = nums.length; |
| 49 | + int sum = 0; |
| 50 | + for (int num : nums) { |
| 51 | + sum += num; |
| 52 | + } |
| 53 | + if (sum % 2 != 0) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + int subsetSum = sum / 2; |
| 57 | + boolean[][] dp = new boolean[numLen][subsetSum + 1]; |
| 58 | + // first column - always true as sum of empty subset is zero |
| 59 | + for (int n = 0; n < numLen; n++) { |
| 60 | + dp[n][0] = true; |
| 61 | + } |
| 62 | + // first row - true if its value is equal to subset sum |
| 63 | + for (int s = 1; s <= subsetSum; s++) { |
| 64 | + dp[0][s] = nums[0] == s; |
| 65 | + } |
| 66 | + for (int n = 1; n < numLen; n++) { |
| 67 | + for (int s = 1; s <= subsetSum; s++) { |
| 68 | + if (s >= nums[n]) { |
| 69 | + dp[n][s] = dp[n - 1][s] || dp[n - 1][s - nums[n]]; |
| 70 | + } else { |
| 71 | + dp[n][s] = dp[n - 1][s]; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return dp[numLen - 1][subsetSum]; |
| 76 | + } |
| 77 | + |
| 78 | + // Test |
| 79 | + public static void main(String[] args) { |
| 80 | + PartitionEqualSubsetSum solution = new PartitionEqualSubsetSum(); |
| 81 | + |
| 82 | + int[] input1 = {1, 5, 11, 5}; |
| 83 | + boolean output1 = solution.canPartition(input1); |
| 84 | + System.out.println("Test 1 passed? " + (output1 == true)); |
| 85 | + |
| 86 | + int[] input2 = {1, 2, 3, 5}; |
| 87 | + boolean output2 = solution.canPartition(input2); |
| 88 | + System.out.println("Test 2 passed? " + (output2 == false)); |
| 89 | + } |
| 90 | +} |
0 commit comments