题目:

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

Design an algorithm to find the maximum profit. You may complete as
many transactions as you like (ie, buy one and sell one share of the
stock multiple times). However, you may not engage in multiple
transactions at the same time (ie, you must sell the stock before you
buy again).

题解:

简单的方法就是一旦第二天的价格比前一天的高,就在前一天买入第二天卖出。代码如下:

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

但是这个会违反“不能同一天买卖的规则”,例如3天的价格分别为1,2,3,那么按上述算法就会在2那天同一天买卖了。。。

正确的做法是: 第1天买第3天卖。

虽然两种方法数字结果是对的,但是逻辑不一样。。

不过虽然这么说,这道题的本事仍然是让你找最大利润,并没有让你明确哪天买哪天卖。

所以对面试官你仍然可以说,这种方法只是帮助我找到最大利润,我并没有说是要在同一天买卖,只是计算:所有第二天比前一天大的差值的合,我是为了找最大利润而已(画个趋势图讲解一下就行了。。)。

不过如果不是那样略有点投机取巧的话,干嘛非要每一次有一点点增长就加总和,带来不必要的解释麻烦?

何不先找到递减的局部最低点,再找到递增的局部最高点,算一次加和,然后再这么找? 这样就能保证买卖不会在同一天了。。

代码如下:

 1    public static int maxProfit(int[] prices) {
 2         int len = prices.length;
 3         if(len <= 1)
 4             return 0;
 5         
 6         int i = 0;
 7         int total = 0;
 8         while(i < len - 1){
 9             int buy,sell;
             //寻找递减区间的最后一个值(局部最小点)
             while(i+1 < len && prices[i+1] < prices[i])
                 i++;
             //局部最小点作为买入点
             buy = i;
             
             //找下一个点(卖出点至少为下一个点)
             i++;
             //不满足。。继续往下找递增区间的最后一个值(局部最高点)
             while(i<len && prices[i] >= prices[i-1])
                 i++;
             //设置卖出点
             sell = i-1;
             //计算总和
             total += prices[sell] - prices[buy];
         }
         return total;
     }

感谢JustdoIT(http://www.cnblogs.com/TenosDoIt/p/3436457.html)的方法,要不然网上都是弥漫着第一个不太好说明白的,但是看起来简洁的方法。。这个方法更令人信服。

Best Time to Buy and Sell Stock II leetcode java的更多相关文章

  1. leetcode:122. Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  2. Best Time to Buy and Sell Stock II ——LeetCode

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

  3. 122. Best Time to Buy and Sell Stock II ——LeetCode

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

  4. Best Time to Buy and Sell Stock II [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...

  5. Best Time to Buy and Sell Stock III leetcode java

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

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

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

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

  8. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  9. Leetcode-122 Best Time to Buy and Sell Stock II

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

随机推荐

  1. 牛客练习赛1 C - 圈圈

    链接:https://www.nowcoder.com/acm/contest/2/C来源:牛客网 题目描述 shy有一个队列a[1], a[2],…,a[n].现在我们不停地把头上的元素放到尾巴上. ...

  2. CSUOJ 1005 Binary Search Tree analog

    Description Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following ...

  3. 【原创】Linux常用脚本

    #1.启用停用VIP sudo /etc/ha.d/resource.d/IPaddr 10.10.10.10 start sudo /etc/ha.d/resource.d/IPaddr 10.10 ...

  4. 将已有的项目提交到GitHub

    1.目的: 将已有的项目提交到GitHub 2.准备工作 2.1 此教程建立在对git有初步的理解上 2.2 此教程之前需准备工作 a.熟悉git的一些基本命令和原理. b.已注册有GitHub账号. ...

  5. Javascript中call方法和apply方法用法和区别

    第一次在博客园上面写博客,知识因为看书的时候发现了一些有意思的知识,顺便查了一下资料,就发到博客上来了,希望对大家有点帮助. 连续几天阅读<javascript高级程序设计>这本书了,逐渐 ...

  6. hdu 4540 dp

    题意: 假设: 1.每一个时刻我们只能打一只地鼠,并且打完以后该时刻出现的所有地鼠都会立刻消失: 2.老鼠出现的位置在一条直线上,如果上一个时刻我们在x1位置打地鼠,下一个时刻我们在x2位置打地鼠,那 ...

  7. 【洛谷】NOIP提高组模拟赛Day2【动态开节点/树状数组】【双头链表模拟】

    U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...

  8. C++反汇编-结构体和类

    学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 对象的内存布局 一般计算公式: 对象内存大小 = sizeof(数据成员1)+ sizeof(数据成员2) +. ...

  9. Complete list of APDU responses

    https://www.eftlab.com.au/index.php/site-map/knowledge-base/118-apdu-response-list List of APDU resp ...

  10. 2007 Audi A4 INSTRUMENT CLUSTER WIRING DIAGRAM

    BOSCH RB8 8E0920 951G I found the answer by myself...... Here is what it's work for me. GREEN CONNEC ...