Skip to content

Commit 2383b89

Browse files
committed
Sync LeetCode submission - Maximum Subarray (python3)
1 parent fc81daf commit 2383b89

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

problems/maximum_subarray/solution.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class Solution:
22
def maxSubArray(self, nums: List[int]) -> int:
3-
curr_sum = max_sum = nums[0]
3+
# start from nums[1...n], n = length of array
44

55
for i in range(1,len(nums)):
6-
curr_sum = max(nums[i],curr_sum + nums[i])
7-
max_sum = max(max_sum, curr_sum)
8-
return max_sum
6+
7+
if nums[i-1] > 0: # prev value > 0, then add
8+
9+
nums[i] += nums[i-1]
10+
return max(nums)

0 commit comments

Comments
 (0)