这样的题就不要去考虑N^2的算法了。肯定会超时的。乍一看,非常可能会想到贪心,可是普通的贪心思路是不行的,比方想找到一个最小值用来买入。尽管它跟最大值之间的差一定是最好的,可是最大值出如今它前面就不行了,找出如今它后面的最大值,仅仅只是是一个局部最优,非常可能前面的最大和次小比他们要好。

所以呢,贪心找的不是两个点,而是差。这种话。每一步都维护一个最小值,然后算跟当前最小之间的差来更新最后的结果。

代码简洁,不在赘述。

class Solution {
public:
int maxProfit(vector<int> &prices) {
int len = prices.size();
if(len<=1)
return 0;
int mmax_profit = 0, mmin = prices[0];
for(int i=1;i<len;i++){
mmin = min(mmin, prices[i]);
mmax_profit = max(mmax_profit, prices[i] - mmin);
}
return mmax_profit;
}
};

leetcode第一刷_Best Time to Buy and Sell Stock的更多相关文章

  1. leetcode第一刷_Best Time to Buy and Sell Stock II

    这道题尽管是上一道题的增强.可是反而简单了. 能够交易无数次,可是买卖必须成对的出现. 为了简单起见.我用abc三股股票来说明,且忽略掉相等的情况.三个数一共同拥有六种大小关系.注意他们之间的先后顺序 ...

  2. leetcode第一刷_Best Time to Buy and Sell Stock III

    这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类.只是相比之下稚嫩多了.有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律.这道题 ...

  3. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  4. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  5. 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...

  6. 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  7. 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  9. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

随机推荐

  1. C#通过post发送接收数据流

    发送数据流方法 /// <summary> /// /// </summary> /// <param name="url">目标url< ...

  2. Java基础(十四)--装箱、拆箱详解

    Java中基本数据类型都有相对应的包装类 什么是装箱?什么是拆箱? 在Java SE5之前,Integer是这样初始化的 Integer i = new Integer(10); 而在从Java SE ...

  3. java_lock锁

    lock锁是一个接口,jdk5.0新增的接口: 在线程中创建一个他的实现类对象Reentrantlock,默认为fals可以改为true,改为true后是有序的 把操作共享资源的代码放入try中,在t ...

  4. mysql事件【定时器】

    一,借鉴[luo奔的蜗牛] 1.创建一张表 create table mytable ( id int auto_increment not null, name ) not null default ...

  5. Linux 内核框架图

  6. Linux基础学习一

    swap:虚拟内存ctrl+a:跳到命令首部 ctrl+e:跳到命令尾部alias:指令别名cp -r:递归复制粘贴mv 源路径 目标路径:移动操作 (如果提示是否覆盖,在mv前加\即可不提示:\mv ...

  7. linux如何正确设置静态ip

    如果是新安装的CentOS7的用户,刚开始应该是没网的,ifconfig命令在CentOS7已经被淘汰了. 1.使用ip addr 即查看分配网卡情况. 2.激活网卡 [root@localhost ...

  8. mysql负载均衡

    一.docker安装haproxy:docker pull haproxy 二.配置haproxy(参考url:https://zhangge.net/5125.html),vim /usr/loca ...

  9. Memcache 分布式存储 【一致性Hash】crc32

    class memcacheHash { private $_node = array(); private $_nodeData = array(); private $_keyNode = 0; ...

  10. 语法,if,while循环,for循环

    目录 一.语法 二.while循环 三.for循环 一.语法 if: if判断其实是在模拟人做判断.就是说如果这样干什么,如果那样干什么.对于ATM系统而言,则需要判断你的账号密码的正确性. if 条 ...