Skip to content

Commit 57ee9b4

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0053
1 parent 0bbf5ab commit 57ee9b4

File tree

6 files changed

+54
-32
lines changed

6 files changed

+54
-32
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131

132132
### 动态规划
133133

134+
- [最大子序和](/solution/0000-0099/0053.Maximum%20Subarray/README.md)
134135
- [打家劫舍](/solution/0100-0199/0198.House%20Robber/README.md)
135136
- [打家劫舍 II](/solution/0200-0299/0213.House%20Robber%20II/README.md)
136137
- [最长上升子序列](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
120120

121121
### Dynamic Programming
122122

123+
- [Maximum Subarray](/solution/0000-0099/0053.Maximum%20Subarray/README_EN.md)
123124
- [House Robber](/solution/0100-0199/0198.House%20Robber/README_EN.md)
124125
- [House Robber II](/solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
125126
- [Longest Increasing Subsequence](/solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)

solution/0000-0099/0053.Maximum Subarray/README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,42 @@
2222

2323
<!-- 这里可写通用的实现逻辑 -->
2424

25+
设 dp[i] 表示 `[0..i]` 中,以 nums[i] 结尾的最大子数组和,状态转移方程 `dp[i] = nums[i] + max(dp[i - 1], 0)`
26+
27+
由于 `dp[i]` 只与子问题 `dp[i-1]` 有关,故可以用一个变量 f 来表示。
28+
2529
<!-- tabs:start -->
2630

2731
### **Python3**
2832

2933
<!-- 这里可写当前语言的特殊实现逻辑 -->
3034

3135
```python
32-
36+
class Solution:
37+
def maxSubArray(self, nums: List[int]) -> int:
38+
n = len(nums)
39+
res = f = nums[0]
40+
for i in range(1, n):
41+
f = nums[i] + max(f, 0)
42+
res = max(res, f)
43+
return res
3344
```
3445

3546
### **Java**
3647

3748
<!-- 这里可写当前语言的特殊实现逻辑 -->
3849

3950
```java
40-
51+
class Solution {
52+
public int maxSubArray(int[] nums) {
53+
int f = nums[0], res = nums[0];
54+
for (int i = 1, n = nums.length; i < n; ++i) {
55+
f = nums[i] + Math.max(f, 0);
56+
res = Math.max(res, f);
57+
}
58+
return res;
59+
}
60+
}
4161
```
4262

4363
### **...**

solution/0000-0099/0053.Maximum Subarray/README_EN.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@
2929
### **Python3**
3030

3131
```python
32-
32+
class Solution:
33+
def maxSubArray(self, nums: List[int]) -> int:
34+
n = len(nums)
35+
res = f = nums[0]
36+
for i in range(1, n):
37+
f = nums[i] + max(f, 0)
38+
res = max(res, f)
39+
return res
3340
```
3441

3542
### **Java**
3643

3744
```java
38-
45+
class Solution {
46+
public int maxSubArray(int[] nums) {
47+
int f = nums[0], res = nums[0];
48+
for (int i = 1, n = nums.length; i < n; ++i) {
49+
f = nums[i] + Math.max(f, 0);
50+
res = Math.max(res, f);
51+
}
52+
return res;
53+
}
54+
}
3955
```
4056

4157
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
class Solution {
22
public int maxSubArray(int[] nums) {
3-
int n = nums.length;
4-
if (n == 1) {
5-
return nums[0];
3+
int f = nums[0], res = nums[0];
4+
for (int i = 1, n = nums.length; i < n; ++i) {
5+
f = nums[i] + Math.max(f, 0);
6+
res = Math.max(res, f);
67
}
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-
8+
return res;
179
}
1810
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
class Solution:
2-
def maxSubArray(self, nums):
3-
"""
4-
:type nums: List[int]
5-
:rtype: int
6-
"""
7-
n=len(nums)
8-
if n == 1:
9-
return nums[0]
10-
res=[0]*n
11-
res[0]=nums[0]
12-
max0=nums[0]
13-
for i in range(1,n):
14-
res[i]=max(res[i-1]+nums[i],nums[i])
15-
max0=max(res[i],max0)
16-
return max0
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
res = f = nums[0]
5+
for i in range(1, n):
6+
f = nums[i] + max(f, 0)
7+
res = max(res, f)
8+
return res

0 commit comments

Comments
 (0)