File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Constraints:
3+ - 1 <= nums.length <= 2 * 10^4
4+ - -10 <= nums[i] <= 10
5+ - The product of any subarray of nums is guaranteed to fit in a 32-bit integer.
6+
7+ Time Complexity:
8+ - O(n): ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๋ฉด์ ๊ฐ ์์น์์ ์์ ์๊ฐ ์ฐ์ฐ๋ง ์ํ
9+
10+ Space Complexity:
11+ - O(1): ๊ณ ์ ๋ ์ถ๊ฐ ๋ณ์๋ง ์ฌ์ฉ (curr_max, curr_min, ...)
12+
13+ ํ์ด๋ฐฉ๋ฒ:
14+ 1. DP๋ก ๊ฐ ์์น์์ ๊ฐ๋ฅํ ์ต๋๊ณฑ๊ณผ ์ต์๊ณฑ์ ๋์์ ์ถ์ ํจ
15+ 2. ๊ฐ ์์น์์ ์ธ ๊ฐ์ ์ ํ์ง ์กด์ฌ: ์๋ก ์์ vs ์ด์ ์ต๋๊ณฑ๊ณผ ๊ณฑํ๊ธฐ vs ์ด์ ์ต์๊ณฑ๊ณผ ๊ณฑํ๊ธฐ
16+ 3. ์ต์๊ณฑ์ด ํ์ํ ์ด์ : ๋์ค์ ์์๋ฅผ ๋ง๋ฌ์ ๋ ์ต๋๊ฐ์ด ๋ ์ ์๊ธฐ ๋๋ฌธ
17+ 4. ๋งค ๋จ๊ณ๋ง๋ค result ์
๋ฐ์ดํธ
18+
19+ ๊ณ ๋ ค์ฌํญ:
20+ 1. ๊ฐ์ด 0์ธ ๊ฒฝ์ฐ โ ์๋ก ์์ํด์ผ ํจ
21+ 2. temp ๋ณ์: curr_max๋ฅผ ์
๋ฐ์ดํธํ๋ฉด curr_min ๊ณ์ฐ ์ ์๋ ๊ฐ์ด ํ์ํจ
22+ """
23+ class Solution :
24+ def maxProduct (self , nums : List [int ]) -> int :
25+ curr_max = nums [0 ]
26+ curr_min = nums [0 ]
27+ result = nums [0 ]
28+
29+ for i in range (1 , len (nums )):
30+ temp = curr_max
31+ curr_max = max (nums [i ], curr_max * nums [i ], curr_min * nums [i ])
32+ curr_min = min (nums [i ], temp * nums [i ], curr_min * nums [i ])
33+ result = max (result , curr_max )
34+
35+ return result
36+
37+
Original file line number Diff line number Diff line change 1+ """
2+ Constraints:
3+ -
4+
5+ Time Complexity:
6+ -
7+
8+ Space Complexity:
9+ -
10+
11+ ํ์ด๋ฐฉ๋ฒ:
12+ 1.
13+ """
14+
You canโt perform that action at this time.
0 commit comments