Monday, July 13, 2015

Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Have you met this question in a real interview? 
Yes

Example
Given an example [3,2,3,1,2], return 1

public class Solution {
    /**
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    public int maxProfit(int[] prices) {
        // write your code here
        if(prices == null || prices.length <= 1) return 0;
        int maxProfit = Integer.MIN_VALUE, minIndex = 0;
        for(int i = 0; i < prices.length; i++){
            maxProfit = Math.max(maxProfit, prices[i] - prices[minIndex]);
            if(prices[i] < prices[minIndex]){
                minIndex = i;
            }
        }
        
        return maxProfit;
        
    }
}

No comments:

Post a Comment