File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ # 152. Maximum Product Subarray
3
+
4
+ solution reference: https://www.algodale.com/problems/maximum-product-subarray/
5
+
6
+ ## ์ต๋ ๊ณฑ ๋ฐฐ์ด ๊ตฌํ๊ธฐ
7
+ - ์ฐ์ ๋ฐฐ์ด(subarray)์ ์์, ์์, 0์ด ํฌํจ๋ ์ ์๋ค.
8
+ - ์์๊ฐ ๊ฒฐ๊ณผ์ ์ํฅ์ ๋ฏธ์น ์ ์๊ธฐ ๋๋ฌธ์ ์ต์๊ฐ/์ต๋๊ฐ ์ถ์ ์ด ํ์ํ๋ค.
9
+
10
+ ## ๊ฐ
11
+ - result: ์ต์ข
์ ์ผ๋ก ๋ฐํํ ๊ฐ
12
+ - min_prod: ํ์ฌ๊น์ง์ ์ต์ ๊ณฑ ๊ฐ (์์๋ฅผ ๊ณ ๋ คํ ์ถ์ )
13
+ - max_prod: ํ์ฌ๊น์ง์ ์ต๋ ๊ณฑ ๊ฐ
14
+
15
+ ## ์๋ก์ด ๊ฐ num์ด ์ฃผ์ด์ก์ ๋
16
+ - ์๋ก์ด ๋ฐฐ์ด์ ์์ํ ์ง, ๊ธฐ์กด ๋ฐฐ์ด์ ์ถ๊ฐํ ์ง ๊ฒฐ์
17
+ - ํ๋ณด๋ค๋ก ์ต๋๊ฐ์ ๊ฐ๋ฅ์ฑ์ ํ์ธํ๊ณ result๋ฅผ ์
๋ฐ์ดํธํ๋ค.
18
+ '''
19
+ class Solution :
20
+ def maxProduct (self , nums : List [int ]) -> int :
21
+ result = nums [0 ]
22
+ min_prod = 1
23
+ max_prod = 1
24
+
25
+ for num in nums :
26
+ candidates = (min_prod * num , max_prod * num , num )
27
+ min_prod = min (candidates )
28
+ max_prod = max (candidates )
29
+ result = max (max_prod , result )
30
+
31
+ return result
You canโt perform that action at this time.
0 commit comments