[抄题]:

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. RedisCluster读写分离改造

      RedisCluster模式启动的环境中,通过Redis中的每个连接,都可以访问 cluster nodes 访问到所有的服务器列表以及其所处于的角色(master/slave).对于RedisC ...

  2. 第12章 网络基础(1)_网络分层和TCP/IP协议族

    1. 协议的概念 (1)计算机网络中实现通信必须有一些约定.如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被称为通信协议 (2)在两个节点之间要成功地进行通信,两个节点之间必须 ...

  3. python爬虫rp+bs4

    一.开发环境 Beautiful Soup 4.4.0 文档: http://beautifulsoup.readthedocs.io/zh_CN/latest/#id28 Requests : ht ...

  4. Flash和滚动字幕

    flash 1.插入flash     1)<object>             <embed src="路径"></embed>      ...

  5. 5S后返回首页

    <!DOCTYPE html> <html> <head> <title>5S后返回首页</title> <meta http-equ ...

  6. Python——连接操作数据库

    1.安装MySQL驱动程序.下载自动安装包,双击安装即可,非常简单. 2.连接MySQL,下面是Python示例代码. import MySQLdbconn=MySQLdb.connect(host= ...

  7. django项目环境设置

    1.连接数据库,如mysql DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django_comi ...

  8. 异步与websocket

    异步与WebSockets 知识点 理解同步与异步执行过程 理解异步代码的回调写法与yield写法 Tornado异步 异步Web客户端AsyncHTTPClient tornado.web.asyn ...

  9. 17.scrapy框架简例使用

    目标:创建scrapy项目 创建一个spider来抓取站点和处理数据 通过命令行将抓取内容导出 1.创建项目 scrapy startproject tutorial 2.创建spider cd tu ...

  10. 0_Simple__simpleSurfaceWrite

    使用表面写入函数,结合纹理引用实现图片的旋转▶ 源代码 #include <stdio.h> #include <windows.h> #include <cuda_ru ...