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.

click to show more practice.

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.

看见这题的第一想法就是决不能用暴力穷举。

然后应该会想到简单的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的更多相关文章

  1. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  2. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  3. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  4. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  5. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. [leetcode]Maximum Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...

  7. 53. [LeetCode] Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  8. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

  9. LeetCode Maximum Subarray (最大子段和)

    题意: 给一个序列,求至少含一个元素的最大子段和? 思路: 跟求普通的最大子段和差不多,只不过需要注意一下顺序.由于至少需要一个元素,所以先将ans=nums[0].接下来可以用sum求和了,如果小于 ...

随机推荐

  1. 【 Linux 】单台服务器上并发TCP连接数

    单台服务器上并发TCP连接数    问题:一台服务器到底能够支持多少TCP并发连接呢? 1. 文件描述符限制:    对于服务器来说,每一个TCP连接都要占用一个文件描述符,一旦文件描述符使用完,新的 ...

  2. m个苹果放在n个盘子里面有多少种放法?(动态规划)

    m个苹果放在n个盘子里面有多少种放法?(动态规划) 实现代码如下: #include <iostream> using namespace std; int s(int m ,int n) ...

  3. R笔记4:ggplot绘制商务图表--玫瑰图

    我们说Excel有难度的图表,可以考虑ggplot2是否更方便,本帖的例子就是用ggplot做玫瑰图. Excel做玫瑰图有一定难度,可以使用雷达图或圆环图来构建,我的博客上曾有多个帖子讨论这个,见 ...

  4. 【面试】hibernate n+1问题

    Hibernate中常会用到set,bag等集合表示1对多的关系, 在获取实体的时候就能根据关系将关联的对象或者对象集取出, 还可以设定cascade进行关联更新和删除.这不得部说hibernate的 ...

  5. 【转】MFC WM_CTLCOLOR 消息

    WM_CTLCOLOR消息用来完成对EDIT, STATIC, BUTTON等控件设置背景和字体颜色, 其用法如下: 1.首先在自己需要设置界面的对话框上点击右键-->建立类向导-->加入 ...

  6. dm8127-内存分配

    在前天一直完车辆捕获算法和车牌识别算法之后,算法移植告一段落,五月份以来,总算有点欣慰了,可是cmos采集视频有点问题,主要是前端采集不是我接手,嵌入式部门的小宋和小李负责,据说是20多万没了图纸,防 ...

  7. asp.net管线

  8. 3d引擎列表

    免费引擎 Agar - 一个高级图形应用程序框架,用于2D和3D游戏. Allegro library - 基于 C/C++ 的游戏引擎,支持图形,声音,输入,游戏时钟,浮点,压缩文件以及GUI. A ...

  9. Mac 下,修改plist文件

    /usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName $DISPLAY_NAME" "${PROJECT_TEMP_D ...

  10. Windows网络编程Internet Gopher了解下

    Gopher:中文译“地鼠”,是迪士尼卡通人物之一(谷佛). 英文原义:The Internet Gopher Protocol 中文释义:(RFC-1436)网际Gopher协议 该系统是在明尼苏达 ...