Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)
题目描述
已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益
测试样例
Input: [7, 1, 5, 3, 6, 4]
Output: 5
最大收益 = 6-1 = 5 (不是7-1 = 6,因为先买后卖,7买,1买亏了6)
Input: [7, 6, 4, 3, 1]
Output: 0
最大收益为0
详细分析
初看非常简单,遍历数组,每次选择一个元素,找到这个元素后面的数组的最大值,计算差值,和当前最大收益比较即可,就像这样:
[7,1,5,3,6,4] 当前7,后面最大6,收益-1
[7,1,5,3,6,4] 当前1,后面最大6,收益5
[7,1,5,3,6,4] 当前5,后面最大6,收益1
如此继续找到即可。
不过这种方法会超时,稍微改变一下,现在不止保持最大收益,还保存最低价格,如果当天价格更低,就刷新最小价格(买),同时如果当天价格减去最小价格的收益最大,就刷新最大价格(卖),过程如下:
[7,1,5,3,6,4] minPrice=INT_MAX,maxProfit=0
=> minPrice=7,maxProfit=0
[7,1,5,3,6,4] minPrice=7,maxProfit=0
=> minPrice=1,maxProfit=0
[7,1,5,3,6,4] minPrice=1,maxProfit=0
=> minPrice=1,maxProfit=4
[7,1,5,3,6,4] minPrice=1,maxProfit=4
=> minPrice=1,maxProfit=4
[7,1,5,3,6,4] minPrice=1,maxProfit=4
=> minPrice=1,maxProfit=5
[7,1,5,3,6,4] minPrice=1,maxProfit=5
=> minPrice=1,maxProfit=5
算法实现
- 方法1:Time Limit Exceeded
class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0;
for(auto start=prices.begin();start!=prices.end();start++){
int buy = *start;
int sell = *std::max_element(start,prices.end());
if((sell - buy)>profit){
profit = sell-buy;
}
}
return profit;
}
};
- 方法2: Accepted
class Solution{
public:
int maxProfit(vector<int> &prices){
int profit = 0;
int minBuy = std::numeric_limits<int>::max();
for(int i=0;i<prices.size();i++){
//buy it if current price is less than minimum prices yet
if(prices[i]<minBuy){
minBuy = prices[i];
}
//sell it if current profit got optimally
if((prices[i]-minBuy)>profit){
profit = prices[i]-minBuy;
}
}
return profit;
}
};
Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)的更多相关文章
- 30. leetcode 121. Best Time to Buy and Sell Stock
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 、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] 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 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 ...
- Java for 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 121. Best Time to Buy and Sell Stock ----- java
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Python [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 ...
- [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 121. Best Time to Buy and Sell Stock (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- Spring MVC配置详解(2)---bai
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...
- PHP类(四)-类的继承
类的继承就是从已经定义的类中继承数据,也可以重新定义或者加入一些数据. 被继承的类称为父类,基类,超类 继承的类称为子类,派生类 在PHP中只能使用单继承,也就是一个类只能从一个类中继承数据,但是一个 ...
- 2015.9.28 不能将多个项传入“Microsoft.Build.Framework.ITaskItem”类型的参数 问题解决
方法是:项目->属性->安全性->启用ClickOnce安全设置, 把这个复选框前面的勾去掉就可以了.
- MessageBox如何输出整数
int cx=10;CString s;s.Format(_T("整数是:%d"),cx);MessageBox(s);
- Oracle 11g oracle 用户密码过期问题 (ZT)
http://www.blogjava.net/freeman1984/archive/2013/04/23/398301.html Oracle 11g 之前默认的用户时是没有密码过期的限制的,在O ...
- mysql中表触发器的简单使用
以前有接触过触发器,但没有亲自写过,所以在这里简单写个例子: 功能为:两张表t_test,t_emp;给t_test添加触发器让t_test每插入一条数据,就给t_emp中num字段值增1: 1,建立 ...
- C++——static
1.第一条也是最重要的一条:隐藏.(static函数,static变量均可) 所有未加static前缀的全局变量和函数都具有全局可见性:加static前缀的全局变量和函数只有有局部可见性: //a.c ...
- cocos2dx中的内存管理方式
转载:http://www.cocoachina.com/bbs/read.php?tid=195219 今天看了一下cocos2dx的内存管理机制,有些地方不太好理解搞了挺长的时间,现在感觉自己理解 ...
- Linux共享对象之编译参数 -fPIC
转载自:https://www.cnblogs.com/cswuyg/p/3830703.html 在Linux系统中,动态链接文件称为动态共享对象(DSO,Dynamic Shared Ob ...
- 算法Sedgewick第四版-第1章基础-009一链表与数组的比较及其他数据结构
1. 2.