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
题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大
遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次计算当前位置价格与之前最低价格的差值,获得最大差值即为结果
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.empty())
return ;
int min_num = prices[];
int profit = ;
for(int i = ;i < prices.size();i++){
min_num = min(min_num,prices[i]);
if(prices[i] > min_num)
profit = max(profit,prices[i] - min_num);
}
return profit;
}
};
122.Best Time to Buy and Sell Stock II
可以买任意次,所以只要当前的价格比之前的一个多,就买卖,这样最多
class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = ;
for(int i = ;i < prices.size();i++){
if(prices[i] > prices[i-])
res += prices[i] - prices[i-];
}
return res;
}
};
309. Best Time to Buy and Sell Stock with Cooldown
https://www.cnblogs.com/grandyang/p/4997417.html
https://blog.csdn.net/qq508618087/article/details/51671504
注意题目的注释:you must sell the stock before you buy again,也就是说不能买了又买,卖了又卖,只能买一次再卖一次
用buy、sell两个数组表示当前位置以buy、sell操作结尾的最大收入,以buy为例,当前状态只可能来自两种情况:一、这一位置不做任何操作,就是不买也不卖 二、这一位置买,那之前的状态就是i-2的时候卖出了东西
初始化的时候注意0、1都要初始化,因为i-2,从2开始的循环。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int length = prices.size();
if(length <= )
return ;
vector<int> buy(length+,);
vector<int> sell(length+,);
buy[] = -prices[];
for(int i = ;i <= length;i++){
buy[i] = max(buy[i-],sell[i-] - prices[i-]);
sell[i] = max(sell[i-],buy[i-] + prices[i-]);
}
return max(buy[length],sell[length]);
}
};
714. Best Time to Buy and Sell Stock with Transaction Fee
https://www.cnblogs.com/grandyang/p/7776979.html
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
vector<int> buy(prices.size(),);
vector<int> sell(prices.size(),);
buy[] = -prices[];
for(int i = ;i < prices.size();i++){
buy[i] = max(buy[i-],sell[i-] - prices[i]);
sell[i] = max(sell[i-],buy[i-] + prices[i] - fee);
}
return max(buy[prices.size() - ],sell[prices.size() - ]);
}
};
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的更多相关文章
- 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 (买卖股票的最好时机)
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 买卖股票的最佳时间
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 (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 ...
- [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 ...
- 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...
随机推荐
- 判断php变量是否定义,是否为空,是否为真的一览表
分类: 使用 PHP 函数对变量 $x 进行比较 表达式 gettype() empty() is_null() isset() boolean : if($x) $x = ""; ...
- 插件式WebApi服务及自动生成Api帮助文档
上一篇博客中,讲到了将WebApi Host到控制台和IIS,本篇总结一下如何将WebApi的Service以插件的形式进行动态部署,并设置Hoster的首页显示Api帮助文档,当然,也包括动态部署进 ...
- 原生Ajax(XMLHttpRequest)
一.什么是Ajax: 全称Asynchronous JavaScript and XML: 异步的 JavaScript 和 XML: 可以在不重新加载整个页面的情况下(偷偷发数据),与服务器交换数据 ...
- 使用react——解决this.props.history.push无法跳转的问题
转自: https://blog.csdn.net/yingzizizizizizzz/article/details/78751305 场景: 一个组件中,含有ul展开数组的组件,在每一行中,都能点 ...
- react父子组件各自生命周期函数加载的先后顺序
理解记忆总结: 父组件即将挂载(最外层的父组件都还没准备进入,其内部的子组件当然更没进入了) -> 子组件即将挂载 -> 子组件挂载完成(父内部都没完成,父当然不能算完成) -> ...
- LeetCode赛题394----Decode String
394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...
- xshell连接虚拟机Connection failed
一.问题描述:xshell连接不了虚拟机,出现错误提示:Could not connect to '192.168.1.100' (port 22): Connection failed. 二.查找错 ...
- IEC62304软件维护框架
软件维护计划的任务 建立接收.记录.评估.解决和追踪医疗器械软件发行后的反馈 制定确认反馈是否是问题的标准 使用风险管理过程 使用配置管理过程 制定升级.补丁以及遗留问题修正计划 问题和修改分析的任务 ...
- spring cloud Eureka server配置
参考:http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html spring boot版本:2.0.3.RELEA ...
- c# webservice中访问http和https的wsdl,生成的配置节点的不同之处
http: https: