Skip to content

Commit 40cb7ab

Browse files
committed
Maximum Subarray
1 parent 33fa586 commit 40cb7ab

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

053-maximum-subarray.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

53-maximum-subarray.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/maximum-subarray/description/
3+
4+
Given an integer array nums, find the contiguous subarray
5+
(containing at least one number) which has the largest sum and return its sum.
6+
7+
Example:
8+
Input: [-2,1,-3,4,-1,2,1,-5,4],
9+
Output: 6
10+
Explanation: [4,-1,2,1] has the largest sum = 6.
11+
"""
12+
class Solution:
13+
def maxSubArray(self, nums: List[int]) -> int:
14+
max_sum = cur_sum = float('-inf')
15+
16+
for num in nums:
17+
cur_sum = max(cur_sum + num, num)
18+
max_sum = max(max_sum, cur_sum)
19+
20+
return max_sum

0 commit comments

Comments
 (0)