JAVA----------Easy Version To Understand!!!!!!!!!!!!!!!!!!!


  • 5
    H
    	public static int maxProfit(int[] prices) {
    	if (prices == null || prices.length == 0)
    		return 0;
    	int len = prices.length;
    	int[] sell = new int[len];
    	int[] coolDown = new int[len];
    	sell[0] = 0;
    	coolDown[0] = 0;
    	for (int i = 1; i < len; i++) {
    		sell[i] = Integer.max(sell[i - 1] + prices[i] - prices[i - 1], coolDown[i - 1]);
    		coolDown[i] = Integer.max(sell[i - 1], coolDown[i - 1]);
    	}
    	return Integer.max(sell[len - 1], coolDown[len - 1]);
    }

  • 0
    C

    Good job. Also, the default value of an int is 0, so no need the two lines "sell[0] = 0;" and "coolDown[0] = 0;"


  • 0

    Such a nice one!
    Easy!


  • 0
    C

    It looks like sold at i -1, than buy again at i-1, which conflicts with cooldown requirement? Can you give a more detailed explanation?
    sell[i] = Integer.max(sell[i - 1] + prices[i] - prices[i - 1], coolDown[i - 1]);


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.