这样的题就不要去考虑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. git 支持tree命令

    由于git 里面是不支持tree命令的 有两种方法可以达到tree的效果 1.使用 winpty tree.com 2.安装tree.exe可执行文件 下载链接: https://sourceforg ...

  2. RN code push自定义弹框

    最近在弄react native的code push热更新问题.开始是用的后台默默更新配置.由于微软服务器速度问题,经常遇到用户一直在下载中问题.而用户也不知道代码需要更新才能使用新功能,影响了正常业 ...

  3. [笔记] APIO 2018 Day1

    计算折纸 computaional origami 全息算法(???) margulis napkin problem 素数里有任意长的等差数列 xor gate Σxi or gate(exact ...

  4. python3.x Day4 内置方法,装饰器,生成器,迭代器

    内置方法,就是python3提供的各种函数,可以认为是关键字,帮助进行一些列的牛x运算. abs()#取绝对值 all([])#可迭代对象中的所有元素都为True 则为True,只要至少一个为Fals ...

  5. centos6 用户登陆管理

    查看当前登陆有哪些用户,在做什么 [root@web01 ~]# w :: up :, users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGI ...

  6. 一个页面从输入URL到加载显示完成,发生了什么?

    面试经典题--URL加载 一.涉及基本知识点: 1. 计算机网络 五层因特尔协议栈: 应用层(dns.http):DNS解析成IP并完成http请求发送: 传输层(tcp.udp):三次握手四次挥手模 ...

  7. Python面向对象之类属性类方法静态方法

    类的结构 实例 使用面向对象开发时,第一步是设计类: 当使用 类名() 创建对象时,会自动执行以下操作: 1.为对象在内存中分配空间--创建对象: 2.为对象的属性 设置初始值--初始化方法(init ...

  8. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  9. servlet页面没有跳转

    Boolean b = userService.selectByParams(user);if (b) { req.getSession().setAttribute("loginname& ...

  10. Leetcode 213.大家劫舍II

    打家劫舍II 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两 ...