-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path628.cpp
More file actions
25 lines (25 loc) · 773 Bytes
/
Copy path628.cpp
File metadata and controls
25 lines (25 loc) · 773 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
class Solution {
public:
int maximumProduct(vector<int> &nums) {
int n = nums.size();
int min1 = 0, min2 = 0, max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;
for (int i = 0; i < n; ++i) {
if (nums[i] < min1) {
min2 = min1;
min1 = nums[i];
} else if (nums[i] < min2) {
min2 = nums[i];
}
if (nums[i] > max1) {
max3 = max2;
max2 = max1;
max1 = nums[i];
} else if (nums[i] > max2) {
max3 = max2;
max2 = nums[i];
} else if (nums[i] > max3)
max3 = nums[i];
}
return max(min1 * min2 * max1, max1 * max2 * max3);
}
};