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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 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 In this case, no transaction is done, i.e. max profit = 0.

题目标签:Array, Dynamic Programming

  题目给了我们一个array, 每一个数字代表了当时股票价格。让我们找出最大的收益。

  来更新一下,之前写的解题思路有点错误,所以这里重新写。

  试想一下,什么时候才有收益? Example 2 里就不存在最大收益,那么最基本是的 有一个小的值在前面,一个大的值在后面才有收益。

  那么什么时候才是 最大收益? 当一个最大收益出现时候,从index 0 开始 到 后面那个 卖出的价格 这个区间里,买入的价格肯定是这个区间里的min,卖出的是 从买入价格开始 到 卖出价格 这段区间里的max。

  那么是不是意味着 我们只要找到array里的min 和在min 之后范围里的max 就对了呢? 这是之前想错的地方。

  举个例子:7 14 1 5 3 6 4  这里最小的是1 在1后面最大的是6, 1 和 6的 profit 是5, 但是这里最大的收益是 14 - 7 = 7。

  所以我们要在遍历的过程中,一直维护更新一个min, 这个min最终的确是最小的那个,但是我们需要的min不一定是整个array中最小的。在遍历中,对于每一次的 收益 (目前的price - 目前的min),都要检查它是不是可能成为最大的收益。 这样做的目的是因为:我们不知道我们需要的min 是在什么时候出现,所以我们要检查所有 price - min 的可能性。因为上面的黑体字,最大收益肯定出现在 一段区间内, 买入的价格是区间里的min。

Java Solution:

Runtime beats 88.15%

完成日期:04/06/2017

关键词:Array, Dynamic Programming

关键点:时时保存最小价钱,对于每一个价钱,利用当前价格 - 最小价格, 保存最大收益值

 class Solution
{
public int maxProfit(int[] prices)
{
int maxProfit = 0;
int min = Integer.MAX_VALUE; for(int price: prices)
{
// keep updating the min all the time
min = Math.min(min, price);
// check each possible maxProfit and keep the larest one
maxProfit = Math.max(maxProfit, price - min);
} return maxProfit;
}
}

参考资料:

http://www.cnblogs.com/springfor/p/3877059.html

LeetCode 题目列表 - LeetCode Questions List

LeetCode 121. Best Time to Buy and Sell Stock (买卖股票的最好时机)的更多相关文章

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

  2. 121. Best Time to Buy and Sell Stock买卖股票12

    一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...

  3. 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机

    网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...

  4. 30. 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 of ...

  5. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  8. Java for 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 ...

  9. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

随机推荐

  1. 练习使用markdown

    我的随笔 写随笔的原因 1 完全是为了练习使用markdown编辑器 2 我是个爱学习的宝宝 3 学习能力问题? 随笔内容 弄懂markdown语法 随便谢谢心情 个人心情 冷漠 不想说话 神经 个人 ...

  2. Java内部类的总结

    内部类是指在一个外部类的内部再定义一个类.类名不需要和文件夹相同. 内部类分为: 成员内部类.局部内部类.静态嵌套类.匿名内部类 . 1.成员内部类 成员内部类是最普通的内部类,它的定义为位于另一个类 ...

  3. activiti07- Task

    任务 用户任务: 用户任务,用来对那些需要人参与完成的工作进行建模.当流程执行到这样的用户任务时,会在被分配到该任务的用户或用户组的任务列表中创建新的任务. 用户任务中可以包含描述.事实上,任何BPM ...

  4. IT连创业系列:创业者逆境下的思维

    距上篇文章,又半个多月过去了,是时候来一发阶段性的总结了. 可能最近比较懒,也可能是想不到写文的主题,故写文已变成越来越艰难的一个任务. 这个系列的大标题,也改了:它从<一个想法>到< ...

  5. MySQL集群(四)之keepalived实现mysql双主高可用

    前面大家介绍了主从.主主复制以及他们的中间件mysql-proxy的使用,这一篇给大家介绍的是keepalived的搭建与使用! 一.keepalived简介 1.1.keepalived介绍 Kee ...

  6. mongodb 在windows下面进行分片

    在mongodb里面存在另一种集群,就是分片技术,跟sql server的表分区类似,我们知道当数据量达到T级别的时候,我们的磁盘,内存就吃不消了,针对这样的场景我们该如何应对. 一:分片 mongo ...

  7. mybatis-generato的功能扩展

    项目代码地址:https://github.com/whaiming/java-generator 我在原有的基础上扩展了和修改了一些功能: 1.增加获取sqlServer数据库字段注释功能 2.Ma ...

  8. ArrayList 和 LinkedList 的实现与区别

    (转载请标明出处) 1.ArrayLis t的实现 2.LinkedLis t的实现 3.ArrayList 和 LinkedList 的区别 ArrayList 的实现: 1.MyArrayList ...

  9. Spark组件

    1,Application application(应用)其实就是用spark-submit提交的程序.比方说spark examples中的计算pi的SparkPi.一个application通常包 ...

  10. mybatis 错误CGLIB is not available

    ### Error querying database. Cause: java.lang.IllegalStateException: Cannot enable lazy loading beca ...