File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -453,6 +453,7 @@ class Solution:
453453```
454454
455455### Go:
456+ 一维dp
456457``` go
457458// 分割等和子集 动态规划
458459// 时间复杂度O(n^2) 空间复杂度O(n)
@@ -480,6 +481,44 @@ func canPartition(nums []int) bool {
480481}
481482```
482483
484+ 二维dp
485+ ``` go
486+ func canPartition (nums []int ) bool {
487+ sum := 0
488+ for _ , val := range nums {
489+ sum += val
490+ }
491+ if sum % 2 == 1 {
492+ return false
493+ }
494+ target := sum / 2
495+ dp := make ([][]int , len (nums))
496+ for i := range dp {
497+ dp[i] = make ([]int , target + 1 )
498+ }
499+ for j := nums[0 ]; j <= target; j++ {
500+ dp[0 ][j] = nums[0 ]
501+ }
502+ for i := 1 ; i < len (nums); i++ {
503+ for j := 0 ; j <= target; j++ {
504+ if j < nums[i] {
505+ dp[i][j] = dp[i-1 ][j]
506+ } else {
507+ dp[i][j] = max (dp[i-1 ][j], dp[i-1 ][j-nums[i]] + nums[i])
508+ }
509+ }
510+ }
511+ return dp[len (nums)-1 ][target] == target
512+ }
513+
514+ func max (x , y int ) int {
515+ if x > y {
516+ return x
517+ }
518+ return y
519+ }
520+ ```
521+
483522### JavaScript:
484523
485524``` js
You can’t perform that action at this time.
0 commit comments