-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
41 lines (36 loc) · 1.12 KB
/
Solution.java
File metadata and controls
41 lines (36 loc) · 1.12 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
package com.q0053;
import java.util.stream.IntStream;
/**
* @author xjn
* @since 2020-05-31
* https://leetcode-cn.com/problems/maximum-subarray/
* 53. 最大子序和
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
public class Solution {
public int maxSubArray(int[] nums) {
//dp[i]:以nums[i]为结尾的最大子数组的和
//1.相连 dp[i] = dp[i-1] + nums[i]
//2. 不相连 dp[i] = nums[i]
if (nums == null) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
int[] dp = new int[nums.length];
for (int i = 0; i < dp.length; i++) {
dp[i] = nums[i];
}
for (int i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i], dp[i - 1] + nums[i]);
}
return IntStream.of(dp).boxed().max(Integer::compareTo).get();
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
System.out.println(solution.maxSubArray(new int[]{-1, -2}));
}
}