【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 & ...
随机推荐
- SQL Server 2016新特性:Live Query Statistics
SSMS可以提供可以查看正在执行的计划.live query plan可以查看一个查询的执行过程,从一个查询计划操作到另外一个查询计划操作.live query plan提供了整体的查询运行进度和操作 ...
- 利用SEH防范BP(int 3)断点
利用SEH技术实现反跟踪,这个方法比单纯用判断API函数第一个字节是否为断点更加有效,可以防止在API函数内部的多处地址设置断点 通过int 3指令故意产生一个异常,从而让系统转入自己的异常处理函数, ...
- VS调试不能进入断点,提示当前不会命中断点还未为文档加载任何符号
经过仔细检查后发现,是DLL版本和源码生成的DLL版本不一致,造成的! 复制新的过去,问题就解决了.
- Ubuntu环境使用apt命令下载管理包的优势
操作系统:Ubuntu 18.04 LTS 一.概述 之前在Ubuntu下我一直坚持将软件下载包下载到指定文件夹下进行解压安装的习惯,在部门同事的建议下,我开始使用apt命令下载管理包. 由于网上已经 ...
- (转)The C10K problem翻译
The C10K problem 如今的web服务器需要同时处理一万个以上的客户端了,难道不是吗?毕竟如今的网络是个big place了. 现在的计算机也很强大了,你只需要花大概$1200就可以买一个 ...
- JAVA并发理论与实践
JDK5.0中更灵活.更具可伸缩性的锁定机制 流行的原子 非阻塞算法简介
- 《effective Go》读后记录:GO基础
一个在线的Go编译器 如果还没来得及安装Go环境,想体验一下Go语言,可以在Go在线编译器 上运行Go程序. 格式化 让所有人都遵循一样的编码风格是一种理想,现在Go语言通过gofmt程序,让机器来处 ...
- 【OCP|OCM】Oracle培训考证系列
[OCP|OCM]Oracle培训考证系列 我的个人信息 网名:小麦苗 QQ:646634621 QQ群:618766405 我的博客:http://blog.itpub.net/26736162 ...
- Centos下普通用户设置sudo权限
若执行sudo命令的用户没有sodu权限,则会报以下错误 violet is not in the sudoers file.This incident will be reported 若想让vio ...
- 【C++ 模板迭代器实例/半素数】
题目:判断一个数是不是两个素数的乘积,是输出YES,不是输出NO.数据范围为2-1000000. 为了解决这个问题,我们继续使用STL——vector & set,分别用来存储素数和半素数.为 ...