53. Maximum Subarray (JAVA)
iven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
法I: 动态规划法
class Solution {
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE; //注意有负数的时候,不能初始化为0
int currentSum = Integer.MIN_VALUE;
for(int i = 0; i < nums.length; i++){
if(currentSum < 0) currentSum = nums[i];
else currentSum += nums[i];
if(currentSum > maxSum) maxSum = currentSum;
}
return maxSum;
}
}
法II:分治法
class Solution {
public int maxSubArray(int[] nums) {
return partialMax(nums,0,nums.length-1);
}
public int partialMax(int[] nums, int start, int end){
if(start == end) return nums[start];
int mid = start + ((end-start) >> 1);
int leftMax = partialMax(nums,start, mid);
int rightMax = partialMax(nums,mid+1,end);
int maxSum = Math.max(leftMax,rightMax);
int lMidMax = Integer.MIN_VALUE;
int rMidMax = Integer.MIN_VALUE;
int current = 0;
for(int i = mid; i >= start; i--){
current += nums[i];
if(current > lMidMax) lMidMax = current;
}
current = 0;
for(int i = mid+1; i <= end; i++){
current += nums[i];
if(current > rMidMax) rMidMax = current;
}
if(lMidMax > 0 && rMidMax > 0) maxSum = Math.max(lMidMax + rMidMax,maxSum);
else if(lMidMax > rMidMax) maxSum = Math.max(lMidMax,maxSum);
else maxSum = Math.max(rMidMax,maxSum);
return maxSum;
}
}
53. Maximum Subarray (JAVA)的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- 类锁和对象锁,synchronized修饰static方法与非static方法的区别
当synchronized修饰一个static方法时,多线程下,获取的是类锁(即Class本身,注意:不是实例), 作用范围是整个静态方法,作用的对象是这个类的所有对象. 当synchronized修 ...
- [BZOJ3453]tyvj 1858 XLkxc:拉格朗日插值
分析 之前一直不知道拉格朗日插值是干什么用的,只会做模板题,做了这道题才明白这个神奇算法的用法. 由题意可知,\(f(x)\)是关于\(x\)的\(k+1\)次函数,\(g(x)\)是关于\(x\)的 ...
- D. Restore Permutation
D. Restore Permutation 就是给一个n个数的全排,然后bi记录比ai小且在排在ai前面的数的和,求ai 树状数组维护,二分 #include<bits/stdc++.h> ...
- es入门--curl的使用
文档介绍: 首先要讲什么是文档,我们中大多是java程序员,java是面向对象的,那么在elasticsearch看来:对象和文档是等价的.只不过这个对象是可以被序列化成key-value形式的jso ...
- 程序员心髓:移动应用API设计10大技巧
移动App与基于Web/云服务发生对话是很常见的事情,最简单的可能仅仅只是检索数据,但也可能包含发送数据.用户授权和管理.而这也就验证了为移动应用建立API的重要性,为此,我们特总结了10大移动API ...
- express 请求参数的一些问题
先说点别的,项目入口是index.js,运行 node index 启动项目. 路由部分app.get('/', function(req, res) { res.send('hello, expre ...
- leetcode-mid-backtracking-17. Letter Combinations of a Phone Number
mycode 68.26% class Solution(object): def letterCombinations(self, digits): """ :typ ...
- RotateZoom.cpp 20180622
20180622代码加入随意变换图像大小 批处理框架先不看:-B src3.bmp 10 1 30 2 0.1 3 tar.bmp src2.bmp 37.5 2.1 tar // RotateZo ...
- [VBA]批量新建指定名称的工作表
sub 批量新建指定名称的工作表() Dim i As Integer For i = 2 To 10 '根据实际情况修改i大小 Worksheets.Add after:=Worksheets ...
- java中? extends T 和? super T解析
转:https://blog.csdn.net/qq_25337221/article/details/81669630 PECS原则 最后看一下什么是PECS(Producer Extends Co ...