Skip to content

Commit 4239abe

Browse files
committed
leetcode.com 152. Maximum Product Subarray
문제 링크: https://leetcode.com/problems/maximum-product-subarray/description/
1 parent 1125a7c commit 4239abe

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxProduct(self, nums: List[int]) -> int:
6+
mx = nums[0]
7+
mn_prev = nums[0]
8+
mx_prev = nums[0]
9+
10+
for v in nums[1:]:
11+
tmp_mn_prev = mn_prev
12+
tmp_mx_prev = mx_prev
13+
mn_prev = min(v, tmp_mn_prev * v, tmp_mx_prev * v)
14+
mx_prev = max(v, tmp_mn_prev * v, tmp_mx_prev * v)
15+
mx = max(mx, mx_prev)
16+
17+
return mx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
5+
class TestSolution(TestCase):
6+
def test1_max_product(self):
7+
sol = Solution()
8+
self.assertEqual(6, sol.maxProduct([2, 3, -2, 4]))
9+
10+
def test2_max_product(self):
11+
sol = Solution()
12+
self.assertEqual(0, sol.maxProduct([-2, 0, -1]))

0 commit comments

Comments
 (0)