【题目】

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. 第 3 章 镜像 - 019 - 使用公共 Registry

    保存和分发镜像的最直接方法就是使用 Docker Hub.https://hub.docker.com/ Docker Hub 是 Docker 公司维护的公共 Registry.用户可以将自己的镜像 ...

  2. 单细胞数据初步处理 | drop-seq | QC | 质控 | 正则化 normalization

    比对 The raw Drop-seq data was processed with the standard pipeline (Drop-seq tools version 1.12 from ...

  3. Linux(centos7)上安装最新版R3.4.1

    说来惭愧,居然没有在Linux安装R的经验,因为一直很少用R,用也是在win平台. 下载路径:https://cran.rstudio.com/src/base/R-3/ 强烈建议不要安装最新的R,除 ...

  4. android -------- 蓝牙Bluetooth

    什么是蓝牙? 也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用“蓝牙”技术,能够有效地简化掌上电脑.笔记本电脑和移动电话手机等移动通 ...

  5. git 基础知识

    git 分布式版本控制系统 git三棵树: 工作目录 红色 等待添加到暂存区域 需执行git add filename 命令添加到暂存区 暂存区域 绿色 文件等待被提交 需执行 git commit ...

  6. BottomNavigationBar使用详解

    gitHub地址:https://github.com/Ashok-Varma/BottomNavigation 一.基本使用 1.在AndroidStudio下添加依赖: compile 'com. ...

  7. 『MXNet』第九弹_分类器以及迁移学习DEMO

    解压文件命令: with zipfile.ZipFile('../data/kaggle_cifar10/' + fin, 'r') as zin: zin.extractall('../data/k ...

  8. 2018-2019 ACM-ICPC Brazil Subregional Programming Contest

    A:留坑 B:二维sg函数,特判边界情况 //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optimize(4) / ...

  9. HDU-4587-tarjin/割点

    http://acm.hdu.edu.cn/showproblem.php?pid=4587 给出一幅无向图,问除去两个点之后子图的最大联通分量个数. 考虑每次ban一个点然后跑一遍tarjin统计下 ...

  10. 牛客练习赛24-B-贪心

    链接:https://www.nowcoder.com/acm/contest/157/B来源:牛客网 题目描述 凤凰于飞,翙翙其羽,亦集爰止.                             ...