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求和了,如果小于 ...
随机推荐
- OpenResty(Nginx+Lua)开发入门
Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...
- d3js网络拓扑关系特效可视化展现
d3js拓扑关系特效可视化展现 在上一篇d3js文档http://www.cnblogs.com/juandx/p/3959900.html中讲了简单的d3js方法和效果,现在我做一个完整的演示,使用 ...
- [从jQuery看JavaScript]-注释(comments)
jQuery片段: /*! * jQuery JavaScript Library v1.3.2 * http://jquery.com/ * * Copyright (c) 2009 John Re ...
- 《FPGA全程进阶---实战演练》第二章之硬件平台的搭建
学习FPGA,多多少少应该要懂得硬件电路的设计,这样不单单增加了自己的技能,而且还能够对FPGA的硬件实现有更好的了解. 1 模块划分 对于一个基本的FPGA硬件平台,常用的几个电路部分:(1)电源电 ...
- SQLServer 跨库查询实现方法
不使用链接的服务器名,而提供特殊的连接信息,并将其作为四部分对象名的一部分 本文给出一条 SQL 语句用于展示在同一名服务器上,不同的数据库间查询,注意当前连接用户要对两个库都有权限SQL Serve ...
- Java 经典问题
九种基本类型及封装类 基本类型 boolean byte char short int long double void 二进制位数 1 8(一字节) 16(2字节) 16(2字节) 32(4字节) ...
- e578. Setting the Clipping Area with a Shape
This example demonstrates how to set a clipping area using a shape. The example sets an oval for the ...
- JAVA 多线程机制(二)
主要内容 1.理解线程的并发性 2.线程的同步 3.线程的常用方法 上一章中由于线程的并发性导致了多线程的执行总是会出现一些问题..线程的并发性是程序员不可控制 的,也是不可避免的,线程的并发性往 ...
- Android Looper详解
在Android下面也有多线程的概念,在C/C++中,子线程可以是一个函数, 一般都是一个带有循环的函数,来处理某些数据,优先线程只是一个复杂的运算过程,所以可能不需要while循环,运算完成,函数结 ...
- CentOS下 Uptime 命令
对于一些人来说系统运行了多久是无关紧要的,但是对于服务器管理员来说,这是相当重要的信息.服务器在运行重要应用的时候,必须尽量保证长时间的稳定运行,有时候甚至要求零宕机.那么我们怎么才能知道服务器运行了 ...