【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 & ...
随机推荐
- js cookie跨域设置
/** * 设置cookie方法 * @param {string} c_name cookie键值 * @param {string} value cookie值 * @param {Boolean ...
- docker容器运行与退出
#下载centos镜像,运行一个名为mycentos的容器,并在容器里运行/bin/bash docker run -ti --name mycentos centos /bin/bash #退出 e ...
- Java编程的逻辑 (87) - 类加载机制
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- windows保存的文件传输到linux中格式转换
直接从window传输到linux的脚本执行时,会出现以下错误. -bash: xxx: /bin/sh^M: bad interpreter: No such file or directory 解 ...
- GraphX学习笔记——可视化
首先自己造了一份简单的社交关系的图 第一份是人物数据,id和姓名,person.txt 1 孙俪 2 邓超 3 佟大为 4 冯绍峰 5 黄晓明 6 angelababy 7 李冰冰 8 范冰冰 第二份 ...
- centos7 yum安装ffmpeg,以及ffmpeg的简单用法
yum install ffmpeg: 安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持: yum install -y epel-release #如果出现缺少C ...
- 【QT】QT更改构建目录
菜单“工具”-“选项”. Qt Creator怎样更改默认构建目录_百度经验 https://jingyan.baidu.com/article/6181c3e0b9d170152ef153af.ht ...
- Spring MVC 知识点整理
extend:http://www.jianshu.com/p/bef0e52067d2 1. Redis 存储方式 Redis存储机制分成两种Snapshot 和 AOF.无论是那种机制,Redis ...
- Linux 的基本操作(初识linux)
linux世界 [Linux 系统启动过程] Linux的启动其实和windows的启动过程很类似,不过windows我们是无法看到启动信息的,而linux启动时我们会看到许多启动信息,例如某个服务是 ...
- markdown 基本语法(转载)
最近感觉一直使用富文本编辑器写东西,感觉有点烦,所以就试着学习了一下简单的markdown编辑器的使用 原文地址:http://www.jianshu.com/p/815788f4b01d markd ...