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. jquery 设置页面元素不可点击、不可编辑、只读(备忘)

    $("input").attr('readonly', true); $("textarea").attr('readonly', true); $(':rad ...

  2. Android studio Gradle 教程

    一 . Gradle基础:https://segmentfault.com/a/1190000002439306 module下的gradle文件: // 声明是Android程序 apply plu ...

  3. 用PHP实现浏览器点击下载各种格式文档的方法详解【txt apk等等】

    [[注:其他文件想设置成下载文件,和下面介绍的方法一致]] 由于现在的浏览器已经可以识别txt文档格式,如果只给txt文档做一个文字链接的话,点击后只是打开一个新窗口显示txt文件的内容,并不能实现点 ...

  4. Analyzer报表里显示的 * 星号、红叉、#井号的意义

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

  5. Myeclipse出现 java文件中文乱码问题

    一.将整个project设置编码UTF-8(UTF-8可以最大的支持国际化) windows->Preferences->general->Workspace->Text fi ...

  6. lucene 索引 demo

    核心util /** * Alipay.com Inc. * Copyright (c) 2004-2015 All Rights Reserved/ */ package com.lucene.de ...

  7. pyqt官方示例

    文件夹 PATH 列表 卷序列号为 00000058 F027:7BEC C:. ├─activeqt │ └─webbrowser │ ├─icons │ └─pycache ├─animation ...

  8. Oralce 账户被锁后的解决办法

    SQL> connect sys/sys as sysdba; 已连接. SQL> alter user system account unlock; 用户已更改.  www.2cto.c ...

  9. 在线调试lua原型设计

    在规模日益增长的软件项目开发中, 如何有效的进行代码调试成为影响开发效率的致命因素之一.在当今网络游戏项目中, lua几乎成了项目脚本的标配.编译型的语言, 诸如C++, 都有良好的ide支持调试.而 ...

  10. 编写我的第一个CGI代码——python学习

    在编程学习网站学习Python语言,学习到cgi编程章节遇到了一些小问题,课程介绍的为linux环境的Apache配置方法,具体如下: [linux环境配置方法:] 在进行CGI编程前,确保您的Web ...