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.

解题思路:

本题目是《算法导论》 4.1 节 给出的股票问题的原题,可以转化为最大子数组的问题,书本上给出的是分治的做法,练习4.1-5给出了线性时间的算法。

Java for LeetCode 053 Maximum Subarray实现了相应的解法,如果不转化为最大子数组问题,解法如下:

一次遍历,每次找到最小的Buy点即可,JAVA实现如下:

  1. public int maxProfit(int[] prices) {
  2. int buy = 0;
  3. int profit = 0;
  4. for (int i = 0; i < prices.length; ++i) {
  5. if (prices[buy] > prices[i])
  6. buy = i;
  7. profit = Math.max(profit, prices[i] - prices[buy]);
  8. }
  9. return profit;
  10. }

Java for LeetCode 121 Best Time to Buy and Sell Stock的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

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

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

  4. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

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

  7. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

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

  8. Java for LeetCode 122 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. Python [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 ...

随机推荐

  1. 受检查异常要求try catch或者throws,但是要记住只要catch异常了,就不会向下继续抛了

    所以在框架中,要想异常被统一的异常拦截器处理,就要将受检查异常转换为运行异常,在受检查异常的catch时候,手动throw new runtime exception

  2. 在红米note4上实现自动安装软件

    因为要做自动化测试,需要对已发布的包进行回归手测,这个时候需要手动安装APK,但是红米会弹出继续安装的按钮,手点一次比较烦,想自动点"继续安装"按钮! 感谢先行者们的分享 本文参考 ...

  3. How to convert .crt to .pem [duplicate]证书转化

    openssl x509 -in mycert.crt -out mycert.pem -outform PEM openssl x509 -inform DER -in yourdownloaded ...

  4. 200多种Android动画效果的强悍框架

    admin 发布于2015-10-23 14:33 363/68015 [精品推荐]200多种Android动画效果的强悍框架,太全了,不看这个,再有动画的问题,不理你了^@^ 功能模块和技术方案 只 ...

  5. selenium firefox46.0.1设置禁用图片

     firefox_profile = webdriver.FirefoxProfile()firefox_profile.set_preference('permissions.default.ima ...

  6. Iowait的成因、对系统影响及对策

    什么是iowait?顾名思义,就是系统因为io导致的进程wait.再深一点讲就是:这时候系统在做io,导致没有进程在干活,cpu在执行idle进程空转,所以说iowait的产生要满足两个条件,一是进程 ...

  7. dom控制

    (1)创建新节点 createDocumentFragment()    //创建一个DOM片段 createElement_x_x()   //创建一个具体的元素 createTextNode()  ...

  8. js 小总结

    数组操作 创建数组:var standTerm = new Array("维护","维修"); var arr = new Array(); 数组长度:leng ...

  9. robotframe使用之滚动条

    方法一:Excute JavaScript window.scrollTo(0,document.body.scrollHeight); 方法二:Execute javascript document ...

  10. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...