Skip to content

Commit 0ce6c82

Browse files
committed
maximum-subarray
1 parent b7232f6 commit 0ce6c82

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

maximum-subarray/changhyumm.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
max_total = nums[0]
4+
total = 0
5+
# subarray는 array에서 서로 인접해야함
6+
# 인접한 값의 합이 마이너스인 경우, 그냥 현재 값만 사용하는게 합보다 큼
7+
# total 이 0보다 작은경우 그냥 0으로 변경
8+
for num in nums:
9+
if total < 0:
10+
total = 0
11+
total += num
12+
max_total = max(total, max_total)
13+
# 시간복잡도 O(n), 공간복잡도 O(1)
14+
return max_total

0 commit comments

Comments
 (0)