53. Maximum Subarray最大求和子数组12 3(dp)
[抄题]:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
方法一:贪心算法greedy
[一句话思路]:
每次都取最大值,sum-max-sum。
[画图]:

[一刷]:
[总结]:
就是背吧。
[复杂度]:
n/1
[英文数据结构]:
Range Queries
[其他解法]:
preflix sum
[题目变变变]:
minimum Subarray最小求和子数组。*(-1)后变成copy[i],求最大,再return(-1)*copy[i]。
public class Solution {
/*
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(List<Integer> nums) {
// write your code here
int size = nums.size();
int[] left_min = new int[size];
int[] copy = new int[size];
/*Get negative copy*/
for(int i = 0; i < size; i++){
copy[i] = -1 * nums.get(i);
}
int max = Integer.MIN_VALUE;
int sum = 0;
int minSum = 0;
for(int i = 0; i < size; i++){
sum += copy[i];
max = Math.max(max, sum - minSum);
minSum = Math.min(sum, minSum);
}
return -1 * max;
}
}
最小
方法二:preflix sum
[一句话思路]:
[画图]:

[一刷]:
minSum = Math.min(minSum, sum);为了使得连续和最大,Sum[j] 确定的情况下,Sum[i - 1]的值越小越好.
[总结]:
也是sum-max-sum,注意minSum要取最小值。
[复杂度]:n/1
[英文数据结构]:
[其他解法]:
class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 0 || nums == null) {
return -1;
}
int sum = 0;
int minSum = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
max = Math.max(max, sum - minSum);
minSum = Math.min(minSum, sum);
}
return max;
}
}
[题目变变变]:
2
[抄题]:
给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大。每个子数组的数字在数组中的位置应该是连续的,返回最大的和。给出数组 [1, 3, -1, 2, -1, 2]
这两个子数组分别为 [1, 3] 和 [2, -1, 2] 或者 [1, 3, -1, 2] 和 [2],它们的最大和都是 7。
[思维问题]:
以为因为两个数组不连续,不能用连续枚举。但由于是subarray所以可以。
[一句话思路]:
枚举:看哪个left[i] + right[i + 1]可以达到max
[画图]:
[一刷]:
- 用preflix sum,变量都能复用:再初始化就行。
- 最后枚举的时候不要用二元运算符,复用max就行了。
- 链表长度函数是.size,不是.sizeof
- for循环中的int i是局部变量,要重复定义
- for (int i = size - 1; i >= 0; i--) , 逆向时0也要取
- 不要妄想把隔着的几个负数挑出来,结果对了就行。最后的max是负数也得认,所以初始化max = Integer.MIN_VALUE;
[总结]:
两个量都在变,还有位置关系时,用两个数组表示。
[复杂度]:
n/1
[英文数据结构]:
[其他解法]:
[题目变变变]:
思路:左右的max分别存一个数组,然后用 max = Math.max(max, left[i] + right[i + 1]);。
这个题的思路是,因为 两个subarray 一定不重叠
所以必定存在一条分割线
分开这两个 subarrays
所以 最后的部分里:
max = Integer.MIN_VALUE;
for(int i = 0; i < size - 1; i++){
max = Math.max(max, left[i] + right[i + 1]);
}
return max;
这里是在枚举 这条分割线的位置
然后 left[] 和 right[] 里分别存的是,某个位置往左的 maximum subarray 和往右的 maximum subarray。
public class Solution {
/*
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
public int maxTwoSubArrays(List<Integer> nums) {
// write your code here
int size = nums.size();
int[] left = new int [size];
int[] right = new int [size];
//put in the left, using perflix sum
int sum = 0;
int minSum = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
sum += nums.get(i);
max = Math.max(max, sum - minSum);
minSum = Math.min(minSum, sum);
left[i] = max;
}
//put in the right, using perflix sum
sum = 0;
minSum = 0;
max = Integer.MIN_VALUE;
for (int i = size - 1; i >= 0; i--) {//0也要取
sum += nums.get(i);
max = Math.max(max, sum - minSum);
minSum = Math.min(minSum, sum);
right[i] = max;
}
//for the result
max = Integer.MIN_VALUE;
for (int i = 0; i < size - 1; i++) {
max = Math.max(max, left[i] + right[i + 1]);
}
return max;
}
}
53. Maximum Subarray最大求和子数组12 3(dp)的更多相关文章
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- 刷题53. Maximum Subarray
一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
随机推荐
- https的加密解密是怎么写的?
原文转载至:http://blog.csdn.net/aqiangsz/article/details/53611665 文章中有些不对的地方,比如用证书对改随机码进行加密,这个是不对,证书本身并没有 ...
- centos安装jdk1.7.80的rpm包
1.jdk1.7.80的rpm的包的下载地址如下,请自己选择32位还是64位,下载完之后通过vmware的共享功能共享一个文件下,然后使用cp命令copy到linux系统中去. http://www. ...
- Vuex 状态管理模式
Vuex 是一个专为 Vue.js 设计的状态管理模式 vuex解决了组件之间同一状态的共享问题.当我们的应用遇到多个组件共享状态时,会需要: 多个组件依赖于同一状态.传参的方法对于多层嵌套的组件将会 ...
- Python中的logger和handler到底是个什么鬼
最近的任务经常涉及到日志的记录,特意去又学了一遍logging的记录方法.跟java一样,python的日志记录也是比较繁琐的一件事,在写一条记录之前,要写好多东西.典型的日志记录的步骤是这样的: 创 ...
- spark streaming 概述
批处理 & 流处理 像这个是批处理 像这样就是流处理 为什么需要流处理--更多场景需要 Spark Core & RDD 本质上是离线运算 Spark Streaming是什么(分布式 ...
- 教Alexa看懂手语,不说话也能控制语音助手
Alexa.Siri.小度……各种语音助手令人眼花缭乱,但这些设备多是针对能力健全的用户,忽略了听.说能力存在障碍的人群.本文作者敏锐地发现了这一 bug,并训练亚马逊语音助手 Alex 学会识别美式 ...
- fs和http模块
fs模块写入文件的方式 导入内置模块 const fs=require("fs") 一.异步写入方式 fs.writeFile("写入文件的路径&qu ...
- mysql更新(四) 数据类型
07-数据类型 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考链接:http://www.runoob.com/mysql/m ...
- tornado-输出,request
3种输出方法:write render redirectimport tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 ...
- Xeon Phi 编程备忘
▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...