We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b0b8657 commit 2e26787Copy full SHA for 2e26787
โmaximum-product-subarray/yyyyyyyyyKim.py
@@ -0,0 +1,19 @@
1
+class Solution:
2
+ def maxProduct(self, nums: List[int]) -> int:
3
+
4
+ # ์ฐ์๋ ๋ถ๋ถ ๋ฐฐ์ด์ ๊ณฑ์ด ๊ฐ์ฅ ํฐ ๊ฐ ์ฐพ๊ธฐ(์ ์)
5
+ # DP (์๊ฐ๋ณต์ก๋ O(n), ๊ณต๊ฐ๋ณต์ก๋ O(1))
6
+ answer = nums[0]
7
+ prev_max = prev_min = nums[0]
8
9
+ for i in range(1,len(nums)):
10
+ n = nums[i]
11
12
+ # ์์*์์๋ ์์์ด๋ฏ๋ก max,min ๋๋ค ๊ณ์ฐ
13
+ curr_max = max(n,prev_max*n, prev_min*n)
14
+ curr_min = min(n,prev_max*n, prev_min*n)
15
16
+ answer = max(answer, curr_max) # ์ต๋ ๊ณฑ ์ ๋ฐ์ดํธ
17
+ prev_max, prev_min = curr_max, curr_min # ํ์ฌ๊ฐ์ ์ด์ ๊ฐ์ผ๋ก ์ ๋ฐ์ดํธ
18
19
+ return answer
0 commit comments