Skip to content

Commit 0b0ad1b

Browse files
committed
feat: week9 - maximum product subarray
1 parent a075497 commit 0b0ad1b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
이전 곱을 저장해서 곱셈 연산을 줄이는 방식
3+
해당 연산 방식은 Time Limited Exceeded 오류를 발생했습니다.
4+
5+
시간 복잡도: O(n^2)
6+
- 외부 for-loop과 내부 for-loop이 각각 n번씩 실행되기 때문
7+
공간 복잡도: O(1)
8+
'''
9+
class Solution:
10+
def maxProduct(self, nums: List[int]) -> int:
11+
max_product = nums[0]
12+
13+
for i in range(len(nums)):
14+
prev = 1
15+
for j in range(i, len(nums)):
16+
prev = prev * nums[j]
17+
max_product = max(max_product, prev)
18+
19+
return max_product

0 commit comments

Comments
 (0)