Skip to content

Commit 065acb2

Browse files
committed
maximum subarray 로직 수정 : 공간 최적화
1 parent 3bdc80f commit 065acb2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

maximum-subarray/devyejin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution:
22
def maxSubArray(self, nums: list[int]) -> int:
3-
dp = [0] * len(nums)
4-
dp[0] = nums[0]
3+
current_sum = nums[0]
4+
max_sum = nums[0]
55

66
for i in range(1, len(nums)):
7-
dp[i] = max(nums[i], dp[i - 1] + nums[i])
7+
current_sum = max(nums[i], current_sum + nums[i])
8+
max_sum = max(current_sum, max_sum)
89

9-
return max(dp)
10+
return max_sum
1011

0 commit comments

Comments
 (0)