URL: https://leetcode.com/problems/partition-equal-subset-sum/ Signed-off-by: Matej Focko <me@mfocko.xyz>
26 lines
588 B
Kotlin
26 lines
588 B
Kotlin
class Solution {
|
|
fun canPartition(nums: IntArray): Boolean {
|
|
val total = nums.sum()
|
|
if (total % 2 != 0) {
|
|
return false
|
|
}
|
|
|
|
val target = total / 2
|
|
val dp = BooleanArray(target + 1)
|
|
|
|
dp[0] = true
|
|
for (num in nums) {
|
|
var current = target
|
|
while (current >= num) {
|
|
dp[current] = dp[current] || dp[current - num]
|
|
if (dp[target]) {
|
|
return true
|
|
}
|
|
|
|
current--
|
|
}
|
|
}
|
|
|
|
return dp[target]
|
|
}
|
|
}
|