【题目】

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. 昂达 v891 v1 终于 删除 windows 分区 并且恢复了容量。

    参考了很多文章(最后列出重要的),却始终失败. 途中因为乱改分区表,竟然fastboot 都进不去了,当时真是欲哭无泪. 总结关键点: 1) partition.tbl不能把硬盘剩余空间全给data分 ...

  2. 利用adb截图然后传到电脑

    首先配置好adb环境变量 然后adb devices查看是否连接手机,记得把手机调成开发者模式. 截屏 adb shell /system/bin/screencap -p 路径/文件名.后缀名 ad ...

  3. Webpack实现路由懒加载的三种方式

    原文指路:https://blog.csdn.net/qq_37540004/article/details/78727063 第一种: 引入方式(正常引入): const router = new ...

  4. 2.5 UML顺序图

    相关概念 交互 对象之间为实现某一功能而必须实施的协作过程.动态行为,称为交互 消息 对象间的协作与交流表现为一个对象以某种方式启动另一个对象的活动,这种交流在 UML里被定义为消息 顺序图的建模元素 ...

  5. git 更新代码到本地

    正规流程 git status(查看本地分支文件信息,确保更新时不产生冲突) git checkout – [file name] (若文件有修改,可以还原到最初状态; 若文件需要更新到服务器上,应该 ...

  6. grep 和curl -d等命令 单引号里面既使用正则,又使用变量的方法

    a='{"type":"d_log", "log_format":"d_log", "exclude" ...

  7. sqlite3 新增数据

    cx = sqlite3.connect("c:/数据库名字")#打开数据库cu = cx.cursor()cu.execute("INSERT INTO [user] ...

  8. Oracle11g温习-第十六章:用户管理

    2013年4月27日 星期六 10:50 1.概念 (1)schema : user.object    就是用户创建的对象 (2)用户认证方式:                            ...

  9. Linux系统下tomcat的配置

    Linux系统下tomcat的配置 完成后可以输入命令查看日志文件: 最后进入网页测试下吧: 可以出来这个网页就好了

  10. ORM框架之SQLALchemy

    一.面向对象应用场景: 1.函数有共同参数,解决参数不断重用: 2.模板(约束同一类事物的,属性和行为) 3.函数编程和面向对象区别: 面向对象:数据和逻辑组合在一起:函数编程:数据和逻辑不能组合在一 ...