forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0152.cpp
35 lines (35 loc) · 840 Bytes
/
0152.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int maxProduct(vector<int>& nums)
{
auto numsB(nums);
reverse(numsB.begin(), numsB.end());
for (unsigned int i = 1; i < nums.size(); ++i)
{
if (nums[i-1]) nums[i] *= nums[i-1];
if (numsB[i-1]) numsB[i] *= numsB[i-1];
}
int result = INT_MIN;
for (auto num : numsB)
{
if (result < num) result = num;
}
for (auto num : nums)
{
if (result < num) result = num;
}
return result;
}
};
int main()
{
vector<int> nums = {0, -2};
cout << Solution().maxProduct(nums) << endl;
return 0;
}