原题地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

解决方法:动态规划,minimun存储的是当前价格中最小的。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int minimum = INT_MAX, ret = , size = prices.size();
for(int i = ; i < size; ++i){
if(prices[i] < minimum)
minimum = prices[i];
int diff = prices[i] - minimum;
if(diff > ret)
ret = diff;
}
return ret;
}
};

LeetCode题目:Best Time to Buy and Sell Stock的更多相关文章

  1. [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 ...

  2. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [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 ...

  6. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. 【LeetCode】Best Time to Buy and Sell Stock IV

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

  8. [Leetcode Week6]Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...

  9. [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 关于__GNU_SOURCE 这个宏---如何开启【转】

    关于__GNU_SOURCE 这个宏 转自:http://blog.csdn.net/stevenliyong/article/details/4160181 版权声明:本文为博主原创文章,未经博主允 ...

  2. 自己写操作系统 2 - 安装bochs虚拟机

    以ubuntu操作系统为例 一.命令行安装 sudo apt-get install vgabios bochs bochs-x bximage 此方法方便,不过都说这个方法安装的bochs没有调试功 ...

  3. mysql 取消命令行继续编辑

    mysql> create database mingongge defa\c#回车 置空mysql>  加一个\c  cancel 编辑命令 回车

  4. laravel-u-editor工具栏语言切换的方法

    更改/config/app.php/locale,可支持en,zh_CN,zh_TW,我们一般设为zh_CN

  5. Python的Web编程[1] -> Web服务器[0] -> Web 服务器与 CGI / WSGI

    Web服务器 / Web Server 对于Web来说,需要建立一个Web服务器,必须建立一个基本的服务器和一个处理程序, 基本服务器的主要作用是,在客户端和服务器端完成必要的HTTP交互, 处理程序 ...

  6. 前端html第三方登录

    首先推荐一下,这个博客主的文章:https://www.cnblogs.com/v-weiwang/p/5732423.html 很不错,基本靠他的博客. 我这里记一点自己调试过程中的报错等: 1,微 ...

  7. IO模型同步与异步阻塞与非阻塞的区别

    同步异步的区别 关注点:同步和异步关注的是消息通信机制 同步:所谓同步,就是在发出一个*调用*时,在没有得到结果之前,该*调用*就不返回.但是一旦调用返回,就得到返回值了.换句话说,就是由*调用者*主 ...

  8. linux-启动脚本-souce与sh

    source:        在当前shell程序中执行,  因此当前shell程序中的变量和环境变量,均可见.   执行的脚本,能更新到当前shell程序. sh:            开启一个新 ...

  9. 细说JavaScript对象(4): for in 循环

    如同 in 运算符一样,使用 for in 循环遍历对象属性时,也将往上遍历整个原型链. // Poisoning Object.prototype Object.prototype.bar = 1; ...

  10. HashMap在高并发下引起的死循环

    HashMap事实上并非线程安全的,在高并发的情况下,是非常可能发生死循环的,由此造成CPU 100%,这是非常可怕的.所以在多线程的情况下,用HashMap是非常不妥当的行为,应採用线程安全类Con ...