-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53-MaximumSubarray.go
More file actions
159 lines (140 loc) · 5.14 KB
/
53-MaximumSubarray.go
File metadata and controls
159 lines (140 loc) · 5.14 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
// 53. Maximum Subarray
// Given an integer array nums, find the subarray with the largest sum, and return its sum.
// Example 1:
// Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
// Output: 6
// Explanation: The subarray [4,-1,2,1] has the largest sum 6.
// Example 2:
// Input: nums = [1]
// Output: 1
// Explanation: The subarray [1] has the largest sum 1.
// Example 3:
// Input: nums = [5,4,-1,7,8]
// Output: 23
// Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
// Constraints:
// 1 <= nums.length <= 10^5
// -10^4 <= nums[i] <= 10^4
// Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
import "fmt"
func maxSubArray(nums []int) int {
mx, sum := -1 >> 32 - 1, 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := range nums {
sum = max(sum + nums[i], nums[i]) // 如果累加值都小于当前值,从当前值开始
mx = max(mx, sum)
}
return mx
}
func maxSubArray1(nums []int) int {
var l = len(nums)
var max = 0
array := make([][]int, l)
for i := 0; i < l; i++ {
subArray := make([]int, l)
subArray[0] = nums[i]
for j := i + 1; j < l; j++ {
subArray[i] += nums[j]
if subArray[i] > max {
max = subArray[i]
}
}
array[i] = subArray
}
// // 输出
// for i := range array {
// for j := range array[i] {
// fmt.Printf("%v ", array[i][j])
// }
// fmt.Println()
// }
return max
}
// DP
func maxSubArray2(nums []int) int {
if len(nums) == 0 {
return 0
}
if len(nums) == 1 {
return nums[0]
}
max := func (a int, b int) int { if a > b { return a; }; return b; }
dp, res := make([]int, len(nums)), nums[0]
dp[0] = nums[0]
for i := 1; i < len(nums); i++ {
if dp[i-1] > 0 { // 如果值大于 0 则累加
dp[i] = nums[i] + dp[i-1]
} else {
dp[i] = nums[i]
}
res = max(res, dp[i])
}
return res
}
// 模拟
func maxSubArray3(nums []int) int {
if len(nums) == 1 {
return nums[0]
}
res, sum, p := nums[0], 0, 0
for p < len(nums) {
sum += nums[p]
if sum > res {
res = sum
}
if sum < 0 {
sum = 0
}
p++
}
return res
}
// best solution 快慢指针
func maxSubArray4(nums []int) int {
res, sum, fast, slow, n := -1 >> 32 - 1, 0, 0, 0, len(nums)
for fast < n && slow < n {
sum = sum + nums[fast]
if sum > res {
res = sum
}
for sum< 0 {
sum = sum - nums[slow]
slow++
}
fast++
}
return res
}
func main() {
fmt.Printf("maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}) = %v\n",maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})) // 6
fmt.Printf("maxSubArray([]int{1}) = %v\n",maxSubArray([]int{1})) // 1
fmt.Printf("maxSubArray([]int{5,4,-1,7,8}) = %v\n",maxSubArray([]int{5,4,-1,7,8})) // 23
fmt.Println(maxSubArray([]int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(maxSubArray([]int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(maxSubArray([]int{-3,-3,-3})) // -3
fmt.Printf("maxSubArray1([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}) = %v\n",maxSubArray1([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})) // 6
fmt.Printf("maxSubArray1([]int{1}) = %v\n",maxSubArray1([]int{1})) // 1
fmt.Printf("maxSubArray1([]int{5,4,-1,7,8}) = %v\n",maxSubArray1([]int{5,4,-1,7,8})) // 23
fmt.Println(maxSubArray1([]int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(maxSubArray1([]int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(maxSubArray1([]int{-3,-3,-3})) // -3
fmt.Printf("maxSubArray2([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}) = %v\n",maxSubArray2([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})) // 6
fmt.Printf("maxSubArray2([]int{1}) = %v\n",maxSubArray2([]int{1})) // 1
fmt.Printf("maxSubArray2([]int{5,4,-1,7,8}) = %v\n",maxSubArray2([]int{5,4,-1,7,8})) // 23
fmt.Println(maxSubArray2([]int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(maxSubArray2([]int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(maxSubArray2([]int{-3,-3,-3})) // -3
fmt.Printf("maxSubArray3([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}) = %v\n",maxSubArray3([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})) // 6
fmt.Printf("maxSubArray3([]int{1}) = %v\n",maxSubArray3([]int{1})) // 1
fmt.Printf("maxSubArray3([]int{5,4,-1,7,8}) = %v\n",maxSubArray3([]int{5,4,-1,7,8})) // 23
fmt.Println(maxSubArray3([]int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(maxSubArray3([]int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(maxSubArray3([]int{-3,-3,-3})) // -3
fmt.Printf("maxSubArray4([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}) = %v\n",maxSubArray4([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})) // 6
fmt.Printf("maxSubArray4([]int{1}) = %v\n",maxSubArray4([]int{1})) // 1
fmt.Printf("maxSubArray4([]int{5,4,-1,7,8}) = %v\n",maxSubArray4([]int{5,4,-1,7,8})) // 23
fmt.Println(maxSubArray4([]int{1,2,3,4,5,6,7,8,9})) // 45
fmt.Println(maxSubArray4([]int{9,8,7,6,5,4,3,2,1})) // 45
fmt.Println(maxSubArray4([]int{-3,-3,-3})) // -3
}