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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. N次剩余和二次剩余

    N次剩余 给定 \(N,a,P\),且 \(P\) 最好为质数 可以算出 \(x^N\equiv a(mod~p)\) 的解 首先可以算出 \(P\) 的原根 \(g\) 解方程 \(g^y\equi ...

  2. JavaScript的进阶之路(七)客户端JavaScript知识点总结

    一.客户端JavaScript主要是BOM DOM的操作和js脚本的兼容性.互用性.可访问性.安全性的应用.以及一些框架的引用. 二.BOM:浏览器对象模型 主要介绍window对象 1.定时器:se ...

  3. WinForm实现Rabbitmq官网6个案例-Routing

    代码: namespace RabbitMQDemo { public partial class Routing : Form { private string exchangeName = &qu ...

  4. KaTeX.js

    KaTeX.js 官网:https://khan.github.io/KaTeX/

  5. CSS的下拉菜单被挡住,修改Z-INDEX也不成功

    CSS的下拉菜单被挡住,修改Z-INDEX也不成功 做了一个鼠标放上去就出现的下拉菜单,但是这个下拉的内容被挡住了. Z-INDEX 是设置不同块的层次的,我修改了问题还是有. 后来发现是必须要把该便 ...

  6. Ubuntu更换硬盘

    0x00 背景: 目前ubuntu用的是一个80g的硬盘( 一开始没买硬盘,直接拆了一个老老老老的机子来用),系统16.04, 只有一个/分区 ( /dev/sdb1 ) . 新的硬盘是一块256G的 ...

  7. Gitlab installtation

    环境:Centos7 安装配置(安装开启http和sshd): 1 yum -y install curl openssh-server postfix 2 systemctl enable sshd ...

  8. git操作合集

    目录 安装 下载 本地配置 创建用户凭证ssh 忽略文件 基础操作 新建仓库 克隆仓库 获取更新 推送更新 查看历史 版本回退 分支 别名 linux服务器 疑难问题 清除历史大文件 安装 下载 下载 ...

  9. 彻底澄清c/c++指针概念

    传统的指针概念教科书上已经写的很详细了,这里作为一些补充 在声明一个指针变量的时候 double  *ptr: 这是声明了一个ptr的指针变量,ptr本身是地址,它的数据类型是double  *,在变 ...

  10. course & time