Timeline
Timeline
2026-03-21
init
Dynamic Programming
Problem:
Sum the subarray, then divide the sum by 2, transform to find a subset of the array that sums to half of the total, which is a 0-1 knapsack problem
dp[i][j] indicates whether there exists a selection of some positive integers (possibly zero) from the subarray [0,i] such that the sum of selected positive integers equals j. Initially, all elements in dp are false.
Initialization:
- If no positive integer is selected, the sum of selected positive integers is 0. Therefore, for all 0 ≤ i < n, dp[i][0] = true.
- When i==0, there is only one positive integer nums[0] that can be selected, so dp[0][nums[0]] = true.
State transition:
dp[i][j] = dp[i−1][j] ∣ dp[i−1][j−nums[i]](j >= nums[i])dp[i][j] = dp[i-1][j](j < nums[i])
1 |
|
