Skip to content

Commit 2c52d47

Browse files
committed
53. Maximum Subarray
1 parent 3edbc89 commit 2c52d47

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ out/
2424
target/
2525
.idea/
2626
.DS_Store
27+
.classpath
28+
.project
29+
.settings/
2730

2831
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2932
hs_err_pid*

markdown/53. Maximum Subarray.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
### [53\. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
7+
8+
**Example:**
9+
10+
```
11+
Input: [-2,1,-3,4,-1,2,1,-5,4],
12+
Output: 6
13+
Explanation: [4,-1,2,1] has the largest sum = 6.
14+
```
15+
16+
**Follow up:**
17+
18+
If you have figured out the O(_n_) solution, try coding another solution using the divide and conquer approach, which is more subtle.
19+
20+
21+
#### Solution
22+
23+
Language: **Java** 参考
24+
25+
```java
26+
class Solution {
27+
   public int maxSubArray(int[] nums) {
28+
       int[] dp = new int[nums.length]; // 表示以 index 为结尾的,最大子序列和的值
29+
       dp[0] = nums[0];
30+
       int maxSum = dp[0];
31+
       for (int i = 1; i < nums.length; i++) {
32+
           dp[i] = dp[i - 1] > 0 ? dp[i - 1] + nums[i] : nums[i];
33+
           maxSum = dp[i] > maxSum ? dp[i] : maxSum;
34+
      }
35+
       return maxSum;
36+
  }
37+
}
38+
```

src/main/java/leetcode/_51_/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode._51_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
10+
int i = solution.maxSubArray(nums);
11+
System.out.println(i);
12+
}
13+
}
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode._51_;
2+
3+
class Solution {
4+
public int maxSubArray(int[] nums) {
5+
int[] dp = new int[nums.length]; // 表示以 index 为结尾的,最大子序列和的值
6+
dp[0] = nums[0];
7+
int maxSum = dp[0];
8+
for (int i = 1; i < nums.length; i++) {
9+
dp[i] = dp[i - 1] > 0 ? dp[i - 1] + nums[i] : nums[i];
10+
maxSum = dp[i] > maxSum ? dp[i] : maxSum;
11+
}
12+
return maxSum;
13+
}
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
### [53\. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
7+
8+
**Example:**
9+
10+
```
11+
Input: [-2,1,-3,4,-1,2,1,-5,4],
12+
Output: 6
13+
Explanation: [4,-1,2,1] has the largest sum = 6.
14+
```
15+
16+
**Follow up:**
17+
18+
If you have figured out the O(_n_) solution, try coding another solution using the divide and conquer approach, which is more subtle.
19+
20+
21+
#### Solution
22+
23+
Language: **Java** 参考
24+
25+
```java
26+
class Solution {
27+
   public int maxSubArray(int[] nums) {
28+
       int[] dp = new int[nums.length]; // 表示以 index 为结尾的,最大子序列和的值
29+
       dp[0] = nums[0];
30+
       int maxSum = dp[0];
31+
       for (int i = 1; i < nums.length; i++) {
32+
           dp[i] = dp[i - 1] > 0 ? dp[i - 1] + nums[i] : nums[i];
33+
           maxSum = dp[i] > maxSum ? dp[i] : maxSum;
34+
      }
35+
       return maxSum;
36+
  }
37+
}
38+
```

0 commit comments

Comments
 (0)