Question:

Find the contiguous subarray within an array (containing at least one number) that has the largest sum.

For example,

given the array [2, 1, –3, 4, –1, 2, 1, –5, 4],

The contiguous array [4, –1, 2, 1] has the largest sum = 6.

此问题提供了两种解决方案,首先是L+A[M]+R模式,把一个问题分解为左右及中间三部分。若是包含中间部分则直接输出最大值,否则为L或R,并重复前面的操作。

O(n log n) runtime, O(log n) stack space

public int maxSubArray(int[] A) {
return maxSubArrayHelper(A, 0, A.length - 1);
}
private int maxSubArrayHelper(int[] A, int L, int R) {
if (L > R) return Integer.MIN_VALUE;
int M = (L + R) / 2;
int leftAns = maxSubArrayHelper(A, L, M - 1);
int rightAns = maxSubArrayHelper(A, M + 1, R);
int lMaxSum = 0;
int sum = 0;
for (int i = M - 1; i >= L; i--) {
sum += A[i];
lMaxSum = Math.max(sum, lMaxSum);
}
int rMaxSum = 0;
sum = 0;
for (int i = M + 1; i <= R; i++) {
sum += A[i];
rMaxSum = Math.max(sum, rMaxSum);
}
return Math.max(lMaxSum + A[M] + rMaxSum, Math.max(leftAns, rightAns));
}

注:读代码时,从整体到部分,不要一下子陷入到递归中。

第二种解决方案,动态编程(DP)。

O(n) runtime, O(1) space  

若f(k)是截止到下标为k时的最大值,那么一定满足 f(k) = max( f(k-1) + A[k], A[k] ),这样就找到了f(k) 与 f(k-1) 的关系。这样,用两个标记,一个记载到k处的最大值,一个更新当前的最大值。

public int maxSubArray(int[] A) {
int maxEndingHere = A[0], maxSoFar = A[0];
for (int i = 1; i < A.length; i++) {
maxEndingHere = Math.max(maxEndingHere + A[i], A[i]);
maxSoFar = Math.max(maxEndingHere, maxSoFar);
}
return maxSoFar;
}

  

Leetcode详解Maximum Sum Subarray的更多相关文章

  1. 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...

  2. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

    题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  3. 【leetcode】1031. Maximum Sum of Two Non-Overlapping Subarrays

    题目如下: Given an array A of non-negative integers, return the maximum sum of elements in two non-overl ...

  4. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  5. 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...

  6. LeetCode算法题-Maximum Average Subarray I(Java实现)

    这是悦乐书的第278次更新,第294篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第146题(顺位题号是643).给定由n个整数组成的数组,找到具有最大平均值的长度为k的 ...

  7. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  8. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  9. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

随机推荐

  1. Mysql修改字段长度

    alter table '表名' modify column '列名' varchar(50);

  2. 【前端】JavaScript获取指定范围内的随机整数

    function getRandomIntNumber(min, max) { var span = max - min + 1; var result = Math.floor(Math.rando ...

  3. Swift_1基础

    // swift中导入类库使用import,不再使用<>和""import Foundation // 输出print("Hello, World!" ...

  4. OpenModelica仿真

    复杂产品通常涉及机械.控制.电子.液压.气动和软件等多学科领域,其设计过程需要进行仿真,以满足对成本.质量.性能等的要求.目前各个学科和领域都已经有了比较成熟的仿真软件,但大部分仿真软件仅适用于本学科 ...

  5. 利用cytoscape做网络图

    首先做出下面的基因间相互关系图 1.准备sif文件 data.sif 网络数据文件 gene1 pp gene2 gene3 gene4 gene5 gene6 gene7 gene8 gene9 n ...

  6. LBWE更新模式切换问题:缓存清理

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. Openlayers自定义简单popup

    OpenLayers中可以使用很多种类型的popup,大家可以到Openlayers的 popupMatrix.html示例中看.之前存在这样一个错误的想法:popup和marker是绑定的,要有po ...

  8. Jmeter + Grafana + InfluxDB 性能测试监控

    阅读目录 1. 安装InfluxDB 2. 安装Grafana 3. 配置Jmeter 序章 前几天在群里看到大神们在讨论Jmeter + InfluxDB + Grafana监控.说起来Jmeter ...

  9. [Android] Google IAP unmaneged items服务器校验

    android IAP unmaneged items 服务器校验 当成功IAP以后, 会在google服务器记录此次购买的状态. 可以通过Google Play Android Developer ...

  10. 解决IIS上无法添加.NET用户的问题

    最近开发了一个简单的管理后台,后台用户管理都用的是AspNetSqlMembershipProvider这一套框架,添加和删除用户的功能在开发阶段通过ASP.NET网站管理工具完成. 部署到服务器上时 ...