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基础之JDK的下载与安装

    做Java开发已经很长一段时间了,最近在回顾Java的基础知识,感觉好多都是知道这个概念,能说个皮毛,但是往深了说又不知道怎么说,所以打算对Java从头做一个回顾,算是对自己所学知识的一个巩固和深入了 ...

  2. MySQL常见常用的SQL优化

    应尽量避免在where中使用!=或<>操作符.否则会进行全表查询 对于查询,避免全盘扫描,考虑在where或order by涉及到的列上建立索引 避免在where中进行null值判断,否则 ...

  3. JDBC入门(3)--- PrepareStatement

    一.PrepareStatement概述 PrepareStatement是Statement接口的子接口: 1.强大之处: 防SQL攻击: 提高代码的可读性: 提高效率; 2.PrepareStat ...

  4. 第二天-while循环 格式化输出 运算符 编码

    一.while循环 while 条件: 语句块(循环体)     #判断条件是否成立,若成立执行循环体,然后再次判断条件...直到不满足跳出循环 else: 当条件不成立的时候执行这里,和break没 ...

  5. 获取所有后缀DDE打开命令

    概述: 由于需要使用DDE方式打开文件,所以把支持DDE方式打开文件的参数都导出来到文件,方便查找. 并且提供运行DDE命令的工具,可以用于测试DDE功能. 1.运行脚步GetDDE.vbs可以获取系 ...

  6. Redis的基本操作语句

    注:以下出现的key.value都需要具体 1.String类型的数据存储获取 set key value:设置key的值为value,若存在则覆盖,不存在则自动创建decrby get key:获取 ...

  7. shrio的知识储备

    博客讲解; shrio的知识储备 shrio的简单认识 笔记整理地址: Shrio.pdf 下载 Shrio理论.doc 下载 Shrio知识储备.doc  下载 Shrio的知识储备 (一)   S ...

  8. maven项目在idea下右键不出现maven的解决办法

    重新删除项目,导出 再重新引入.

  9. 从零学React Native之04自定义对话框

    本篇主要介绍: 1. 自定义组件 2. Alert 对话框 自定义对话框 之前的我都是利用React Native提供的基础组件对它们进行排列组合, 其实自定义也很简单, 我们还是拿上一篇文章的例子进 ...

  10. 【Oracle】等待事件详细内容

    一.等待事件的相关知识 1.1 等待事件主要可以分为两类,即空闲(IDLE)等待事件和非空闲(NON-IDLE)等待事件.1). 空闲等待事件指ORACLE正等待某种工作,在诊断和优化数据库的时候,不 ...