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.

Invariant 在于,如果第i 天和第j 天分别是最高收益的买入点和卖出点,那么prices[i] 必然是[0, j] 区间内价格的最低点。

只用遍历一次,用当前找到的最低价格来计算收益,并更新收益最大值,同时更新价格最低点。遍历完成之后即得结果。

/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var n = 0;
var lowest = prices[0];
prices.forEach(function(p) {
n = Math.max(n, p - lowest);
lowest = Math.min(lowest, p);
});
return n;
};

[LeetCode] Best Time to Buy and Sell Stock的更多相关文章

  1. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  2. [LeetCode] 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 ...

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

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

  5. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

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

  7. LEETCODE —— 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 Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  9. [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  10. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

随机推荐

  1. 高性能集群软件Keepalived(1)

    1介绍 Keepalived是linux下一个轻量级的高可用解决方案,与HeartBeat,RoseHA实现的功能类似,但是还是有差别.HeartBeat是一个专业的功能完善的高可用软件,它提供了HA ...

  2. Java开发11个过不去的梗

    现在随着编程的普及,作为java程序猿开发的过程逐渐的受到领导的重视,无论自己的经理是能看懂,还是不能看懂,一些事项必须注意起来,不要让自己将来处于不尴不尬的境地,当然这样也方便你我他 1.不在属性文 ...

  3. 网页特效:用CSS3制作3D图片立方体旋转特效

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. Fibonacci 数列算法分析

    /************************************************* * Fibonacci 数列算法分析 ****************************** ...

  5. 如何合并两个Docker 镜像

    http://www.open-open.com/lib/view/open1437746544709.html 在你的机器上使用docker pull来从Docker Hub下载镜像. docker ...

  6. SQL Server附加数据库时报1813错误的解决方案

    SQL Server附加数据库时报1813错误的解决方案   无法打开新数据库 'ASR'.CREATE DATABASE 中止. 文件激活失败.物理文件名称'E:\SqlServer\MSSQL\D ...

  7. js中的逻辑与(&&)和逻辑或(||)

    之前有一个同事去面试,面试过程中碰到这样一个问题: 在js中写出如下的答案 : var a = 2; var b = 3; var andflag = a && b ; var orf ...

  8. SearchLookUpEdit

    参考资料: 慧都控件网-DevExpress开发资源 在GridControl控件中使用SearchLookUpEdit构建数据快速输入

  9. Java与MySQL的连接

    下载数据库驱动文件,解压并保存至任意位置 下载地址 新建Java项目,并将驱动文件添加到项目中 项目名右键-->构建路径-->配置构建路径-->添加外部Jar 在项目中新建类,编写代 ...

  10. Codeforces #261 D

    Codeforces #261 D D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per te ...