Skip to content

Commit 5dd4f1b

Browse files
committed
Add solution 053
1 parent 6194534 commit 5dd4f1b

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Complete solutions to Leetcode problems, updated daily.
1616
| 007 | [Reverse Integer](https://github.com/yanglbme/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
1717
| 013 | [Roman to Integer](https://github.com/yanglbme/leetcode/tree/master/solution/013.Roman%20to%20Integer) | `Math`, `String` |
1818
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
19+
| 053 | [Maximum Subarray](https://github.com/yanglbme/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
1920
| 070 | [Climbing Stairs](https://github.com/yanglbme/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
2021
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
2122
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## 最大子序和
2+
### 题目描述
3+
4+
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
5+
6+
示例:
7+
```
8+
输入: [-2,1,-3,4,-1,2,1,-5,4],
9+
输出: 6
10+
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
11+
```
12+
13+
进阶:
14+
15+
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
16+
17+
### 解法
18+
此题可以用动态规划法,开辟一个数组res,res[i] 表示以当前结点nums[i] 结尾的最大连续子数组的和。最后计算 res 的最大元素即可。
19+
也可以用分治法,最大连续子数组有三种情况:在原数组左侧、右侧、跨中间结点,返回这三者的最大值即可。
20+
21+
22+
动态规划法:
23+
24+
```java
25+
class Solution {
26+
public int maxSubArray(int[] nums) {
27+
int n = nums.length;
28+
if (n == 1) {
29+
return nums[0];
30+
}
31+
int[] res = new int[n];
32+
res[0] = nums[0];
33+
int max = res[0];
34+
for (int i = 1; i < n; ++i) {
35+
res[i] = Math.max(res[i - 1] + nums[i], nums[i]);
36+
max = Math.max(res[i], max);
37+
}
38+
39+
return max;
40+
41+
}
42+
}
43+
```
44+
45+
分治法:
46+
47+
```java
48+
class Solution {
49+
public int maxSubArray(int[] nums) {
50+
return maxSubArray(nums, 0, nums.length - 1);
51+
}
52+
53+
private int maxSubArray(int[] nums, int start, int end) {
54+
if (start == end) {
55+
return nums[start];
56+
}
57+
int mid = start + ((end - start) >> 1);
58+
int left = maxSubArray(nums, start, mid);
59+
int right = maxSubArray(nums, mid + 1, end);
60+
61+
int leftSum = 0;
62+
int leftMax = Integer.MIN_VALUE;
63+
for (int i = mid; i >= start; --i) {
64+
leftSum += nums[i];
65+
leftMax = Math.max(leftSum, leftMax);
66+
}
67+
68+
int rightSum = 0;
69+
int rightMax = Integer.MIN_VALUE;
70+
for (int i = mid + 1; i <= end; ++i) {
71+
rightSum += nums[i];
72+
rightMax = Math.max(rightSum, rightMax);
73+
}
74+
75+
return Math.max(Math.max(left, right), leftMax + rightMax);
76+
77+
78+
}
79+
}
80+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
int n = nums.length;
4+
if (n == 1) {
5+
return nums[0];
6+
}
7+
int[] res = new int[n];
8+
res[0] = nums[0];
9+
int max = res[0];
10+
for (int i = 1; i < n; ++i) {
11+
res[i] = Math.max(res[i - 1] + nums[i], nums[i]);
12+
max = Math.max(res[i], max);
13+
}
14+
15+
return max;
16+
17+
}
18+
}

0 commit comments

Comments
 (0)