Converse thinking,o(n) solution,java


  • 0
    C
    class Solution {
        public int maxProfit(int[] prices) {
    		int n = prices.length;
    		int res = 0, max = -1;
    		for (int i = n - 1; i >= 0; i--) {
    			if (max > prices[i]) {
    				if (max - prices[i] > res) {
    					res = max - prices[i];
    				}
    			} else
    				max = prices[i];
    		}
    		return res;
        }
    }
    

Log in to reply
 

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