[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 ...
随机推荐
- Android样式的编写格式
<?xml version="1.0" encoding="utf-8"?> <resources> <style name=&q ...
- 简要介绍EF(实体框架)
原文地址:http://wenku.baidu.com/link?url=eutYH1QWA9y7fnxsxT9pZfJTPfa36nCI4R3Ub8Y4ybAVSgmXzEnXHwUj-GPFinn ...
- Codeforces Round #316 (Div. 2) D、E
Problem D: 题意:给定一棵n个点树,每个点有一个字母,有m个询问,每次询问某个节点x的子树中所有深度为k的点能否组成一个回文串 分析:一堆点能组成回文串当且仅当数量为奇数的字母不多于1个,显 ...
- mysql 索引相关知识
由where 1 =1 引发的思考 最近工作上被说了 说代码中不能用 where 1=1,当时觉得是应该可以用的,但是找不到什么理据, 而且mysql 语句优化这方面确实很薄弱 感觉自己mysql ...
- java中的“包”与C#中的“命名空间
原文地址:http://www.cnblogs.com/lidabo/archive/2012/12/15/2819865.html Package vs. Namespace 我们知道,重用性(re ...
- (转)UIWebView与JavaScript的那些事儿
UIWebView是IOS SDK中渲染网面的控件,在显示网页的时候,我们可以hack网页然后显示想显示的内容.其中就要用到javascript的知识,而UIWebView与javascript交互的 ...
- 转载:mybatis和hibernate 解析
第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...
- UserManageSys
JSP部分: err.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...
- [每日一题] 11gOCP 1z0-052 :2013-09-3 Because of frequent checkpoints...........................A30
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11022433 正确答案:BC 这里我就偷一下懒了,引用 http://www.itpub. ...
- Swift2.0下UICollectionViews拖拽效果的实现
文/过客又见过客(简书作者)原文链接:http://www.jianshu.com/p/569c65b12c8b著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文UICollecti ...