Skip to content

Commit c53001c

Browse files
committed
update 53 problem solution
1 parent 6e118ee commit c53001c

File tree

4 files changed

+155
-23
lines changed

4 files changed

+155
-23
lines changed

SUMMARY-LIST.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SUMMARY-LIST
22

33

4-
- Link List
4+
- Linked List
55
- [19. Remove Nth Node From End of List(删除从结尾数第n个结点)](https://leetcode.kyle.link/src/0019.Remove-Nth-Node-From-End-of-List/)
66
- [83. Remove Duplicates from Sorted List(删除链表中的重复元素)](https://leetcode.kyle.link/src/0083.Remove-Duplicates-from-Sorted-List/)
77
- [206. Reverse Linked List(反转链表)](https://leetcode.kyle.link/src/0206.Reverse-Linked-List/)
@@ -16,11 +16,26 @@
1616
- [148. Sort List(链表排序)](https://leetcode.kyle.link/src/0148.Sort-List/)
1717
- [146. LRU Cache (LRU缓存)](https://leetcode.kyle.link/src/0146.LRU-Cache/)
1818

19-
- DFS
19+
- Depth-first Search
2020

2121
- Tree
2222

23-
- DP
23+
- Dynamic Programming
24+
- [53. Maximum Subarray (求最大连续子序列长度)]()
25+
- [300. Longest Increasing Subsequence ()]()
26+
- [72. Edit Distance ()]()
27+
- [121. Best Time to Buy and Sell Stock()]()
28+
- [122. Best Time to Buy and Sell Stock II ()]()
29+
- [123. Best Time to Buy and Sell Stock III ()]()
30+
- [188. Best Time to Buy and Sell Stock IV ()]()
31+
- [309. Best Time to Buy and Sell Stock with Cooldown ()]()
32+
- [198. House Robber ()]()
33+
- [213. House Robber II ()]()
34+
- [312. Burst Balloons ()]()
35+
- [96. Unique Binary Search Trees()]()
36+
- [140. Word Break II()]()
37+
- [10. Regular Expression Matching ()]()
38+
2439

2540
- QUEUE/STACK
2641

src/0053.Maximum-Subarray/README.md

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,75 @@ If you have figured out the O(*n*) solution, try coding another solution using t
1818

1919
**Tags:** Array, Divide and Conquer, Dynamic Programming
2020

21+
## 题意
22+
> 给定一个整数数组 `nums` ,找到``最大的连续子序列(至少包含一个数字),返回该子序列的和
2123
2224
## 题解
2325
### 思路1
24-
> 普通DP 找到公式就好了
25-
```$xslt
26-
如果nums[0]>=0,则sum = sum + nums[1]
27-
如果nums[0]<0,则sum = nums[1],
28-
```
26+
> (动态规划) O(n)
27+
28+
- 1.设 dp(i)dp(i) 表示以第`i`y个数字为结尾的最大连续子序列的``是多少。
29+
- 2.初始化 dp(0)=nums[0]dp(0)=nums[0]
30+
- 3.转移方程 dp(i)=max(dp(i−1)+nums[i],nums[i])dp(i)=max(dp(i−1)+nums[i],nums[i])。可以理解为当前有两种决策,一种是将第`i`个数字和前边的数字拼接起来;另一种是第`i`个数字单独作为一个新的子序列的开始。
31+
- 4.最终答案为ans=max(dp(k)),0≤k<n。
32+
33+
> 时间复杂度
34+
- 状态数为 O(n),决策数为 O(1),故总时间复杂度为 O(n)。
2935

3036
```go
3137
func maxSubArray(nums []int) int {
3238
dp, ans := make([]int, len(nums)), nums[0]
3339
dp[0] = nums[0]
3440

3541
for i := 1; i < len(nums); i++ {
36-
dp[i] = Max(nums[i], nums[i]+dp[i-1])
37-
ans = Max(ans, dp[i])
42+
dp[i] = max(nums[i], nums[i]+dp[i-1])
43+
ans = max(dp[i], ans)
3844
}
39-
4045
return ans
4146
}
4247
```
4348
### 思路2
49+
> (分治) O(nlogn)
50+
51+
- 考虑区间 `[l, r]` 内的答案如何计算,令 `mid = (l + r) / 2`,则该区间的答案由三部分取最大值,分别是:
52+
- (1). 区间 `[l, mid]` 内的答案(递归计算)。
53+
- (2). 区间 `[mid + 1, r]` 内的答案(递归计算)。
54+
- (3). 跨越 mid和mid+1 的连续子序列。
55+
- 其中,第(3)部分只需要从 `mid` 开始向 `l` 找连续的最大值,以及从 `mid+1` 开始向 `r` 找最大值即可,在线性时间内可以完成。
56+
- 递归终止条件显然是 l==r ,此时直接返回` nums[l]`
57+
58+
> 时间复杂度
59+
60+
- 由递归主定理 S(n)=2S(n/2)+O(n),解出总时间复杂度为 O(nlogn)。
61+
- 或者每一层时间复杂度是 O(n),总共有 O(logn) 层,故总时间复杂度是 O(nlogn)。
62+
63+
> Code
64+
```go
65+
func maxSubArray(nums []int) int {
66+
return calc(0, len(nums)-1, nums)
67+
}
68+
69+
func calc(l, r int, nums []int) int {
70+
if l == r {
71+
return nums[l]
72+
}
73+
mid := (l + r) >> 1
74+
lmax, lsum, rmax, rsum := nums[mid], 0, nums[mid+1], 0
75+
76+
for i := mid; i >= 1; i-- {
77+
lsum += nums[i]
78+
lmax = max(lmax, lsum)
79+
}
80+
81+
for i := mid + 1; i <= r; i++ {
82+
rsum += nums[i]
83+
rmax = max(rmax, rsum)
84+
}
85+
86+
return max(max(calc(l, mid, nums), calc(mid+1, r, nums)), lmax+rmax)
87+
}
88+
89+
```
4490

4591

4692
## 结语

src/0053.Maximum-Subarray/Solution.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,40 @@ func maxSubArray(nums []int) int {
55
dp[0] = nums[0]
66

77
for i := 1; i < len(nums); i++ {
8-
dp[i] = Max(nums[i], nums[i]+dp[i-1])
9-
ans = Max(ans, dp[i])
8+
dp[i] = max(nums[i], nums[i]+dp[i-1])
9+
ans = max(dp[i], ans)
10+
//fmt.Println(i, ans, dp, nums)
1011
}
11-
1212
return ans
1313
}
1414

15-
func Max(x, y int) int {
15+
func max(x, y int) int {
1616
if x > y {
1717
return x
1818
}
1919
return y
2020
}
21+
22+
func maxSubArray2(nums []int) int {
23+
return calc(0, len(nums)-1, nums)
24+
}
25+
26+
func calc(l, r int, nums []int) int {
27+
if l == r {
28+
return nums[l]
29+
}
30+
mid := (l + r) >> 1
31+
lmax, lsum, rmax, rsum := nums[mid], 0, nums[mid+1], 0
32+
33+
for i := mid; i >= 1; i-- {
34+
lsum += nums[i]
35+
lmax = max(lmax, lsum)
36+
}
37+
38+
for i := mid + 1; i <= r; i++ {
39+
rsum += nums[i]
40+
rmax = max(rmax, rsum)
41+
}
42+
43+
return max(max(calc(l, mid, nums), calc(mid+1, r, nums)), lmax+rmax)
44+
}
Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
11
package Solution
22

3-
import "testing"
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
48

59
func TestSolution(t *testing.T) {
6-
t.Run("Test-1", func(t *testing.T) {
7-
got := maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4})
8-
want := 6
9-
if got != want {
10-
t.Error("GOT:", got, "WANT:", want)
11-
}
12-
})
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs []int
14+
expect int
15+
}{
16+
{"TestCase", []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}, 6},
17+
}
18+
19+
// 开始测试
20+
for i, c := range cases {
21+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
22+
got := maxSubArray(c.inputs)
23+
if !reflect.DeepEqual(got, c.expect) {
24+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
25+
c.expect, got, c.inputs)
26+
}
27+
})
28+
}
29+
}
30+
31+
func TestSolution2(t *testing.T) {
32+
// 测试用例
33+
cases := []struct {
34+
name string
35+
inputs []int
36+
expect int
37+
}{
38+
{"TestCase", []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}, 6},
39+
}
40+
41+
// 开始测试
42+
for i, c := range cases {
43+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
44+
got := maxSubArray2(c.inputs)
45+
if !reflect.DeepEqual(got, c.expect) {
46+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
47+
c.expect, got, c.inputs)
48+
}
49+
})
50+
}
51+
}
52+
53+
// 压力测试
54+
func BenchmarkSolution(b *testing.B) {
55+
56+
}
57+
58+
// 使用案列
59+
func ExampleSolution() {
1360

1461
}

0 commit comments

Comments
 (0)