ptmdmusique

https://leetcode.com/problems/partition-equal-subset-sum/sub

Sep 24th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. class Solution {
  2.     public int[][] remain;
  3.    
  4.     public int sum(int[] arr) {
  5.         int total = 0;
  6.         for (int data : arr) {
  7.             total += data;
  8.         }
  9.        
  10.         return total;
  11.     }
  12.    
  13.     /*
  14.         Since we are finding if there is a way to partition the set
  15.             into 2 subsets that have equal sum (let's call it x1 and x2)
  16.             then we can check if there is a subset that has
  17.             the sum equal to half
  18.         Because x1 = x2 and x1 + x2 = total => x1 = x2 = total / 2
  19.     */
  20.    
  21.     /*
  22.         To find if there is a subset that can be sum up to a certain number,
  23.             the idea is have a 2d array[][] where arr[i][j] represents
  24.                 if we haven't gone through that path (0),
  25.                 can't be summed up to sum (-1),
  26.                 can be summed up to sum (1).
  27.         We can then search for by going:
  28.             arr[half][i] -> arr[half - i][i'] -> ... until arr[n][m] = 0
  29.             where i, i', m belongs to nums[]
  30.     */
  31.     public boolean canPartition(int[] nums) {
  32.         int total = sum(nums);
  33.         //Meaningless if the total is odd
  34.         if (total % 2 != 0) {
  35.             return false;
  36.         }
  37.        
  38.         int half = total / 2;   //Find half of the total sum
  39.         remain = new int[half + 1][nums.length];
  40.        
  41.         //Fill the base cases
  42.         for (int indx = 0; indx < nums.length; indx++) {
  43.             remain[0][indx] = 1;
  44.         }
  45.        
  46.         //Fill out the rest
  47.         fillTable(nums, half, nums.length - 1);
  48.        
  49.         boolean found = false;
  50.         for(int data : remain[half]) {
  51.             if (data == 1) {
  52.                 found = true;
  53.                 break;
  54.             }
  55.         }
  56.        
  57.         //Check if there is a subset that has the sum equals to that half
  58.         return found;
  59.     }
  60.    
  61.     public boolean fillTable(int[] nums, int curSum, int index) {
  62.         if (curSum < 0 || index < 0) {
  63.             //Invalid
  64.             return false;
  65.         }
  66.        
  67.         //No need to check again
  68.         if (remain[curSum][index] != 0) {
  69.             return remain[curSum][index] == 1 ? true : false;    
  70.         }
  71.        
  72.         //Default
  73.         remain[curSum][index] = -1;
  74.         boolean found = false;
  75.        
  76.         for (int indx = index; indx > -1 && found != true; indx--) {
  77.             found = fillTable(nums, curSum - nums[indx], indx - 1);
  78.         }    
  79.        
  80.         if (found == true) {
  81.             remain[curSum][index] = 1;    
  82.         }
  83.        
  84.         return found;
  85.     }
  86.    
  87. }
Add Comment
Please, Sign In to add comment