【题目】

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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
  Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

【思路】
Kadane's Algorithm差值
【代码】

class Solution {
    public int maxProfit(int[] prices) {
        int cur=0;
        int sum=0;
        for(int i=1;i<prices.length;i++){
            cur+=prices[i]-prices[i-1];
            sum=Math.max(sum,cur);
            cur=cur>0?cur:0;
        }
        return sum;
    }
}

[leetcode121]股票买卖 Best Time to Buy and Sell Kadane算法的更多相关文章

  1. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

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

  2. Leetcode-121 Best Time to Buy and Sell Stock

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

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

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

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

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

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

  8. [LintCode] 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 ...

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

随机推荐

  1. 【转载】LINUX下安装wget命令(SFTP实现法)

    如何安装wget命令. 方法一:通过yum 命令行为:yum install wget 完成.此操作很简单,但是我安装的linux是centos的最小版本,运行上述命令时会出现无法连接到源网站(大概是 ...

  2. JavaScript 第九章总结

    Handing events 前言 这一章节主要讲了关于 events 的内容,讲了 event 的定义,以及如何用 code 来 react to events.同时,也说明了 JavaScript ...

  3. anaconda安装tensorflow

    1.下载anaconda python3.5版本,Windows不支持python3.6,linux和mac支持python2.7和python3.3+ 2.创建环境   conda create - ...

  4. 1 虚拟环境virtualenv

    一.windows下虚拟环境创建 1.1 虚拟环境virtualenv 如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新 ...

  5. apicloud 环信总结

    点击链接先查看一下apicloud 环信的文档 https://docs.apicloud.com/Client-API/Open-SDK/easeChat 文档中写了很多,但官方给的文档还是有问题, ...

  6. Routing a Marathon Race

    直接爆搜的复杂度是2^n,对于n<=40的数据过不了. 考虑优化一下. 发现如果走了一个点后,以后是不可能再经过与它相邻的点的,因为这样走显然不如直接走那个与它相邻的点. 这样每走一步就可以删掉 ...

  7. CF903G Yet Another Maxflow Problem

    考虑最大流=最小割 不妨把a到a的边称为a类边,b到b的称为b类边,a到b的称为c类边. 显然,答案一定是由最多1条a和最多一条b以及一些c组成的. 只有a是会变的,也就是说每个a对应了唯一的最优的b ...

  8. vm15安装MACOS

    VMWare15 安装 Mac OS 系统文章目录VMWare15 安装 Mac OS 系统安装环境工具准备准备工作MAC虚拟机设置启动MAC前准备工作安装系统安装VMware Tool注意事项参考链 ...

  9. day1-6 字符串、列表、元组、字典、类型转换

    day1 1.python历史. 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 2.python的环 ...

  10. zend framwork黑箱测试

    1.我采用的测试时phpunit 1).按照:https://phpunit.de/  把下载的文件放到C:/window 目录,让后修改一下文件的后缀,使在敲击命令的时候可以不用敲文件的全称 如:p ...