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. java实现邮箱验证的功能

    在日常生活中,我们在一个网站中注册一个账户时,往往在提交个人信息后,网站还要我们通过手机或邮件来验证,邮件的话大概会是下面这个样子的: 用户通过点击链接从而完成注册,然后才能登录. 也许你会想,为什么 ...

  2. SparkGraphx计算指定节点的N度关系节点

    直接上代码: package horizon.graphx.util import java.security.InvalidParameterException import horizon.gra ...

  3. SSM迁移到Springboot记录

    日志问题 Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a ...

  4. 使用sql语句备份一张表

    如何使用sql语句复制一张表? 方法一:第一步:先建一张新表,新表的结构与老表相等. create table newtable like oldtable; 第二步:将老表中的值复制到新标中. in ...

  5. SecureCRT远程连接Linux下的sqlplus中退格键不能使用之解决方法

    ^H不是H键的意思,是backspace 主要是当你的终端backspace有问题的时候才需要设置   在linux环境下使用sqlplus,在回删(backspace)时往往会出现 一串的乱码.出现 ...

  6. 【SSH网上商城项目实战26】完成订单支付后的短信发送功能

     转自: https://blog.csdn.net/eson_15/article/details/51475431 上一节我们使用了Java mail完成了给买家发送邮件的功能,还遗留一个功能,就 ...

  7. CSS属性之relative

    0.相对定位relative特点 相对定位relative元素总是会占据位置,所占据的位置是在relative元素没有设置left/top/right/bottom属性时的位置: 相对定位relati ...

  8. 关于npm构建angular2项目问题

    我在win10系统下用npm构建好angular2项目之后,在命令行下运行 ng serve --open,报一下错误: Unknown browser query 'basedir=$(dirnam ...

  9. Java设计模式—备忘录模式

    个人感觉备忘录模式是一个比较难的设计模式,备忘录模式就是一个对象的备份模式,提供了一种程序数据的备份方法. 定义如下:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以 ...

  10. 仿拉手团购App8-- 更多模块

    1.获得缓存大小和清除缓存 应用内数据的所有路径: /data/data/com.xxx.xxx/cache - 应用内缓存(注:对应方法getCacheDir()) /data/data/com.x ...