[抄题]:

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

[画图]:

[一刷]:

  1. 用preflix sum,变量都能复用:再初始化就行。
  2. 最后枚举的时候不要用二元运算符,复用max就行了。
  3. 链表长度函数是.size,不是.sizeof
  4. for循环中的int i是局部变量,要重复定义
  5. for (int i = size - 1; i >= 0; i--) , 逆向时0也要取
  6. 不要妄想把隔着的几个负数挑出来,结果对了就行。最后的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)的更多相关文章

  1. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  2. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  5. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  8. 刷题53. Maximum Subarray

    一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...

  9. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

随机推荐

  1. ESXI 5.5加载 zabbix OVF 3.2.6操作

    如果是虚拟机安装ZABBIX,ZABBIX的前台WEB时间,是由虚拟机的BIOS时间决定的. 一. 1.去官方下载vmdk磁盘镜像 链接地址为https://sourceforge.net/proje ...

  2. pycharm下getpass.getpass()卡住

    pycharm下getpass.getpass()卡住不运行是什么问题 python pycharm 首先声明 下面这几行代码在命令行和eclipse下都能正常运行 import getpass pr ...

  3. Python Json序列化与反序列化

    在python中,序列化可以理解为:把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式字符串解码为python数据对象.在python的标准库中,专门提供了json ...

  4. IDEA在编辑时提示could not autowire

    IDEA在编辑时提示could not autowire 原创 2016年05月14日 10:53:38 28338 在开发中我再applicationContext-dao.xml中加入了mappe ...

  5. python 字符串与字节之间的相互转化

    1.将字符串转化成字节 b'fffff' bytes('ffff', encoding='utf-8') 'ffff'.encode('utf-8') 2.将字节转化成字符串 str(data, en ...

  6. margin-top失效

    span标签是行类元素,只能margin-left,right 解决办法: 将span标签改为块级标签

  7. c#面向对象基础4

    一.namespace 命名空间 作用:解决不同类重名的问题  我们可以认为类是属于命名空间的 当我们需要再一个类中与另一个类建立关系时,通过命名空间来区别不同的类.所以需要我们这样做:导入命名空间 ...

  8. aspx后台代码写在前段

    合并cs的代码 <%@ Page Language="C#" AutoEventWireup="true" Inherits="System.W ...

  9. hive 锁

    HiveQL是一种SQL语言,但缺少udpate和insert类型操作时的行,列或者查询级别的锁支持,hadoop文件通常是一次写入(支持有限的文件追加功能),hadoop和hive都是多用户系统,锁 ...

  10. gvim下用Vundle安装solarized主题的方法

    1.在.vimrc中加入 Bundle 'Solarized' 2.重启gvim,并执行 :BundleInstall 3.将solarized.vim文件放入.vim下的colors文件夹内(如果没 ...