Skip to content

Commit b3b7dac

Browse files
authored
Create Maximum Subarray - Leetcode 53.py
1 parent 34b166d commit b3b7dac

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Maximum Subarray - Leetcode 53.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
# Bottom Up DP (Constant Space)
4+
# Time: O(n)
5+
# Space: O(1)
6+
max_sum = float('-inf')
7+
curr_sum = 0
8+
9+
for i in range(len(nums)):
10+
curr_sum += nums[i]
11+
max_sum = max(max_sum, curr_sum)
12+
13+
if curr_sum < 0:
14+
curr_sum = 0
15+
16+
return max_sum

0 commit comments

Comments
 (0)