LeetCode——Maximum Subarray
Description:
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.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
看见这题的第一想法就是决不能用暴力穷举。
然后应该会想到简单的dp。思路就是每次都选择最大的sum(这不是贪心?)。时间复杂度是O(n),空间复杂度是O(1);
public class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
int sum = 0;
for(int i=0; i<nums.length; i++) {
sum += nums[i];
if(max < sum) max = sum;
if(sum < 0) sum = 0;
}
return max;
}
}
题目最后还有一个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.
这样的话就要用分治和递归来解。
首先把问题分成若干子问题,找到子问题中的解后逐一合并直到得到整个问题的解。说起来挺简单但是细节问题还是要特别注意的。
时间复杂度是O(nlogn),空间复杂度是O(logn);
public class Solution {
//分治
public int divide(int[] nums, int m, int n) {
if(m == n) {
return nums[m];
}
int mid = m + (n-m)/2; //防止整数溢出
int home = divide(nums, m, mid);
int end = divide(nums, mid+1, n);
int sum = merge(nums, m, n, mid);
return max(sum, max(home, end));
}
//合并
public int merge(int[] nums, int m, int n, int mid) {
int leftMax = nums[mid];
int sum = 0;
for(int i=mid; i>=m; i--) {
sum += nums[i];
if(leftMax < sum) {
leftMax = sum;
}
}
sum = 0;
int rightMax = nums[mid+1];
for(int i=mid+1; i<=n; i++) {
sum += nums[i];
if(rightMax < sum) {
rightMax = sum;
}
}
sum = leftMax + rightMax;
return sum;
}
public int max(int a, int b) {
return a > b ? a : b;
}
public int maxSubArray(int[] nums) {
if(nums.length <= 0) {
return 0;
}
int res = divide(nums, 0, nums.length-1);
return res;
}
}
分治、dp、贪心有时候傻傻分不清楚。
LeetCode——Maximum Subarray的更多相关文章
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...
- 53. [LeetCode] Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- LeetCode Maximum Subarray (最大子段和)
题意: 给一个序列,求至少含一个元素的最大子段和? 思路: 跟求普通的最大子段和差不多,只不过需要注意一下顺序.由于至少需要一个元素,所以先将ans=nums[0].接下来可以用sum求和了,如果小于 ...
随机推荐
- pthread_cond_wait()函数的详解
http://hi.baidu.com/tjuer/item/253cc6d66b921317d90e4483 了解 pthread_cond_wait() 的作用非常重要 -- 它是 POSIX 线 ...
- Android——网格视图 GridView
activity_activitygrid.xml <?xml version="1.0" encoding="utf-8"?> <GridV ...
- 内核定时器timer_list
内核在时钟中断发生后执行检测各个定时器是否到期,到期后的定时器处理函数将作为软中断在底半部执行.实质上,时钟中断处理程序会唤起TIMER_SOFTIRQ软中断,运行当前处理器上到期的所有定时器.lin ...
- 在XP系统下如何访问win10共享的打印机
< > 找到 GUEST 用户,添加即可. 2. Win10 共享本地打印机 右击要共享的打印机,共享选项卡,设置共享名,这个共享很重要,要记住,尽量设置简单点.IP + 共享名就是网络打 ...
- linux设置开机服务自动启动/关闭自动启动命令
linux设置开机服务自动启动/关闭自动启动命令 2012-02-06 15:13 [root@localhost ~]# chkconfig --list 显示开机可以自动启动的服务[roo ...
- Ajax-ajax实例4-多级联动菜单
项目结构: 项目运行: 技术要点: 1.4.1 技术要点在分析具体的实现代码之前,先介绍一下本例的几个技术要点.1 .选项的动态创建与删除document 对象的 createElement 方法可以 ...
- 继电器是如何成为CPU的
阅读目录(Content) 从电池.开关和继电器开始 用继电器做个与门 用继电器做个或门 用继电器做个异或门 做一些看起来可用的东西 小小约定 振荡器 加法器 寄存器 R-S触发器 D触发器 上升沿D ...
- 《FPGA全程进阶---实战演练》第三章之接地设计
信号回路的电位基准点,(直流电源的负极或者零伏点)在单板上可以分为数字地和模拟地.理想的工作地是电路参考点的等电位平面,然而在实际中,工作地被认为信号电流的低阻抗回路和电源的供电回路,这样就会有三个方 ...
- 【转】Hibernate系列学习之(二) 多对一、一对一、一对多、多对多的配置方法
hihernate一对多关联映射(单向Classes----->Student) 一对多关联映射利用了多对一关联映射原理 多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向 ...
- Spring Boot 官方文档学习(一)入门及使用
个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...