package leetcode.medium;

/**
 * Created by wangxiaoyi on 15/11/18.
 */
public class BestTimetoBuyandSellStock {

    public int maxProfit(int[] prices) {
        //int max = Integer.MIN_VALUE;
        int max = 0;
        int len = prices.length;
        if(len < 2) return 0;
        else {
           // prices[0] = 0;
            int currMax = 0;
            int currMin = prices[0];// 记录一个到当前最便宜的股票价格
            for(int i = 1; i < len; ++ i){

                currMax = Math.max(currMax, prices[i] - currMin);
                currMin = Math.min(currMin, prices[i]);
                max = Math.max(currMax, max);
            }

        }
        return max;
    }

    public static void main(String []args){
        System.out.print(new BestTimetoBuyandSellStock().maxProfit(new int[]{1, 2, 4}));
    }
}
