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. SpringJunit4 进行单元测试(实例篇--紧接上一章)

    前言: 在做WEB项目时,我们写好了一个Dao和Service后,接下来就是要进行单元测试,测试的时候还要等到Spring容器全部加载完毕后才能进行,然后通过拿到ApplicationContext对 ...

  2. 文件下载(Servlet/Struts2)

    文件上传(Servlet/Struts2/SpringMVC)的链接:http://www.cnblogs.com/ghq120/p/8312944.html 文件下载 Servlet实现 目录结构 ...

  3. OpenStack IceHouse 部署 - 3 - 控制节点部署

    Mysql部署配置  安装 安装mysql,mysql的python绑定 apt-get install mysql-server 安装过程中会要求设定mysql的root账户的密码,这里假定设为my ...

  4. 旋转/非旋转treap的简单操作

    treap(树堆) 是在二叉搜索树的基础上,通过维护随机附加域,使其满足堆性质,从而使树相对平衡的二叉树: 为什么可以这样呢? 因为在维护堆的时候可以同时保证搜索树的性质: (比如当一棵树的一个域满足 ...

  5. MySQL与PHP的连接教程步骤(图文)

    本篇文章我们介绍一下PHP与MySQL的整合,既然是与MySQL整合,那么我们首先肯定是要安装MySQL.下面我们就介绍下MySQL的安装方法. 第一步,下载MySQL.下载PHP可以去PHP中文网下 ...

  6. line-height属性的深入了解

    line-height属性的细节与大多数CSS属性不同,line-height支持属性值设置为无单位的数字.有无单位在子元素继承属性时有微妙的不同. 语法line-height: normal | & ...

  7. ffmpeg 简介及使用

    简介 ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url ...

  8. 2.bootstrap安装

    1.下载 您可以从 http://getbootstrap.com/ 上下载 Bootstrap 的最新版本(或者 http://www.bootcss.com/  中文网) Download Boo ...

  9. [翻译] JNWSpringAnimation

    JNWSpringAnimation https://github.com/jwilling/JNWSpringAnimation JNWSpringAnimation is a subclass o ...

  10. Python学习---重点模块的学习【all】

    time     [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...