剑指 Offer 63. 股票的最大利润

public int maxProfit(int[] prices) {
        int n = prices.length;
        if(n == 0) return 0;
        int[] x = new int[n];
        x[0] = prices[0];
        int max = -1;
        for(int i = 1;i<n;i++){
            x[i] = Math.min(prices[i],x[i-1]);
            int interest = prices[i] - x[i];
            max = Math.max(max,interest);
        }
        return Math.max(max, 0);
    }

 

 

public int maxProfit(int[] prices) {
        int n = prices.length;
        if(n <= 1) return 0;
        int min = prices[0];
        int max = prices[1] - min;
        for(int i = 1;i<n;i++){
            if(prices[i]<min){
                min = prices[i];
            }
            max = Math.max(max,prices[i] - min);
        }
        return max;
    }

 

posted @ 2020-08-24 15:02  欣姐姐  阅读(120)  评论(0)    收藏  举报