【LeetCode每天一题】Maximum Subarray(最大子数组)
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.
思路
这道题看到之后第一想到的就是使用动态规划来解决这个问题。使用动态规划需要申请一个辅助数组,另外还需要动态方程,方程为dp[i] = nums[i] + ( dp[i-1] if dp[i-1] > 0 else 0)。 这种解法的时间复杂度为O(n),空间复杂度为O(n)。
第二种思路就是我们设置一个sum_标志量和结果变量,然后从头遍历,使用sum_变量存储连续数组的和,如果当前小于0直接赋值为0。最后返回结果变量。时间复杂度为O(n),空间复杂度为O(1)。
第一种思路代码
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) < 1 :
return 0
dp = [0] * len(nums) # 辅助数组
dp[0] = nums[0] # 记录nums第一个的值
max_num = dp[0] # 记录子数组最大的值
for i in range(1, len(nums)):
dp[i] = nums[i] + (dp[i-1] if dp[i-1]> 0 else 0) # 记录当前最大的子数组和的值
max_num = max(max_num, dp[i])
return max_num
第二种思路解决办法
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) < 1 :
return 0
res, sum_ = nums[0], 0
for i in nums:
sum_ += i
res = max(sum_, res)
if sum_ < 0:
sum_ = 0
return res
【LeetCode每天一题】Maximum Subarray(最大子数组)的更多相关文章
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [leetcode]53. Maximum Subarray最大子数组和
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- Maximum Subarray(最大子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【js】Leetcode每日一题-解码异或后数组
[js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...
- [LeetCode每日一题]153.寻找旋转排序数组中的最小值
[LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...
- [LeetCode每日一题]81. 搜索旋转排序数组 II
[LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...
随机推荐
- 每天学习一个命令:find 查找文件
查找的动作在平时使用的频率也还是很高的,所以知道并用好 find 这个命令也很重要.find 命令顾名思义,就是搜索特定文件夹内的文件. 基本使用 最基本的使用 find [path] [expres ...
- springmvc 拦截通配符 /** /
/** 拦截所有 包括 *.js *.css *.png 等等 / 只拦截 /login, /logout, /index等等
- Win10系统安装UEFI+GPT配置
借鉴博文 https://blog.csdn.net/lyj_viviani/article/details/51800445 安装win10需要改用UEFI引导模式,硬盘需要换成GUID格式; ...
- 2.静态AOP实现-装饰器模式
通过装饰器模式实现在RegUser()方法本身业务前后加上一些自己的功能,如:BeforeProceed和AfterProceed,即不修改UserProcessor类又能增加新功能 定义1个用户接口 ...
- LiDAR、LAS、LAS Dataset与点云
LiDAR Light Detection And Ranging,激光探测及测距,是一种光学遥感技术,使用激光对地球表面的密集采样,产生高精度X.Y.Z测量值. 激光雷达系统的主要硬件组成部分包括一 ...
- DB2隔离级别之RR/RS/CS/UR
1.RR隔离级别:在此隔离级别下. DB2会锁住全部相关的纪录. 在一个SQL语句运行期间, 全部运行此语句扫描过的纪录都会被加上对应的锁.在一个SQL语句运行期间,全部运行此语句扫描过的纪录都会 ...
- iOS开发之--UIImageView的animationImages动画
图片动画实现,代码如下: -(UIImageView *)animationImageView { if (!_animationImageView) { _animationImageView= [ ...
- 阿里云负载均衡SLB 七层https协议 nginx 获取真实IP
https://www.cnblogs.com/baylorqu/p/8565667.html https://help.aliyun.com/document_detail/54007.html
- [Golang] lua战斗验证服务器
我的另外一个开源项目,任何建议.指正错误和优化我都非常欢迎 baibaibai_000@163.com 简介 本项目是用go语言编写,结合cgo功能,支持高并发执行lua脚本的程序. 扩展 可以扩展成 ...
- linux系统下Nagios+rrdtool+Pnp4nagios监控环境的搭建
环境中的软件版本>>>>>>>>>>>>>>>>>>nagios版本:3.5.1rrdtoo ...