-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCR101-PartitionEqualSubsetSum.go
More file actions
56 lines (49 loc) · 1.62 KB
/
LCR101-PartitionEqualSubsetSum.go
File metadata and controls
56 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
// LCR 101. 分割等和子集
// 给定一个非空的正整数数组 nums ,请判断能否将这些数字分成元素和相等的两部分。
// 示例 1:
// 输入:nums = [1,5,11,5]
// 输出:true
// 解释:nums 可以分割成 [1, 5, 5] 和 [11] 。
// 示例 2:
// 输入:nums = [1,2,3,5]
// 输出:false
// 解释:nums 不可以分为和相等的两部分
// 提示:
// 1 <= nums.length <= 200
// 1 <= nums[i] <= 100
import "fmt"
// dp
func canPartition(nums []int) bool {
sum := 0
for _, v := range nums { // 累加
sum += v
}
if sum % 2 == 1 { // 积为奇数直接返回
return false
}
half := sum / 2
dp := make([]bool,half, half) // array to mark reachable numbers
dp[0] = true
for _, n := range nums {
if n <= half { // to skip too big numbers
if dp[half - n] == true { // we found our sum
return true
}
for j:= half - n - 1; j >= 0; j-- { // we loop in opposite direction, because we don't want to check index and then loop over it
if dp[j] == true {
dp[j+n] = true
}
}
}
}
return false
}
func main() {
// Explanation: The array can be partitioned as [1, 5, 5] and [11].
fmt.Println(canPartition([]int{1,5,11,5})) // true
// Explanation: The array cannot be partitioned into equal sum subsets.
fmt.Println(canPartition([]int{1,2,3,5})) // false
fmt.Println(canPartition([]int{1,2,3,4,5})) // false
fmt.Println(canPartition([]int{6,2,3,4,5})) // true
}