Skip to content

Commit 80d9991

Browse files
committed
Add solution for Maximum Subarray problem
1 parent f011d0f commit 80d9991

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

maximum-subarray/gcount85.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
# Intuition
3+
max(전 위치까지의 sum + 지금 위치 값, 지금 위치의 값)
4+
5+
# Complexity
6+
- Time complexity: nums의 길이를 N이라고 할 때 O(N)
7+
8+
- Space complexity: O(1)
9+
"""
10+
11+
12+
class Solution:
13+
def maxSubArray(self, nums: List[int]) -> int:
14+
a = nums[0]
15+
best = a
16+
for i in range(1, len(nums)):
17+
a = max(a + nums[i], nums[i])
18+
best = max(a, best)
19+
return best

0 commit comments

Comments
 (0)