[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 ...
随机推荐
- TortoiseSVN优化设置
设置log messages的字体 TortoiseSVN默认的字体太小了,看着难受: 可以在Settings > 左侧目录树General > Dialogs 1中进行设置: 使用Bey ...
- 使用python实现最优化问题
最优化问题 1.无约束的最优化问题 所谓的无约束优化问题指的是一个优化问题的寻优可行集合是目标函数自变量的定义域,即没有外部的限制条件.例如,求解优化问题 [ \begin{array}{rl} \t ...
- 转 :hdoj 4857 逃生【反向拓扑】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- CentOS7.0分布式安装HADOOP 2.6.0笔记-转载的
三台虚拟机,IP地址通过路由器静态DHCP分配 (这样就无需设置host了). 三台机器信息如下 - 1. hadoop-a: 192.168.0.20 #master 2. ha ...
- 网络子系统43_ip选项预处理
//选项格式: // 1.type中指示该选项在分片时是否需要被拷贝 // 2.ptr从1算起,1为type的位置 // 3.len不包括type字段,其余都包括(len,ptr,选项内容) //ty ...
- 【POJ3182】The Grove BFS 最短路径周围
意甲冠军:给定一个N*M图.,间'X'代表树木(树木必须汇集到森林,非分离),然后,'.'它代表的空间.'*'它代表的起点.现在它需要从起点.一圈,最后回到起点,所经过最少点数. 题目中给的'+'就是 ...
- Qt 学习之路:QML 和 QtQuick 2
前面我们已经了解了 Qt 的一部分内容.这部分内容全部集中在 C++ 方面.也就是说,至今为止我们的程序都是使用 C++ 语言完成的.这在 Qt 5 之前的版本中是唯一的途径.不过,自从 Qt 5 开 ...
- xml写代码
#include <QDomDocument> #include <QtGui> int main(int argc, char *argv[]) { QApplication ...
- [转] json in javascript
JavaScript is a general purpose programming language that was introduced as the page scripting langu ...
- rabbitMQ实战(一)---------使用pika库实现hello world
rabbitMQ实战(一)---------使用pika库实现hello world 2016-05-18 23:29 本站整理 浏览(267) pika是RabbitMQ团队编写的官方Pyt ...