-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_152.cpp
More file actions
91 lines (86 loc) · 1.79 KB
/
LC_152.cpp
File metadata and controls
91 lines (86 loc) · 1.79 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <functional>
#include <cstdlib>
#include <list>
#include <iomanip>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define ld long double
#define MOD 1000000007
#define INF LLONG_MAX
#define INF_MIN LLONG_MIN
#define MAX 200000
using namespace std;
class Solution {
public:
int maxProduct(vector<int>& nums) {
int firstNegativeIdx = 0;
int lastNegativeIdx = 0;
int negativeCnt = 0;
int currCnt = 0;
ll curr = 1;
ll ans = LLONG_MIN;
for (int i = 0; i < nums.size(); i++) {
ans = max(ans, (ll)nums[i]);
}
int startIdx = 0;
int endIdx = 0;
nums.push_back(0);
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 0) {
if (negativeCnt % 2 == 0) {
if (currCnt == 0) curr = 0;
ans = max(ans, curr);
}
else {
ll tmpCurr = curr;
if (currCnt > 1) {
for (int j = lastNegativeIdx; j <= endIdx; j++) {
tmpCurr /= nums[j];
}
ans = max(ans, tmpCurr);
}
if (currCnt > 1) {
tmpCurr = curr;
for (int j = startIdx; j <= firstNegativeIdx; j++) {
tmpCurr /= nums[j];
}
ans = max(ans, tmpCurr);
}
}
curr = 1;
currCnt = 0;
negativeCnt = 0;
startIdx = i + 1;
endIdx = i + 1;
}
else {
if (nums[i] < 0) {
if (negativeCnt == 0) {
firstNegativeIdx = i;
}
lastNegativeIdx = i;
negativeCnt++;
}
curr *= nums[i];
endIdx = i;
currCnt++;
}
}
return ans;
}
};
int main() {
Solution sol;
vector<int> v = { 2, 0, 3, -2 };
cout << sol.maxProduct(v);
}