[抄题]:

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. 给iOS开发新手送点福利,简述UIDatePicker的用法

    1.Locale 设置DatePicker的地区,即设置DatePicker显示的语言. 1.跟踪所有可用的地区,取出想要的地区 NSLog(@"%@", [NSLocale av ...

  2. php 七种数据类型介绍

    PHP有7个数据类型.七个类型: 字符串, 整数, 浮动, 布尔, 数组, 对象, 资源. 字符串 字符串保持字符,如“一”.“abc”,“www.manongjc.com”等.PHP字符串是区分大小 ...

  3. CSS个人笔记

    1. CSS盒模型 1.1 控制元素尺寸属性 1.1.1 box-sizing: 改变元素应用的尺寸规则 当设置元素尺寸宽度为固定值时(eg: 100px), 其实是元素内容区域的宽度为100px, ...

  4. 代码生成器 CodeSmith 的使用(二)

    在第一篇中,简单的介绍了 CodeSmith 的使用方法,这次做一个生成简单的数据库字段属性的模板.以下只粘贴主要的代码片段. <%-- Name: Copyright © Sun 2013-2 ...

  5. 完整版openlayer的例子及中文注释(完整中文版)

    //@sourceURL=PersonLocation.jsvar window_temp = { onbeforeunload: null, DEBUG_MODE: false, MAPLIST: ...

  6. AS3 - 数组元素乱序方法以及效率比较

    http://www.hangge.com/blog/cache/detail_453.html

  7. SpringMvc 文件上传后台处理

    springMVC后台参数是通过MultipartFile类来转化Request的文件上传,但需要apache下fileupload的jar包做支持. 在springMVC的dispatcher-co ...

  8. UI5-文档-4.23-Custom Formatters

    如果希望对数据模型的属性进行更复杂的格式化逻辑,还可以编写自定义格式化函数.现在我们将使用自定义格式化程序添加本地化状态,因为数据模型中的状态是一种相当技术性的格式. Preview A status ...

  9. StringUtil字符串工具类

    package com.zjx.test03; /** * 字符串工具类 * @author * */ public class StringUtil { /** * 判断是否是空 * @param ...

  10. MySql DATE_FORMAT函数用法

    DATE_FORMAT(date, format) 函数用法 DATE_FORMAT(date, format) 函数根据format字符串格式化date值. 1.把字符串转为日期格式 实例: SEL ...