-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path152.cpp
More file actions
29 lines (26 loc) · 725 Bytes
/
152.cpp
File metadata and controls
29 lines (26 loc) · 725 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
//
// 152.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/17.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxProduct(vector<int>& nums) {
if (nums.empty()) {
return INT_MIN;
}
int currentMax = nums[0], currentMin = nums[0], result = nums[0];
for (int i = 1; i < nums.size(); i++) {
int temp1 = currentMin * nums[i], temp2 = currentMax * nums[i];
currentMax = max(max(temp1, temp2), nums[i]);
currentMin = min(min(temp1, temp2), nums[i]);
result = max(currentMax, result);
}
return result;
}
};