LeetCode 53. Maximum Subarray(最大的子数组)
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
.
click to show more practice.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
题目标签:Array
Java Solution 1:
Runtime beats 71.37%
完成日期:03/28/2017
关键词:Array
关键点:基于 Kadane's Algorithm 改变
public class Solution
{
public int maxSubArray(int[] nums)
{
// Solution 1: O(n)
// check param validation.
if(nums == null || nums.length == 0)
return 0; int sum = 0;
int max = Integer.MIN_VALUE; // iterate nums array.
for (int i = 0; i < nums.length; i++)
{
// choose a larger one between current number or (previous sum + current number).
sum = Math.max(nums[i], sum + nums[i]);
max = Math.max(max, sum); // choose the larger max.
} return max;
} }
Java Solution 2:
Runtime beats 71.37%
完成日期:03/28/2017
关键词:Array
关键点:Kadane's Algorithm
public class Solution
{
public int maxSubArray(int[] nums)
{
int max_ending_here = 0;
int max_so_far = Integer.MIN_VALUE; for(int i = 0; i < nums.length; i++)
{
if(max_ending_here < 0)
max_ending_here = 0;
max_ending_here += nums[i];
max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
} }
Java Solution 3:
Runtime beats 29.96%
完成日期:03/29/2017
关键词:Array
关键点:Divide and Conquer
public class Solution
{
public int maxSubArray(int[] nums)
{
// Solution 3: Divide and Conquer. O(nlogn)
if(nums == null || nums.length == 0)
return 0; return Max_Subarray_Sum(nums, 0, nums.length-1);
} public int Max_Subarray_Sum(int[] nums, int left, int right)
{
if(left == right) // base case: meaning there is only one element.
return nums[left]; int middle = (left + right) / 2; // calculate the middle one. // recursively call Max_Subarray_Sum to go down to base case.
int left_mss = Max_Subarray_Sum(nums, left, middle);
int right_mss = Max_Subarray_Sum(nums, middle+1, right); // set up leftSum, rightSum and sum.
int leftSum = Integer.MIN_VALUE;
int rightSum = Integer.MIN_VALUE;
int sum = 0; // calculate the maximum subarray sum for right half part.
for(int i=middle+1; i<= right; i++)
{
sum += nums[i];
rightSum = Integer.max(rightSum, sum);
} sum = 0; // reset the sum to 0. // calculate the maximum subarray sum for left half part.
for(int i=middle; i>= left; i--)
{
sum += nums[i];
leftSum = Integer.max(leftSum, sum);
} // choose the max between left and right from down level.
int res = Integer.max(left_mss, right_mss);
// choose the max between res and middle range. return Integer.max(res, leftSum + rightSum); } }
参考资料:
http://www.cnblogs.com/springfor/p/3877058.html
https://www.youtube.com/watch?v=ohHWQf1HDfU
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 53. Maximum Subarray(最大的子数组)的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- CSS3 3D环境实现立体 魔方效果代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- System.getProperty()参数大全
System.getProperty()获取Java各种配置属性,参数如下: Java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vend ...
- 图文详解在Windows server 2008 R2上安装SQL Server 2012集群
1.准备: 4台服务器(1台AD.2台SQL服务器.1台iSCSI存储服务器) 9个IP(1个AD的IP.2个SQL服务器的IP.2个心跳IP.1个iSCSI存储服务器的IP.1个集群IP.1个DTC ...
- oracle sql 树操作
语法:select-start with-connect by-prior 主要有两点 1)prior放在子节点端,则表示扫描树是以start with指定的节点作为根节点从上往下扫描.可能对应一个或 ...
- NET应用——使用RSA构建相对安全的数据交互
最近又被[现场破解共享单车系统]刷了一脸,不得不开始后怕:如何防止类似的情况发生? 想来想去,始终觉得将程序加密是最简单的做法.但是摩拜.ofo也有加密,为什么仍然被破解?那是因为请求在传输过程中被篡 ...
- Split分割字符串
第一种方法:打开vs.net新建一个控制台项目.然后在Main()方法下输入下面的程序. string s="abcdeabcdeabcde"; string[] sArray=s ...
- 安装 node-sass 时报错
在安装 node-sass 时报错,截图如下 解决方法如下: npm install --save node-sass --registry=https://registry.npm.taobao.o ...
- Python操作csv文件
1.什么是csv文件 The so-called CSV (Comma Separated Values) format is the most common import and export fo ...
- Hibernate Mapping Exception:-9
if("true".equals(map.get("isAudited"))){ isAudited="=";//已审核 }else{ is ...
- jdbc学习总结
jdbc学习总结: 一.简介: jdbc,直译为java连接数据库.实际为java为很好的操作数据库而提供的一套接口,接口的实现(即驱动)由各个数据库厂商提供. 二.知识要点: 连接5要素,3 ...