-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53_Maximum_Subarray.py
46 lines (42 loc) · 1.42 KB
/
53_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
42
43
44
45
46
'''
@Author: wangluyu
@Date: 2020-01-16 15:05:43
@LastEditors : wangluyu
@LastEditTime : 2020-01-19 15:06:27
'''
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Desc: 53. 最大子序和 (简单)
Author: wangluyu
Date: 2020/1/14
"""
from typing import List
class Solution:
"""
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
"""
def maxSubArray(self, nums: List[int]) -> int:
max_sum = nums[0]
curr_sum = 0
for i in range(len(nums)):
curr_sum = max(nums[i], curr_sum + nums[i])
max_sum = max(max_sum, curr_sum)
return max_sum
if __name__ == '__main__':
nums = [-2,1,-3,4,-1,2,1,-5,4]
s=Solution()
print(s.maxSubArray(nums))