Skip to content

Commit 0c25205

Browse files
committed
Edit explanation for maximum subarray problem
1 parent 22e4e05 commit 0c25205

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
- The product of any subarray of nums is guaranteed to fit in a 32-bit integer.
66
77
Time Complexity: O(n)
8-
- 배열을 ν•œ 번만 μˆœνšŒν•˜λ©΄μ„œ 각 μœ„μΉ˜μ—μ„œ μƒμˆ˜ μ‹œκ°„ μ—°μ‚°λ§Œ μˆ˜ν–‰
8+
- 배열을 ν•œ 번만 μˆœνšŒν•˜κ³ , 각 μœ„μΉ˜μ—μ„œ μƒμˆ˜ μ‹œκ°„ μ—°μ‚°(max, min, κ³±μ…ˆ)만 μˆ˜ν–‰
99
1010
Space Complexity: O(1)
11-
- κ³ μ •λœ μΆ”κ°€ λ³€μˆ˜λ§Œ μ‚¬μš© (curr_max, curr_min, ...)
11+
- μž…λ ₯ 크기와 λ¬΄κ΄€ν•˜κ²Œ κ³ μ •λœ λ³€μˆ˜(curr_max, curr_min, result, temp)만 μ‚¬μš©
1212
1313
풀이방법:
14-
1. DP둜 각 μœ„μΉ˜μ—μ„œ κ°€λŠ₯ν•œ μ΅œλŒ€κ³±κ³Ό μ΅œμ†Œκ³±μ„ λ™μ‹œμ— 좔적함
15-
2. 각 μœ„μΉ˜μ—μ„œ μ„Έ 개의 선택지 쑴재: μƒˆλ‘œ μ‹œμž‘ vs 이전 μ΅œλŒ€κ³±κ³Ό κ³±ν•˜κΈ° vs 이전 μ΅œμ†Œκ³±κ³Ό κ³±ν•˜κΈ°
16-
3. μ΅œμ†Œκ³±μ΄ ν•„μš”ν•œ 이유: λ‚˜μ€‘μ— 음수λ₯Ό λ§Œλ‚¬μ„ λ•Œ μ΅œλŒ€κ°’μ΄ 될 수 있기 λ•Œλ¬Έ
14+
1. DP둜 각 μœ„μΉ˜μ—μ„œ κ°€λŠ₯ν•œ μ΅œλŒ€κ°’κ³Ό μ΅œμ†Œκ°’μ„ ν•¨κ»˜ μ—…λ°μ΄νŠΈν•¨
15+
2. 각 μœ„μΉ˜μ—μ„œ μ„Έ 개의 선택지 쑴재: μƒˆλ‘œ μ‹œμž‘ vs 이전 μ΅œλŒ€κ°’κ³Ό κ³±ν•˜κΈ° vs 이전 μ΅œμ†Œκ°’κ³Ό κ³±ν•˜κΈ°
16+
3. μ΅œμ†Œκ°’μ„ 계속 μ—…λ°μ΄νŠΈ ν•˜λŠ” 이유: 음수*음수 = μƒˆλ‘œμš΄ μ΅œλŒ€κ°’, curr_max μ—…λ°μ΄νŠΈν•  λ•Œ μ“°μž„ (μ˜ˆμ‹œ: nums = [2, 3, -2, 4, -1])
1717
4. λ§€ λ‹¨κ³„λ§ˆλ‹€ result μ—…λ°μ΄νŠΈ
1818
1919
고렀사항:
20-
1. 값이 0인 경우 β†’ μƒˆλ‘œ μ‹œμž‘ν•΄μ•Ό 함
21-
2. temp λ³€μˆ˜: curr_maxλ₯Ό μ—…λ°μ΄νŠΈν•˜λ©΄ curr_min 계산 μ‹œ μ›λž˜ 값이 ν•„μš”ν•¨
20+
1. 값이 0인 경우 μƒˆλ‘œ μ‹œμž‘ν•΄μ•Ό 함
21+
2. temp λ³€μˆ˜: curr_min 계산 μ‹œ curr_maxλ₯Ό μ—…λ°μ΄νŠΈν•˜κΈ° μ „μ˜ 값이 ν•„μš”ν•¨
2222
"""
2323
class Solution:
2424
def maxProduct(self, nums: List[int]) -> int:
@@ -33,4 +33,3 @@ def maxProduct(self, nums: List[int]) -> int:
3333
result = max(result, curr_max)
3434

3535
return result
36-

0 commit comments

Comments
Β (0)