[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 ...
随机推荐
- Spring MVC中DispatcherServlet工作原理探究
转:http://blog.csdn.net/zhouyuqwert/article/details/6853730 下面类图将主要的类及方法抽离出来,以便查看方便,根据类的结构来说明整个请求是如何工 ...
- sql server知道触发器名如何查看里面代码
以sqlserver2008为例,可以写代码查看,也可以通过SQL Server Manager Studio工具的树形列表查看. 一.代码查看: 直接在SQL Server Manager Stud ...
- javascipt取整数四舍五入
1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...
- C++中new和malloc
1.malloc的工作原理: malloc使用一个数据结构(链表)来维护分配空间链表的构成:分配的空间/上一个空间的数据/下一个空间/空间大小等信息. 对malloc分配的空间不要越界访问,因为 ...
- 佛山Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- VMware workstation12 pro安装Ubuntu14.04LTS过程笔记
由于近期需要加强自己在Linux/C++编程方面的需要,把原来的CentOS6.5格了....在windows8.1系统上重新安装了VMware和Ubuntu... VMware安装Ubuntu的过程 ...
- linux驱动开发之HelloWorld
最近实习,公司项目搞的是平板开发,而我分配的任务是将驱动加载到内核中. 准备工作,必要知识了解:加载有两种方式,一种是动态加载和卸载即模块加载,另一种是直接编译进入内核:Linux内核把驱动程序划分为 ...
- linux创建用户,指定组
本博客不再更新 该文章新链接移步:http://it.lovepet.vip/archives/7/ 一.创建用户: 1.使用命令 useradd 例:useradd test——创建用户test ...
- linux上安装rar解压软件
描述:Linux默认自带ZIP压缩,最大支持4GB压缩,RAR的压缩比大于4GB. -------------------------------------------------- 下载 # wg ...
- mysql inner join,full outer join,left join,right jion
https://sites.google.com/site/349624yu/courses/mysql/mysqldbgjzcx inner join,full outer join,left jo ...