【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 & ...
随机推荐
- Canvas入门到高级详解(上)
神奇的 canvas--AICODER 全栈培训 IT 培训专家 一.canvas 简介 1.1 什么是 canvas?(了解) 是 HTML5 提供的一种新标签 <canvas>< ...
- office2007每次打开都要配置文件,怎么取消配置(可行)
取消打开软件进行配置的方法 打开“我的电脑”——“C盘”— —“Program Files\Common Files\ Microsoft Shared\OFFICE12\Office Setup C ...
- C语言 sscanf用法详解
/* sscanf用法详解 */ #include <stdio.h> /* sscanf头文件 */ #include <stdlib.h> #include <str ...
- MapReduce 计数器简介
转自:http://my.oschina.net/leejun2005/blog/276891?utm_source=tuicool&utm_medium=referral 1.计数器 简介 ...
- java把一个list分割成多个list存入map中(实例)
这都是最近我写工具遇到的一些点, 这些点就是指我在网上没搜到答案,然后实际上我为此花费了时间的 public static void main(String[] args) { List<Str ...
- Apple Watch S3 解锁 MacBook Pro 2015版失败的解决办法
我的MacBook Pro MF839由于只有128G的内存,所以就只能藏在我的抽屉底下,偶尔想体验一下xcode的时候再拿回来用下,想想都浪费 也不是不想换SSD,只是看了一下,价格太贵了,256G ...
- composer lavarel 安装
一:packagist库:https://packagist.org/packages/laravel/laravel 二:composer安装 // 安装到laravel文件夹 composer c ...
- Http/2知识图谱
HTTP/2和HTTP/1.x之间存在很大的差异,但以下优化规则是仍然是通用的:1. 优化DNS查询,若没有resolved的域名会阻塞请求:2. 优化TCP连接,HTTP/2只使用一个TCP连接:3 ...
- Fiddler 会话查找功能
如下,会话列表中有很多会话,我们如果想要找到想要的会话,可以通过通过快捷键 Ctrl + F 进行查找
- PHP5.5+ APC 安装
因php 语言特性(短链接), 没法实现共享内存来提升性能. apc的出现给出了一个解决方案 .不过很可惜5.5+以后PHP官方已经废弃掉这个扩展. 幸好出现了 apcu扩展提供后续功能 api 也没 ...