[LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
问题
假设你有一个数组,其中的第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)的更多相关文章
- [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 ...
- [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 ...
- 【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 ...
- 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. ...
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- 【刷题-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 ...
随机推荐
- iOS越狱开发手记 - iOS9.3 dyld_decache不能提取arm64的dyld的解决方法
参考以下文章 http://iosre.com/t/when-dyld-decache-fails-on-dyld-shared-cache-arm64-dsc-extractor-saves-our ...
- HDOJ 1302(UVa 573) The Snail(蜗牛爬井)
Problem Description A snail is at the bottom of a 6-foot well and wants to climb to the top. The sna ...
- Java正则表达式详解教程
public class Test { private static Scanner scanner; public static void main(String[] args) { scanner ...
- ubuntu下安装pdo扩展
ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装 步聚,在终端输入以下命令 1.pecl search pdo 2.sudo pecl install pdo 当出现E ...
- #python-dateutil下载地址
http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-dateutil
- 使用 AtomicInteger 进行计数(java多线程优化)
通常,在我们实现多线程使用的计数器或随机数生成器时,会使用锁来保护共享变量.这样做的弊端是如果锁竞争的太厉害,会损害吞吐量,因为竞争的同步非常昂贵. volatile 变量虽然可以使用比同步更低的成本 ...
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40955607 作者:毛星云(浅墨) ...
- C++中数组初始化
#include<iostream>using std::cout;using std::endl;int arr1[5];int arr2[5] = {1,3,5};int main() ...
- js中setTimeout/setInterval定时器用法示例
js中setTimeout(定时执行一次)和setInterval(间隔循环执行)用法介绍. setTimeout:在指定的毫秒数后调用指定的代码段或函数:setTimeout示例代码 functio ...
- iOS之AFN错误代码1016(Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable)
请参考这篇博客:点击查看