Leetcode详解Maximum Sum Subarray
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的更多相关文章
- 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...
- 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- 【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 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- LeetCode算法题-Maximum Average Subarray I(Java实现)
这是悦乐书的第278次更新,第294篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第146题(顺位题号是643).给定由n个整数组成的数组,找到具有最大平均值的长度为k的 ...
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
随机推荐
- android shortcut &livefoulder
android shortcut(实现步骤) 1.建立activity 2.minifest 里面注册并加上intent-filter,name为:android.intent.action.CREA ...
- ubuntu安装
今天在win10下安装Ubuntu,结果没经验导致win10找不回来了,我再好好整理些思路 安装前要做一个ghost,万一出现问题可以用来恢复系统! 1,我使用USB Installer 在http: ...
- ubuntu 14.04 vsftpd安装问题
sudo apt-get install vsftpd; 打开允许访问用户名 local_enable=YES write_enable=YES chroot_list_file=/etc/vsftp ...
- 记一次python编码错误
摘要: 断断续续写python一段时间了,让我说python最令我头疼的问题,莫过于编码问题.最近做大论文,使用python再次出现编码报错.错误如下: "UnicodeEncodeErro ...
- for循环j = j++ 和 j = ++j
package com.test.forname; public class TestForName { public static void main(String[] args) throws E ...
- linux sort,uniq,cut,wc.
文章转自 http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html sort sort 命令对 File 参数指定的文件中的行排 ...
- docker compose 笔记
https://www.youtube.com/watch?v=Uez88TWOECg 是基于这个视频做的笔记. Docker Compose: Compose is a tool for defin ...
- 如何挂自己的web项目(免费拥有自己的网站及域名)
http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html ------------------------------------ ...
- 个人博客作业Week1
个人博客作业Week1 一.问题 通读<构建之法>我有一下几个问题 PM没有参与代码编如何进行管理. 软件工程师的职业资格考试对我们来说很有必要吗. 当我们为用户开发软件时我们需要了解用户 ...
- a=av###b=bv###c=cv map键值对 (a,av) (b,bv) (c,cv)
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; impo ...