题目:

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. 洛谷P1527 [国家集训队] 矩阵乘法 [整体二分,二维树状数组]

    题目传送门 矩阵乘法 题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入输出格式 输入格式: 第一行两个数N,Q,表示矩阵大小和询问组数: 接下来N行N列一共N* ...

  2. 虚拟机hadoop集群搭建

    hadoop tar -xvf hadoop-2.7.3.tar.gz mv hadoop-2.7.3 hadoop 在hadoop根目录创建目录 hadoop/hdfs hadoop/hdfs/tm ...

  3. python 与 mongodb的交互---查找

    python与mongo数据库交互时,在查找的时候注意的一些小问题: 代码: from pymongo import * def find_func(): #创建连接对象 client = Mongo ...

  4. odoo打包下载

    view 视图中下载按钮的编辑 <record id="action_download_zip" model="ir.actions.server"> ...

  5. 【基础知识】.Net基础加强第三天

    一. 里氏替换原则--类型转换 1. 里氏替换原则:当需要一个父类类型对象的时候,可以给一个子类类型的对象. 2. 里氏替换原则实际也就是发生了隐身转换 3.  a.>把子类类型赋值给父类类型, ...

  6. OpenContrail 体系

    OpenContrail 体系架构文档 1  概述1.1  使用案例1.2  OpenContrail控制器和vRouter1.3  虚拟网络1.4     Overlay Networking1.5 ...

  7. Python字典使用--词频统计的GUI实现

    字典是针对非序列集合而提供的一种数据类型,字典中的数据是无序排列的. 字典的操作 为字典增加一项 dict[key] = value students = {"Z004":&quo ...

  8. LINUX按键驱动程序

    <<混杂设备驱动模型>> <混杂设设备的描述> <混在设备的概念> 在linux系统中,存在一类字符设备,他们拥有相同的主设备号(10),但是次设备号不 ...

  9. getJSON获取JSON文件加载下拉框及动态验证比输入项

    1.html界面 <form action="" method="get"> <div class="form-group" ...

  10. bzoj4974 字符串大师 KMP

    明显的,有$next[i] = i - pre[i]$ 根据$next[i]$构造比根据$pre[i]$简单 如果$next[i] \neq 0$,那么我们可以直接取前面的结果 否则,我们可以暴力的寻 ...