LeetCode(53) Maximum Subarray
题目
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.
分析
最大子序列和的问题,这道题我写出的是O(n)的算法,属于简单的动态规划,根据题目后面的more practice说明该题目还有更优的分治法解决思路。
AC代码-动态规划
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.empty())
return 0;
//求数组的长度
int len = nums.size();
//将最大和赋值为首元素值,temp记录临时子序列和
int maxSum = nums[0], temp = 0;
for (int i = 0; i < len; i++)
{
temp += nums[i];
//若元素和大于当前最大和
if(temp > maxSum)
{
maxSum = temp;
}//else
//若子系列和为非正数,则从下一个元素重新记录
if (temp <= 0)
{
temp = 0;
}
}//for
return maxSum;
}
};
AC代码-分治法
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.empty())
return 0;
//求数组的长度
int len = nums.size();
return Divide(nums , 0 , len-1);
}
//分治法
int Divide(const vector<int> &nums, int lhs, int rhs)
{
if (lhs == rhs)
return nums[lhs];
int mid = (lhs + rhs) / 2;
int leftMaxSum = Divide(nums, lhs, mid);
int rightMaxSum = Divide(nums, mid + 1, rhs);
int lsum = INT_MIN;
int rsum = INT_MIN;
int temp = 0;
for (int i = mid; i >= lhs; i--)
{
temp += nums[i];
if (temp > lsum)
lsum = temp;
}
temp = 0;
for (int i = mid + 1; i <= rhs; i++)
{
temp += nums[i];
if (temp > rsum)
rsum = temp;
}
//跨越中点的最大子序列和
temp = lsum + rsum;
return std::max(temp, std::max(leftMaxSum, rightMaxSum));
}
};
LeetCode(53) Maximum Subarray的更多相关文章
- LeetCode(152) Maximum Product Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- Leetcode(53)-最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...
- (LeetCode 53)Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode(53):最大子序和
Easy! 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: ...
- LeetCode(164)Maximum Gap
题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...
- LeetCode(104) Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- 【LeetCode算法-53】Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- python之logging模块简单用法
前言: python引入logging模块,用来记录自己想要的信息.print也可以输入日志,但是logging相对print来说更好控制输出在哪个地方.怎么输出以及控制消息级别来过滤掉那些不需要的信 ...
- Hdu 5336 XYZ and Drops (bfs 模拟)
题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
- Poj 2947 widget factory (高斯消元解同模方程)
题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...
- [ZPG TEST 115] 种树【差分约束】
4. 种树 (trees.pas/c/cpp) [问题描述] 一条街的一边有几座房子.因为环保原因居民想要在路边种些树.路边的地区被分割成块,并被编号为1..n.每个块的大小为一个单位尺寸并最多可种一 ...
- openstack知识---hypervisor
hypervisor Hypervisor是一种运行在物理服务器和操作系统之间的中间软件层,可允许多个操作系统和应用共享一套基础物理硬件,因此也可以看作是虚拟环境中的“元”操作系统,它可以协调访问服务 ...
- DB buffer bussy wait 分析一例
####sample 1: DB层分析OI DB层分析OI的信息如下: 1. 异常时间段, Logical reads:/ Physical reads/ Physical write 指标都低于 ...
- 专 linux命令之set x详解
set -x与set +x指令 用于脚本调试.set是把它下面的命令打印到屏幕 set -x 是开启 set +x是关闭 set -o是查看 (xtrace),set去追中一段代码的显示情况. 执 ...
- sed简单脚本练习
sed脚本的执行顺序可以简记为:Read,Execute,Print,Repeat(读取,执行,打印,重复)简称REPR 分析脚本的执行顺序: l 读取一行到模式空间(sed内部的一个临时缓存,用于存 ...
- [BZOJ1085][SCOI2005]骑士精神 搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 大的思路是迭代加深搜索,我们加一个明显的剪枝,当棋盘中位置不对的骑士的数目加上已经走 ...
- esp8266 串口通讯
1.发送 调用uart_init(115200,115200);初始化串口,波特率设置为115200.前面一个是设置uart0的波特率.后面一个是设置.uart的波特率 然后就可以使用uart0_tx ...