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: ...
随机推荐
- laravel中model类中好用的方法
public function field() { return $this->belongsTo(HrmAuthFieldsModel::class, 'filed_id', 'id'); } ...
- 什么是webpack以及为什么使用它
什么是webpack以及为什么使用它 新建 模板 小书匠 在ES6之前,我们要想进行模块化开发,就必须借助于其他的工具.因为开发时用的是高级语法开发,效率非常高,但很可惜的是,浏览器未必会支持或认识 ...
- bind标签_databaseId标签,_parameter标签的使用
1.在接口写方法 public List<Employee> getEmpsTestInnerParameter(Employee employee); 2在映射文件中进行配置 <s ...
- C# CLR20R3 程序终止的几种解决方案 【转】
[转]CLR20R3 程序终止的几种解决方案 这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理异常(例如,线程池线程中的未处理异常)提供支撑,而 Framework ...
- JVM参数配置及内存调优
一.JVM常见参数配置 堆内存相关参数 参数名称 含义 默认值 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40% ...
- 获取使用GitHub api和Jira api Authentication的方法
近段时间在搭建我司的用例管理平台,有如下需求: 1.需要根据项目--版本--轮次的形式来管理项目用例,用例统一保存在git工程. 2.执行用例时,如果用例执行失败,可以通过平台在Jira上提bug. ...
- Django聚合数据
背景: 有些时候,光靠数据库中已有字段的数据,还不足以满足一些特殊场景的需求,例如显示一个作者的所有书籍数量. 这时候就需要在已有数据基础上,聚合出这些没有的数据. 为查询集生产聚合: Django ...
- oracle数据库表空间创建&导入&导出
1.表空间创建 --删除表空间 drop tablespace EVPBDMGIS including contents and datafiles; --删除用户 drop user EVPBDMG ...
- HTTP 常见相应状态码及含义
1xx:信息 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. 101 Switching Protocols 服务器转换协议:服务器将 ...
- PL/SQL基本操作
1.常规过程化形式 declare o_booking_flag ); begin -- Call the procedure destine_ticket(', , 'E', , o_booking ...