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. 利用JavaScript计算引擎进行字符串公式运算

    1.通过js计算引擎计算(java自带) 2.计算公式除了支持基本的方法之外还支持简单js脚本分支计算 3.通过设定map传入参数 4.默认返回最后一个计算结果,如果需返回特定值,将变量补写在公式最后 ...

  2. poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。

    /** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...

  3. hadoop 1.1.2和 hive 0.10 和hbase 0.94.9整合

    今天弄了一下hive0.10和hbase0.94.9整合,需要设置的并不多,但是也遇到了一些问题. 1.复制jar包 拷贝hbase-0.94.9.jar,zookeeper-3.4.5.jar,pr ...

  4. linux取随机数shell版本

    #!/bin/bash aa=$(-) ..} do useradd $i echo $aa|passwd --stdin $i echo "${i} ${aa}" >> ...

  5. ★ java删除代码注释

    package com.witwicky.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java ...

  6. Java-ThreadLocal,Java中特殊的线程绑定机制

    在DRP项目中,我们使用了ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection向下传递(传递connection的目的是由于jdbc事务要求确保使用同一个 ...

  7. 上手并过渡到PHP7(2)——必须传递int, string, bool参数?没问题

    Type hints, Type safe 泊学实操视频 泊学原文链接PHP 7中最引人注目的新特性之一,无疑是Scalar type hints.我们可以在函数参数和返回值中使用scalar typ ...

  8. CSS控制显示图片的一部分

    使用情形:防止反复请求图片资源,我们经常采用一张图片多种效果或内容显示. 假设我有纸张竖直方向的一张图片,竖直y轴方向分别是字母:A,B,C.... 现在分别要显示A.B.C 等字母,我们的CSS可以 ...

  9. 【转】C# 二维码生成

    /// <summary> /// 含有QR码的描述类和包装编码和渲染 /// </summary> public class QRCodeHelper { /// <s ...

  10. Android Studio怎样查看资源或者函数在哪些类中被引用

    很多人在做完Keymap匹配到Eclispe快捷键后,发现查看资源或者函数在哪些地方被引用的快捷键"Ctrl+Shift+G"不灵 了.你选中某个函数后,使用这个快捷键.发现仅仅会 ...