53. Maximum Subarray
程序员文章站
2022-07-15 19:48:32
...
https://leetcode.com/problems/maximum-subarray/description/
题意:求最大连续子段和
思路:DP
摘自《计算机算法设计与分析》王晓东 第3版
class Solution:
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
sum = nums[0] #初始累加和
b = nums[0] #dp初始状态,0号
for i in range(1, len(nums)): #从1号开始
if b > 0:
b += nums[i]
else:
b = nums[i]
sum = max(b, sum)
return sum
推荐阅读
-
oracle 导入报错:field in data file exceeds maximum length
-
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
-
【学习笔记】RMQ-Range Minimum/Maximum Query (区间最小/最大值)
-
lintcode-741. Calculate Maximum Value II题解
-
自适应布局meta标签中viewport、content、width、initial-scale、minimum-scale、maximum-scale总结
-
[cf] 1401 D.Maximum Distributed Tree
-
Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree(dfs)
-
LeetCode------Maximum SubArray
-
leetcode【53】Maximum Subarray
-
53. Maximum Subarray