Skip to content

Commit f327e7e

Browse files
authored
Update 416-partition-equal-subset-sum.js
1 parent 3b93041 commit f327e7e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

416-partition-equal-subset-sum.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,27 @@ function findCombination(nums, target, start) {
6868
return false
6969
}
7070
}
71+
72+
// another
73+
74+
/**
75+
* @param {number[]} nums
76+
* @return {boolean}
77+
*/
78+
function helper(nums, target, pos) {
79+
for (let i = pos; i <= nums.length; i++) {
80+
if (i != pos && nums[i] == nums[i - 1]) continue
81+
if (nums[i] === target) return true
82+
if (nums[i] > target) break
83+
if (helper(nums, target - nums[i], i + 1)) return true
84+
}
85+
return false
86+
}
87+
const canPartition = function(nums) {
88+
const sum = nums.reduce((sum, n) => (sum += n), 0) / 2
89+
if (sum % 1 != 0) {
90+
return false
91+
}
92+
93+
return helper(nums, sum, 0)
94+
}

0 commit comments

Comments
 (0)