Best Time to Buy and Sell Stock I

  题目链接

  题目要求:

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

  If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

  这道题的本质在于找出一个数组中任意两个数(序号大的数减去序号小的数)的最大差值。我们发现数组某一段的最大差值信息是可以保存的,并作为下一段的初始数值。具体的递推式如下:

dp[i] = max(dp[i - ], prices[i] - minPrice);

  程序也是比较简单:

 class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == )
return ; vector<int> dp(sz, );
int minPrice = prices[];
for(int i = ; i < sz; i++)
{
dp[i] = max(dp[i - ], prices[i] - minPrice);
if(minPrice > prices[i])
minPrice = prices[i];
} return dp[sz - ];
}
};

  另一个很有代表性的解法如下(参考自一博文):

  按照股价差价构成一个新的数组,即prices[1]-prices[0], prices[2]-prices[1], prices[3]-prices[2], ..., prices[n-1]-prices[n-2],这样我们的问题就转变成求最大的连续子段和。具体如何高效求解最大连续子段和请参考自我之前写过的一篇博文

Best Time to Buy and Sell Stock II

  LeetCode没有提供题目,源自其他博文

  题目要求:

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

  Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

  这道题的解法更加简单,我们这需要先构建一个股价差数组,然后把该数组中所有大于0的数相加就能够得到结果。具体程序引自同一篇博文

 class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = prices.size();
if(len <= )return ;
int res = ;
for(int i = ; i < len-; i++)
if(prices[i+]-prices[i] > )
res += prices[i+] - prices[i];
return res;
}
};

Best Time to Buy and Sell Stock III

  题目链接

  题目要求:

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

  Design an algorithm to find the maximum profit. You may complete at most two transactions.

  Note:
  You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

  因为只能完成最多两次交易,而且第二次交易只能在第一次交易完成后进行,因此我们可以先按第一题方法,按顺序求得最大和数组,再逆序求得最大和数组,最后再跟据这两个数组求得最大收益。具体程序如下:

 class Solution {
public:
int simpleMaxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == )
return ; vector<int> dp(sz, );
int minPrice = prices[];
for(int i = ; i < sz; i++)
{
dp[i] = max(dp[i - ], prices[i] - minPrice);
if(minPrice > prices[i])
minPrice = prices[i];
} vector<int> dp_rev(sz, );
int maxPrice = prices[sz - ];
for(int i = sz - ; i > -; i--)
{
dp_rev[i] = min(dp_rev[i + ], prices[i] - maxPrice);
if(maxPrice < prices[i])
maxPrice = prices[i];
} vector<int> maxGain(sz, );
maxGain[] = ;
for(int i = ; i < sz; i++)
{
maxGain[i] = max(maxGain[i - ], dp[i] - dp_rev[i]);
} return maxGain[sz - ];
} int maxProfit(vector<int>& prices) {
return simpleMaxProfit(prices);
}
};

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV的更多相关文章

  1. Best Time to Buy and Sell Stock I II III IV

    一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...

  2. LeetCode:Best Time to Buy and Sell Stock I II III

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

  3. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  4. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

  5. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  6. Best Time to Buy and Sell Stock I,II,III [leetcode]

    Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...

  7. Best Time to Buy and Sell Stock I II III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  8. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

  9. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

随机推荐

  1. 一个貌似比较吊的递归转换为loop--总算成功了.

    class Stack(object): """ A class to hold arguements and state data. """ ...

  2. GIF动态图制作

    GIF动态图制作 博客写了也有一阵了,一直好奇大牛的博客里demo的动态图是怎么做的,今天抽空研究了一下,找了一个软件,以后再发现有好的工具再继续推荐 GIF制作工具--LICEcap 效果要比下面的 ...

  3. 在电脑上安装Linux操作系统

    1硬件需求 A 一台电脑 B 一个优盘 2软件需求 A制作优盘启动盘的软件PowerISO BLinux操作系统的镜像文件 3安装PowerISO,并使用PowerISO A安装PowerISO B插 ...

  4. 程序员的自我修养-----Java开发的必须知道的几个注意点

    1. 将一些需要变动的配置写在属性文件中 比如,没有把一些需要并发执行时使用的线程数设置成可在属性文件中配置.那么你的程序无论在DEV环境中,还是TEST环境中,都可以顺畅无阻地运行,但是一旦部署在P ...

  5. Jstorm与RocketMQ整合

    如果是经常关注阿里巴巴的朋友们,看到我这篇博客的题目,就知道我在参加今年的中间件比赛. 好了,废话不说,开始了. 首先我们知道,rocketmq的consumer有两种,一种是DefaultMQPus ...

  6. 1.关于QT中json数据处理和密码md5加密

     新建一个Qt空项目 17Json.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += widgets gui MyWidget ...

  7. AndroidManifest.xml中的application中的name属性

    被这个不起眼的属性折磨了一天,终于解决了. 由于项目需要,要合并两个android应用,于是拷代码,拷布局文件,拷values,所有的都搞定之后程序还是频频崩溃,一直没有找到原因,学android时间 ...

  8. 06 intent flag三种属性

    flag属性可以看做和写在清单文件中的启动模式一样 但效果有一定差别 1,FLAG_ACTIVITY_SINGLE_TOP:启动模式里的SingleTop一致  如果X启动模式设置为FLAG_ACTI ...

  9. [C++学习历程]Visual Studio 2010 中文旗舰版 安装

    作者: 苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/19765441 要开始学习C++了,先装个开发环境吧,没有选择最新的2 ...

  10. TCP/IP入门(2) --网络层

    /** 本篇博客由 126(127不可用) 2^24 -2 B 2^14 -1 128.1 191.255 2^16 -2 C 2^21 -1 192.0.1 223.255.255 2^8 -2 D ...