问题

假设你有一个数组,其中的第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. 算法导论(第三版)Problems2(归并插入排序、数列逆序计算)

    讨论内容不说明,仅提供相应的程序. 2.1:归并插入排序θ(nlgn) void mergeInsertionSort(int a[], int l, int r, int k) { int m; & ...

  2. Java虚拟机内存优化实践

    前面一篇文章介绍了Java虚拟机的体系结构和内存模型,既然提到内存,就不得不说到内存泄露.众所周知,Java是从C++的基础上发展而来的,而C++程序的很大的一个问题就是内存泄露难以解决,尽管Java ...

  3. JS-string内置对象

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

  4. iOS UITableView 修改滚动条颜色 默认选中第一条

    //隐藏 self.tableView.showsVerticalScrollIndicator = NO; //修改颜色 self.tableView.indicatorStyle=UIScroll ...

  5. Cache-control

    网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...

  6. 最全SpringMVC具体演示样例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先.导入SpringMVC须要的jar包. 2.加入Web.xml配置文件里关于SpringMVC的配置 <!--conf ...

  7. android 多项对话框

    在main.xml中 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  8. Android 字体设置

    Android 对中文字体支持很不好~~ 需要加入相应的字体库 (1)创建布局Layout //创建线性布局 LinearLayout linearLayout=newLinearLayout(thi ...

  9. Movie播放Gif,完美实现屏幕适配

    android播放gif  我研究过3种 第一 :GifView支持android播放gif,效果是 先加载第一帧,然后慢慢加载完其他的针,这样效果视觉很不好,是从模糊到清晰的过程:第二:是流行的把g ...

  10. Android 基于Netty的消息推送方案之概念和工作原理(二)

    上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...