版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/xiezhihua120/article/details/32939749

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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

分析:维护一个局部最小值,迭代扫描数组,当在 i 位置卖出时值须要知道过去的最低价minPrices就可以,在i位置得到最大收益。多次迭代求出在n-1位置得最大收益。

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

此处须要注意測试用例,当没有元素或者仅仅有一个元素时,最大获利为0

LeetCode OJ - Best Time to Buy and Sell Stock的更多相关文章

  1. [LeetCode OJ] Best Time to Buy and Sell Stock I

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

  2. 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 ...

  3. [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] 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 ...

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

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

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

  8. 【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 ...

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

随机推荐

  1. Android ActionBar自定义

    关于自定义的ActionBar的实现过程,这里做下笔记以供之后查看. 1.默认状态 使用Android Studio新建一个名为“ActionBar”的应用,默认actionbar如图(1)所示. 图 ...

  2. centos7修改hostname

    [root@centos7 ~]$ hostnamectl set-hostname prd_web1 # 使用这个命令会立即生效且重启也生效 [root@centos7 ~]$ hostname # ...

  3. uboot 版本号生成过程

    uboot 版本号生成过程 uboot版本号貌似与实际开发不相关,但是我现在遇到一个bug与版本号关联密切. 这个bug与<uboot dm9000驱动故障>基本上是一样的,但是在上一篇博 ...

  4. 如何在命令提示符下编译运行含有Package的java文件

    这篇是大二自学Java的时候记下的笔记,中午回顾印象笔记的时候意外看到了这篇.看到多年前写下的文字,我想起那时候我对Java的懵懵懂懂,每天晚上在图书馆照着书写书上的示例代码,为一个中文分号绞尽脑汁, ...

  5. 彻底搞懂DOM事件处理(零)引子

    通过合理使用JavaScript,可以为网站用户提供更好的交互体验.这主要是因为JavaScript能够让网站对用户的各种操作及时做出"反馈".这种"反馈"使网 ...

  6. zeptojs库解读1之整体框架

    首先看的是整体框架, // zepto骨骼,这个函数的作用使得Zepto(slector, context)使用很多$.fn里面的方法 var Zepto = (function(){ // zept ...

  7. 调用libpci库出现的问题和解决方法

    调用libpci库出现的问题和解决方法   本方案以pciutils-3.5.1为例.   1. 从以下地址下载pciutils-3.5.1.tar.xz https://www.kernel.org ...

  8. 【转】IntelliJ IDEA的光芒会盖过Eclipse吗

    作为一个资深的Eclipse用户,我想对IntelliJ IDEA做一个更为严谨的审视.JetBrains的工作人员非常的友善,并为Podcastpedia.org和Codingpedia.org这两 ...

  9. Java Spring-Spring框架概述

    2017-11-06 15:55:38 Spring 是分层的JavaSE/EE full-stack(一站式)轻量级开源框架. * 分层:SUN公司提供了EE的三层结构:Web层,业务层,数据访问层 ...

  10. Tornado源码分析 --- Cookie和XSRF机制

    Cookie和Session的理解: 具体Cookie的介绍,可以参考:HTTP Cookie详解 可以先查看之前的一篇文章:Tornado的Cookie过期问题 XSRF跨域请求伪造(Cross-S ...