Best Time to Buy and Sell Stock

原创 2016年05月31日 10:18:53

这是求最大子数组的问题,编码不难,有穷举、分治、动态规划三种方法。引起我兴趣的是分治的子问题划分和动态规划的最优子结构的证明。


分治:

从数组的中间分开,最大子数组出现的三种情况:

1、在左半部分

2、在右半部分

3、跨越中点(求跨越中点的子数组等价于求从中间元素开始往左累加的最大值 + 从中间元素开始往右累加的最大值


动态规划:

对于所有数组元素进行划分,如果加上该元素之前temp>0且temp+a[i]<0,那么该元素a[i]是一个边界,这样,数组会形成好多段,每段结束元素都满足temp>0且temp+a[i]<0.所以我们能得到多个划分块a[p..q],每个划分快的和是负值,划分块有这样的性质,对任意p<=i<q,显然,sum(a[p..i])>=0且sum(a[i..q])<0;根据划分块性质,容易证明,只要子数组横跨多个划分快,其求和值必定小于某个单独的划 分快中的数组求和

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Best Time to Buy and Sell Stock III

class Solution { public: int maxProfit(vector &prices) { int n=prices.size(); if(n==0) re...

LeetCode:Best Time to Buy and Sell Stock(买卖股票)系列

121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of ...

[Leetcode]122. Best Time to Buy and Sell Stock II

122. Best Time to Buy and Sell Stock IISay you have an array for which the i-th element is the price...

LeetCode----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...

leetcode Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were...

Best Time to Buy and Sell Stock III(leetcode)

题目: Say you have an array for which the ith element is the price of a given stock on day i. ...

leetcode Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III  Say you have an array for which the ith element is the...

LeetCode OJ-121. Best Time to Buy and Sell Stock(股票问题)

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri...

[LeetCode]123. Best Time to Buy and Sell Stock III

[LeetCode]123. Best Time to Buy and Sell Stock III题目描述思路动态规划 举例来说 index 0 1 2 3 4 5 ...

Leetcode Best Time to Buy and Sell Stock IV

Leetcode Best Time to Buy and Sell Stock IV,本题主要是找出最大值与买卖次数的关系,很快可以求得结果。卖买次数与k的关系分析 首先我们知道当次数不限时,只需要...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)