• 石头
    2019-01-15
    纪念一下我的第一次 “faster then 100%” (裸写的)
    “Runtime: 1 ms, faster than 100.00% of Java online submissions for Best Time to Buy and Sell Stock II.”
         public int maxProfit(int[] prices) {
            int profit = 0;
            for (int i = 0; i < prices.length - 1; i++) {
                if (prices[i] < prices[i + 1]) {
                    profit += prices[i + 1 ] - prices[i];
                }
            }
            return profit;
        }
    展开
    
     5
  • Derek
    2019-02-08
    GO语言版本实现:

    func maxProfit(prices []int) int {
        var max int
        for i := 0; i < len(prices)-1; i++ {
            if prices[i+1] > prices[i] {
                max += prices[i+1] - prices[i]
            }
        }
        return max
    }
    展开
    
     1
  • fiveone.lwq
    2018-12-18
    老师深度优先搜索怎么写啊,列出所有的情况
    
     1
  • tomlelouch
    2020-01-17
    class Solution {
      public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
          return 0;
        }
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
          if (prices[i] > prices[i - 1]) {
            profit += prices[i] - prices[i - 1];
          }
        }
        return profit;
      }
    }
    展开
    
    
  • Aliliin
    2019-11-09
    贪心算法 PHP 实现:
    function maxProfit($prices)
        {
            $j = 1;
            $res = 0;
            for ($i = 0; $i < count($prices); $i++) {
                if (!empty($prices[$j]) && $prices[$j] > $prices[$i]) {
                    $res += $prices[$j] - $prices[$i];
                }
                $j++;
            }
            return $res;
        }
    展开
    
    
  • 码农Kevin亮
    2019-10-09
    请问老师,对于交易k次的泛化版本,我看所有通过的算法都是在买入的状态转移方程中使用k-1,当我尝试换成卖出时才使k-1的话,是无法通过测试用例的。我不太明白其中的原理,而老师在写白板推导时也是在这个细节出了差错,希望老师可以补充说明
    
    
  • 陈志恒
    2019-09-27
    将问题抽象为层次问题,利用树的遍历来暴力搜,然后在这个版本基础上改进
    
    
  • Enhao
    2019-09-17
    DFS里每天有三种情况吧:
    1. 买
    2. 卖
    3. 不动
    
    
  • Vilochen.
    2019-05-27
    递归方式:
    public int maxProfit2(int[] prices) {
            return Math.max(maxProfit2(prices, 0, true)
                    , maxProfit2(prices, 0, false));
        }

        public int maxProfit2(int[] prices, int i, boolean buy) {
            if (i >= prices.length - 1) {
                return 0;
            }
            if (buy) {
                int sum = 0;
                if (prices[i] < prices[i + 1]) {
                    sum = prices[i + 1] - prices[i];
                }
                return Math.max(maxProfit2(prices, i + 1, true)
                        , maxProfit2(prices, i + 1, false)) + sum;
            } else {
                return Math.max(maxProfit2(prices, i + 1, true)
                        , maxProfit2(prices, i + 1, false));
            }
        }
    展开
    
    
  • Salad
    2019-05-03
    老师DFS这个方法该怎么实现呀
    
    
  • Allen_
    2019-02-25
    有个问题, 为什么DFS的时间复杂度是2^n? 如果遍历一遍,每一天的价格和后面的每一天计算利润的话,不应该是1+2+。。。+n-1吗?应该是N^2?
    
    
  • kilien
    2019-02-22
    def maxProfit(self, prices: 'List[int]') -> 'int':
            profit = 0
            for i, x in enumerate(prices):
                if i == len(prices) - 1: return profit
                if x < prices[i+1]:
                    profit += prices[i+1] - x
            return profit
    展开

    作者回复: 帅!

    
    
  • Aries
    2018-12-26
    def maxProfit(self, prices):
            pro = 0
            for i in range(1,len(prices)):
                a = prices[i-1] - prices[i]
                if a < 0:
                    pro -= a
            return pro
    这是动规还是贪心呢
    展开
    
    
  • 唔多志
    2018-12-24
    老师能否讲一下,为什么在这种情况下用贪心算法,得到的确实是最大的收益?能否结合子问题的方式来讲一下。
    
    
  • 王磊
    2018-10-27
    搜索算法每一天的操作是买/卖,和不操作。是买是卖取决于当前的持仓状态。
    
    
我们在线,来聊聊吧