问题

假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格。

设计一个算法找出最大的利润值。你可以进行任意多次的交易(即多次的卖出并买入一份股票)。你不能在同一时间进行多次交易(即你必须在再次买入股票之前卖出当前的股票)

初始思路

有了在买入与卖出股票的最佳时机III中的分析,这题就很容易得出答案了。像III中那样,我们使用3个变量来纪录利润:

Profit currentProfit;
Profit maxProfit;
int totalProfit;

currentProfit表示当前日期卖出的利润,maxProfit表示本次交易中的最大利润,totalProfit记录总利润。由于可以进行任意多次的交易,我们应当在发现利润与前一天利润相比变小时就进行一次交易,从而保证利润最大。即每当发现currentProfit小于maxProfit时,将maxProfit中的利润加到totalProfit中,并重置currentProfit和maxProfit。由于maxProfit重置为0,在价格持续减小的情况下并不会影响totalProfit的值。如[3,2,1]这种情况,我们进行了两次增加totalProfit并重置的操作,但最后结果仍然是正确答案0。

一个需要注意的地方是[1,4]这种情况-利润并没有开始变小但是已经到了最后一天。为了处理这种情形,我们需要在对数组遍历完毕后再把maxProfit加到totalProfit一次。

最后的完整代码如下,通过Small和Large的测试:

     class Solution
{
public:
int maxProfit(std::vector<int> &prices)
{
Profit currentProfit;
Profit maxProfit;
int totalProfit = ; for(int day = ; day < prices.size(); ++day)
{
if(currentProfit.buyPrice == -)
{
currentProfit.buyPrice = prices[day];
currentProfit.buyDay = day;
continue;
} currentProfit.profit = prices[day] - currentProfit.buyPrice;
currentProfit.sellDay = day; if(currentProfit.profit > maxProfit.profit)
{
maxProfit = currentProfit;
}
else if(currentProfit.profit < maxProfit.profit)
{
totalProfit += maxProfit.profit; currentProfit.buyPrice = prices[day];
currentProfit.buyDay = day;
currentProfit.profit = ; maxProfit.profit = ;
}
} if(maxProfit.profit != )
{
totalProfit += maxProfit.profit;
} return totalProfit;
} private:
struct Profit
{
Profit() : profit(), buyPrice(-), buyDay(), sellDay()
{
} int profit;
int buyPrice;
int buyDay;
int sellDay;
};
};

maxProfit

[LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)的更多相关文章

  1. [Leetcode 122]买股票II 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 ...

  2. [Swift]LeetCode122. 买卖股票的最佳时机 II | 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 al ...

  3. 【LeetCode】【Python解决问题的方法】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 al ...

  4. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

  5. 31. leetcode 122. Best Time to Buy and Sell Stock II

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

  6. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  7. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  8. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  9. 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II

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

随机推荐

  1. 2015第29周五AOP

    AOP使用场景 AOP用来封装横切关注点,具体可以在下面的场景中使用: Authentication 权限 Caching 缓存 Context passing 内容传递 Error handling ...

  2. 2013第39周一Web打印

    2013第39周一Web打印 项目中遇到了Java Web打印问题,简单调用IE浏览器的打印不能完全满足要求,于是就搜集了Web打印相关的主题,简单汇总一下.web打印难点在分页.页面纸张设置,页眉页 ...

  3. HDU_1426——数独问题,DFS

    Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行 ...

  4. ESB 企业服务总线

    整理的OSChina 第 38 期高手问答 —— ESB 企业服务总线,嘉宾为@肖俊_David . @肖俊_David 恒拓开源架构师,热衷于JAVA开发,有多年的企业级开发经验.曾参和设计和开发基 ...

  5. <Win32_9>有意思的程序——抓取屏幕

    Win32学了一段时间了,跟着杨老师的脚步,准备学习MFC,因此最近几天在复习C++,于是发现有将近一周没写博文了…… 今天来写一个较为简单.但是比较有意思的东西 不知大家在理发店理发的时候注意到一个 ...

  6. 跟我一起学extjs5(22--模块Form的自己定义的设计)

    跟我一起学extjs5(22--模块Form的自己定义的设计)         前面几节完毕了模块Grid的自己定义,模块Form自己定义的过程和Grid的过程类似,可是要更复杂一些.先来设计一下要完 ...

  7. Js 插件修改及优化总结

    1. ajaxfileupload 上传插件版本问题以及数据处理问题 参考链接: http://liwx2000.iteye.com/blog/1540321 现在大家至少也在用jquery1.9以上 ...

  8. DataSet与DataTable的区别

    DataSet:数据集.一般包含多个DataTable,用的时候,dataset["表名"]得到DataTable DataTable:数据表. 一: SqlDataAdapter ...

  9. 关于eclipse几种插件的安装方法

    首先这里的安装方法按文件类型和安装方式来分 首先介绍按不同安装方式来分: 1.利用eclipse自带插件安装功能: 以jode插件为例,启动eclipse,help -> Software Up ...

  10. 触发器内insert,delete,update判断执行不同的内容

    create trigger tr_aon afor insert,update,delere asbegin IF EXISTS(SELECT 1 FROM inserted) AND NOT EX ...