forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum_Subarray.py
41 lines (35 loc) · 1.25 KB
/
Maximum_Subarray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
"""
class Solution:
# @param A, a list of integers
# @return an integer
def maxSubArray(self, A):
return self.maxSubArray_2(A)
def maxSubArray_1(self, A):
max_sum = A[0]
cur_sum = 0
for num in A:
cur_sum += num
max_sum = max(max_sum, cur_sum)
if cur_sum < 0:
cur_sum = 0
return max_sum
def maxSubArray_2(self, A):
res = A[0]
dp = A[0]
for num in A[1:]:
dp = max(num, dp+num)
res = max(res, dp)
return res
# Note
# 1. dp[i] means maximum subarray ends with A[i]
# 2. dp[0] = A[0]
# 3. dp[i] = max(A[i], A[i] + dp[i-1]) 意思就是如果end with A[i-1]的dp是负的话我们就不取,otherwise就取
# 4. dp[N-1]
# Because we don't need to store dp[i], so simplify to dp