forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.cpp
More file actions
32 lines (27 loc) ยท 835 Bytes
/
obzva.cpp
File metadata and controls
32 lines (27 loc) ยท 835 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
30
31
32
/**
* ํ์ด
* - ์ฃผ์ด์ง ๋ฐฐ์ด `nums`๋ฅผ ์์๋๋ก ์กฐํํฉ๋๋ค
* - 0๊ณผ ์์๋ฅผ ๊ณฑํ๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ๊ธฐ ์ํด ํ์ฌ subarray์ ๊ณฑ์ ์ต๋๊ฐ๋ฟ๋ง ์๋๋ผ ์ต์๊ฐ ๋ํ ๊ธฐ๋กํฉ๋๋ค
*
* Big-O
* - N: ์ฃผ์ด์ง ๋ฐฐ์ด `nums`์ size
*
* - Time complexity: O(N)
* - Space complexity: O(1)
*/
class Solution {
public:
int maxProduct(vector<int>& nums) {
int max_prod = nums[0];
int min_prod = nums[0];
int res = nums[0];
for (int i = 1; i < nums.size(); i++) {
int curr = nums[i];
int tmp_max = max(curr, max(curr * max_prod, curr * min_prod));
min_prod = min(curr, min(curr * max_prod, curr * min_prod));
max_prod = tmp_max;
res = max(res, max_prod);
}
return res;
}
};