-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution152.java
More file actions
32 lines (28 loc) · 992 Bytes
/
Solution152.java
File metadata and controls
32 lines (28 loc) · 992 Bytes
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 乘积最大子序列
* @date: 2019/02/17
*/
public class Solution152 {
public int maxProduct(int[] nums) {
if (null == nums || 0 >= nums.length) {
return 0;
}
int ans = nums[0];
for (int i = 1, mx = nums[0], mn = nums[0]; i < nums.length; ++i) {
// 以当前位置结尾,乘积最大的连续子序列有以下几种情况:
// 1. 当前元素为正数,从前一状态挑选最大值转移过来
// 2. 当前元素为负数,从前一状态挑选最小值转移过来(负负得正)
// 3. 前一状态为0, 从前一状态或当前元素选择最大值
int a = mx * nums[i];
int b = mn * nums[i];
mx = Math.max(Math.max(a, b), nums[i]);
mn = Math.min(Math.min(a, b), nums[i]);
if (mx > ans) {
ans = mx;
}
}
return ans;
}
}