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求和了,如果小于 ...
随机推荐
- 推断iframe里的页面载入完毕
//推断iframe是否载入完毕,RMid为iframe的ID document.getElementById("RMid").onload = function () { ale ...
- Select显示多级分类列表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 字符串移位:如“abcdefghi”右移2位后变成“cdefghiab”
函数头: //pStr 是指向以'\0'结尾的字符串指针 //steps 是要求移动的步数 void LoopMove(char *pStr, int steps) { //...... } 使用库函 ...
- 一个残酷的生鲜O2O之梦
三个年轻人,毕业一年,上海浦东张江开了一家生态有机蔬菜店-稻香麦甜. 首先,这不是一个关于成功励志的故事,相反的,我们走向了悬崖.经营2个月,最终以签字转让结束了实体店. 准备阶段 我,首先辞职,那时 ...
- 关于Cocos2d-x中UI按钮的定义
1.要有两张不同状态的图片 2.定义一个MenuItemSprite的实例,把这两张图的Sprite实例放进MenuItemSprite的实例 3.把MenuItemSprite的实例放进Menu实例 ...
- appium的inspectot使用
前提已安装好appium环境 1. 打开appium-doctor 2. 启动appium-service,点击 inspector 3. 配置手机参数,参数获取参考上篇博客 4. 点击start ...
- mips cfe命令
设置串口参数 setenv -p LINUX_CMDLINE "console=ttyS0,115200 root=mtd4 rw rootfstype=jffs2" 内核启动参数 ...
- unity3d绘画手册-------灯光之反射及各个参数解释
下面说一下Reflection Probe, 大家都知道:当使用标准着色器时,每一个材质都会具有一定程度的镜面反射(specularity)和金属反射 (metalness)属性,在没有强大的硬件来处 ...
- js+css+div的点击后显示或者隐藏
<html ><head><meta charset=utf-8 /><title>JS Bin</title></head> ...
- Qt 定时器Timer使用
From: http://dragoon666.blog.163.com/blog/static/107009194201092602326598/ 1.新建Gui工程,在主界面上添加一个标签labe ...