Skip to content

Latest commit

 

History

History
27 lines (27 loc) · 671 Bytes

partition-equal-subset-sum.md

File metadata and controls

27 lines (27 loc) · 671 Bytes
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canPartition = function(nums) {
  let sum = nums.reduce((res, cur) => res + cur, 0);
  if (sum % 2 !== 0) return false;
  const target = sum / 2;
  const len = nums.length;
  const dp = new Array(len);
  for (let i = 0; i <= len; i++) {
    dp[i] = new Array(target + 1).fill(false);
  }
  for (let i = 1; i <= nums.length; i++) {
    dp[i][0] = true; // bad case
    for (let j = 1; j <= target; j++) {
      if (j - nums[i - 1] >= 0) {
        dp[i][j] = dp[i-1][j]|| dp[i-1][j - nums[i - 1]];
      } else {
        dp[i][j] = dp[i-1][j]
      }
    }
  }
  return dp[nums.length][target]
};