[LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)
问题
假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格。
如果只允许你完成一次交易(即买入并卖出股票一次),设计一个找出最大利润的算法。
初始思路
和122一样,基于买入与卖出股票的最佳时机III中的分析很容易得出答案。由于只允许进行一次交易,本题更加简单,我们只需按III中的方法不断更新最大利润即可。
class Solution {
public:
int maxProfit(std::vector<int> &prices)
{
return CaculateProfit(prices).profit;
}
private:
struct Profit
{
Profit() : profit(), buyPrice(-), buyDay(), sellDay()
{
}
int profit;
int buyPrice;
int buyDay;
int sellDay;
};
Profit CaculateProfit(std::vector<int> &prices)
{
Profit currentProfit;
Profit maxProfit;
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 < )
{
currentProfit.buyPrice = prices[day];
currentProfit.buyDay = day;
currentProfit.profit = ;
}
if(currentProfit.profit > maxProfit.profit)
{
maxProfit = currentProfit;
}
}
return maxProfit;
}
};
maxProfit
[LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)的更多相关文章
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
问题 假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格. 设计一个算法找出最大的利润值.你可以进行任意多次的交易(即多次的卖出并买入一份股票).你不能在同一时间进行多次交易(即你必须在再次 ...
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- 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. ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- 【HDOJ】2585 Hotel
字符串水题. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 55 cha ...
- COJ 1010 WZJ的数据结构(十) 线段树区间操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...
- ISAP
跑的是比Dinic快辣. 更新:指针版.... #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- C语言的一个关键字——static
Static在C语言里面有两个作用,第一个是修饰变量,第二个是修饰函数. 1.Static修饰变量 按照作用范围的不同,变量分为局部变量和全局变量.如果用static修饰变量,不论这个变量是全局的 ...
- HDU_2012——判断表达式是否都为素数
Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数. I ...
- split
import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; /** * 解析知网文章的页面 ...
- Java体系总结
一.Java SE部分 1.java基础:基础语法:面向对象(重点):集合框架(重点):常见类库API: 2.java界面编程:AWT:事件机制:Swing: 3.java高级知识:Annotatio ...
- ssh远程登录linux live系统
要想ssh远程登录,须要准备两件事:配置同网段IP和开启SSH服务. 因为live系统没有IP,所以首先须要配置IP. 我的live系统是在虚拟机上启动的,宿主IP为192.168.230.1,liv ...
- cocos2d-x 3.0 画图节点——Node
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Hide the common top menu in Ubuntu 12.04
隐藏:1.sudo apt-get autoremove appmenu-gtk appmenu-gtk3 appmenu-qt2.reboot 恢复: 1.sudo apt-get install ...