1. 程式人生 > >LeetCode#121: Best Time to Buy and Sell Stock

LeetCode#121: Best Time to Buy and Sell Stock

class Solution {
    public int maxProfit(int[] prices) {
         if(prices == null || prices.length <= 0)
    		return 0;
    	 
    	 int maxProfit = 0;
    	 int minPrice = prices[0];
    	 for(int i = 1; i < prices.length; i++) {
    		 if(prices[i] < minPrice)
    			 minPrice = prices[i]
; else if(prices[i] - minPrice > maxProfit) maxProfit = prices[i] - minPrice; } return maxProfit; } }