Skip to content

Commit 4246af3

Browse files
committed
solve 4
1 parent 71a8d1b commit 4246af3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

β€Žmaximum-product-subarray/pmjuu.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
μ‹œκ°„ λ³΅μž‘λ„: O(n)
3+
- 리슀트λ₯Ό ν•œ 번 μˆœνšŒν•˜λ©΄μ„œ 각 μš”μ†Œμ— λŒ€ν•΄ μ΅œλŒ€κ°’κ³Ό μ΅œμ†Œκ°’μ„ κ°±μ‹ ν•˜λ―€λ‘œ O(n)μž…λ‹ˆλ‹€.
4+
5+
곡간 λ³΅μž‘λ„: O(1)
6+
- 좔가적인 배열을 μ‚¬μš©ν•˜μ§€ μ•Šκ³ , λͺ‡ 개의 λ³€μˆ˜λ§Œ μ‚¬μš©ν•˜λ―€λ‘œ O(1)μž…λ‹ˆλ‹€.
7+
'''
8+
9+
from typing import List
10+
11+
class Solution:
12+
def maxProduct(self, nums: List[int]) -> int:
13+
n = len(nums)
14+
max_product = nums[0]
15+
cur_max = nums[0] # ν˜„μž¬ μœ„μΉ˜κΉŒμ§€ μ΅œλŒ€ κ³±
16+
cur_min = nums[0] # ν˜„μž¬ μœ„μΉ˜κΉŒμ§€ μ΅œμ†Œ κ³± (음수 λŒ€λΉ„)
17+
18+
for i in range(1, n):
19+
temp_max = cur_max
20+
cur_max = max(nums[i], cur_max * nums[i], cur_min * nums[i])
21+
cur_min = min(nums[i], temp_max * nums[i], cur_min * nums[i])
22+
max_product = max(max_product, cur_max)
23+
24+
return max_product

0 commit comments

Comments
Β (0)