File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /* *
2+ * ํ์ด
3+ * - ์ฃผ์ด์ง ๋ฐฐ์ด `nums`๋ฅผ ์์๋๋ก ์กฐํํฉ๋๋ค
4+ * - 0๊ณผ ์์๋ฅผ ๊ณฑํ๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ๊ธฐ ์ํด ํ์ฌ subarray์ ๊ณฑ์ ์ต๋๊ฐ๋ฟ๋ง ์๋๋ผ ์ต์๊ฐ ๋ํ ๊ธฐ๋กํฉ๋๋ค
5+ *
6+ * Big-O
7+ * - N: ์ฃผ์ด์ง ๋ฐฐ์ด `nums`์ size
8+ *
9+ * - Time complexity: O(N)
10+ * - Space complexity: O(1)
11+ */
12+
13+ class Solution {
14+ public:
15+ int maxProduct (vector<int >& nums) {
16+ int max_prod = nums[0 ];
17+ int min_prod = nums[0 ];
18+ int res = nums[0 ];
19+
20+ for (int i = 1 ; i < nums.size (); i++) {
21+ int curr = nums[i];
22+
23+ int tmp_max = max (curr, max (curr * max_prod, curr * min_prod));
24+ min_prod = min (curr, min (curr * max_prod, curr * min_prod));
25+ max_prod = tmp_max;
26+
27+ res = max (res, max_prod);
28+ }
29+
30+ return res;
31+ }
32+ };
You canโt perform that action at this time.
0 commit comments