[leetcode]53. 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
原理:任何数加负数肯定比原值小
1. dp[i] stands for max sum of subarray[0,i] (nums[i] must be used)
2. coz any negative will worsen final result. supposing I am standing at current nums[i], if previous sum dp[i-1] > 0, it will benifit result, then we wrap dp[i-1] + nums[i] to dp[i]
otherwise, if dp[i-1] < 0, we only keep nums[i]
3. the dp function will be :
dp[i] = dp[i-1] >0 ? dp[i-1] + num[i] : num[i]
code
class Solution {
public int maxSubArray(int[] nums) {
// dp[i]: max sum of subarray from 0 to i
int[] dp = new int[nums.length];
// initiliaze
dp[0] = nums[0];
int result = nums[0]; for(int i = 1; i < nums.length; i++){
// negative value will worsen result, if dp[i-1] < 0, we only keep nums[i]
dp[i] = dp[i-1] > 0 ? dp[i-1] + nums[i] : nums[i];
// update max result
result = Math.max(result, dp[i]);
}
return result;
}
}
思路
优化空间为O(1)
Coz result is only related to previous sum.
We can use a variable to track previous sum.
代码
public class MaximumSubarray {
public int maxSubArray(int[] nums) {
// initialize
int result = Integer.MIN_VALUE;
// reset it to 0 if it's less than 0.
int sum = 0;
for (int n : nums) {
// if sum < 0, we only keep n; if sum > 0, sum to be added to benefit result
sum = n + Math.max(sum, 0);
// update max result
result = Math.max(result, sum);
}
return result;
}
}
[leetcode]53. Maximum Subarray最大子数组和的更多相关文章
- [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最大子序和 (C++)
题目: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
随机推荐
- 用turtle画图
turtle是python自带一个寓教于乐的小乌龟,可以控制乌龟移动(机器人),可以留下足迹. turtledemo里有许多官方例子.刚才随性而发做,看了介绍随手画了一个,有点像自动原包机,通过简单的 ...
- iso系统镜像刻录到光盘和U盘
使用UltraISO刻录 刻录U盘,点击文件,打开,选择镜像 启动,写入硬盘镜像选择U盘即可 刻录光盘 工具,刻录光盘映像,选择镜像,需要先插入光盘刻录机(有些电脑可能自带光驱盘,且有刻录功能,那么我 ...
- Spring Boot/Spring Cloud
104.什么是 spring boot? 在Spring框架这个大家族中,产生了很多衍生框架,比如 Spring.SpringMvc框架等,Spring的核心内容在于控制反转(IOC) ...
- Hi3516EV100烧录出厂固件
1.Hitool烧录uboot 2.uboot下烧录固件 setenv serverip 192.168.1.138 mw.b ff ;tftp ;sf probe ;sf erase ;sf wri ...
- python3中的编码
python2字符串编码存在的问题: 使用 ASCII 码作为默认编码方式,对中文处理不友好 把字符串分为 unicode 和 str 两种类型,将unicode作为唯一内码,误导开发者 python ...
- Java12配置
配置环境变量: 之前的JAVA_HOME和CLASSPATH已经都不要了.只要配置jdk的bin到Path里: C:\Program Files\Java\jdk-12\bin
- 【idea】之使用SVN一些技巧
@Copy https://www.cnblogs.com/whc321/p/5669804.html
- 初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器
第一次接触nginx的时候,那时候公司还是用的一些不知名的小技术,后来公司发展问题,重新招了人,然后接触到nginx,公司 使用nginx用来做代理服务器,所有请求 都先经过nginx服务器,然后交由 ...
- Python的socket
第一部分socket的简单示例 服务器部分: """ Description: Author:Nod Date: Record: #------------------- ...
- 在 iOS 上通过 802.11k、802.11r 和 802.11v 实现 Wi-Fi 网络漫游
原文: https://support.apple.com/zh-cn/HT202628 了解 iOS 如何使用 Wi-Fi 网络标准提升客户端漫游性能. iOS 支持在企业级 Wi-Fi 网络上 ...