152. 乘积最大子数组
为保证权益,题目请参考 152. 乘积最大子数组(From LeetCode).
解决方案1
CPP
C++
/*
* LeetCode 152. 乘积最大子数组
*
* Author: Keven Ge
* Date: 2020-05-15
*/
#include <iostream>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <climits>
#include <vector>
#include <algorithm>
using namespace std;
//class Solution {
//public:
// int maxProduct(vector<int> &nums) {
// int fmin = nums[0];
// int fmax = nums[0];
// int ans = nums[0];
//
// for (int i = 1; i < nums.size(); ++i) {
// fmax = max(nums[i], max(fmax * nums[i], fmin * nums[i]));
// fmin = min(nums[i], min(fmax * nums[i], fmin * nums[i]));
// ans = max(ans, fmax);
// }
// return ans;
// }
//};
class Solution {
public:
int maxProduct(vector<int> &nums) {
int maxF = nums[0], minF = nums[0], ans = nums[0];
for (int i = 1; i < nums.size(); ++i) {
int mx = maxF, mn = minF;
maxF = max(mx * nums[i], max(nums[i], mn * nums[i]));
minF = min(mn * nums[i], min(nums[i], mx * nums[i]));
ans = max(maxF, ans);
}
return ans;
}
};
int main() {
return 0;
}
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
42
43
44
45
46
47
48
49
50
51
52
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
42
43
44
45
46
47
48
49
50
51
52