[LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
eg:输入为:[6,4,3,7,3,8,1,4,7,6],则最大收益为7-1=6。
思路一:
先找到数组中的最大数8,在第5天以8卖出时,必须在第0-5天买入,且买入时的价格要为第0-5天的最低价3,此时的收益为8-3=5;如果第0-5天的最低价3并不是整个数组的最低价,
再考虑在第5天之后进行买卖交易,找到第6-9天的最高价7(第8天卖出),第6-8天的最低价1(第6天买入),此时的收益为7-1=6;若要在第8天之后进行买卖交易,则收益为0。所以
最大收益为max(5,6,0),即最大收益为6。
class Solution {
public:
int maxProfit(vector<int> &prices) {
int max_profit=, buy_pos, sell_pos;
vector<int>::iterator it1=prices.begin();
vector<int>::iterator it2=prices.end();
while(it1<it2)
{
vector<int>::iterator sell_it = max_element(it1, it2);
vector<int>::iterator buy_it = min_element(it1, sell_it+);
if(max_profit<(*sell_it-*buy_it))
max_profit = *sell_it-*buy_it;
it1 = sell_it+;
}
return max_profit;
}
};
但是这种方法复杂度高,会出现Time limit exceed。
思路二:
eg:输入为:[6,4,3,7,3,8,1,4,7,6]
分别考虑如果在第0天,第1天,第2天,...,第9天卖出股票时的收益,这9种情况下的收益最大值就是我们要求的最大收益。
要在第i天卖出股票,则必须在第0~i天中的某一天买入,当然选取第0~i天内价格最小的一天买入,记这个最小值为buyPrice_i,则第i天卖出股票时的收益为prices[i]-buyPrice_i;
在求buyPrice_i时可以动态地求,不需要每次从头到i遍历一遍。最后我们要求的maxprofit=max(prices[0]-buyPrice_0, prices[1]-buyPrice_1, ... , prices[9]-buyPrice_9)。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()==)
return ;
int max_profit=, sell_pos;
int min_buy_price=prices[];
for(sell_pos=; sell_pos<prices.size(); sell_pos++) //在sell_pos卖出的话,必须在0-sell_pos之间买入,所以sell_pos当天卖出时的最大收益要求buy_pos在0-sell_pos之间最小
{
if(min_buy_price>prices[sell_pos])
min_buy_price = prices[sell_pos];
if(max_profit<(prices[sell_pos]-min_buy_price))
max_profit = prices[sell_pos]-min_buy_price;
}
return max_profit;
}
};
这种思路可以accept。
[LeetCode OJ] Best Time to Buy and Sell Stock I的更多相关文章
- LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
随机推荐
- BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线
1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 892 Solved: ...
- HDU-4925 Apple Tree
http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others) ...
- Linux学习笔记2——Linux中常用文件目录操作命令
ls 显示文件和目录列表 -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 mkdir 创建目录 -p 父目录不存在情况下先生成父目录 cd 切换目录 touch 生成一个空文件 e ...
- 【转】OpenGL基础图形编程(一)
原文:http://blog.chinaunix.net/uid-20638550-id-1909183.html 分类: 一.OpenGL与3D图形世界 1.1.OpenGL使人们进入三维图形世界 ...
- Supervisord管理
原文地址:http://blog.csdn.net/fyh2003/article/details/6837970 学习笔记 Supervisord可以通过sudo easy_install supe ...
- JavaScript高级程序设计1.pdf
第一遍通读的时候对JavaScript一点都不了解翻了一整本书仅仅是眼熟的几个名词,现在会写一些js效果了,对程序有一定的认知,又要在读一遍,再加深一些了解,当然以后还会有第三遍第四遍,等完全啃透了这 ...
- centos平台openstack spice配置
配置过程只涉及控制节点(192.168.209.11)和计算节点(192.168.209.31),根据情况修改为实际环境的IP地址. 修改控制节点 安装软件包 yum install spic ...
- CAS学习笔记(三)—— SERVER登录后用户信息的返回
一旦CAS SERVER验证成功后,我们就会跳转到客户端中去.跳转到客户端去后,大家想一想,客户端总要获取用户信息吧,不然客户端是怎么知道登录的是哪个用户.那么客户端要怎么获取用户信息呢? 其实验证成 ...
- UVa1606 UVaLive3259 FZU1309 HDU1661 POJ2280 ZOJ2390 Amphiphilic Carbon Molecules
填坑系列 考虑所有经过两个点的直线,一定有最优解. 再考虑确定一个点,按极角顺序枚举所有直线,从而O(1)转移信息. 还有代码实现技巧 #include<cstdio> #include& ...
- CMDLINE的解析
在linux的config文件中有一个特殊的宏定义CMDLINE,以前也一直在使用这个宏的参数,但是真正这个宏的解析和使用却不怎么明确.这次有机会多对它有些了解,不妨把这个浅显的认识说出来,记下来. ...