Friday, November 7, 2014

Facebook: Best time to buy and sell stock

Best time to buy and sell stock
http://buttercola.blogspot.com/2014/09/leetcode-best-time-to-buy-and-sell-stock.html
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.
public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) {
            return 0;
        }
        
        int minBuyPrice = Integer.MAX_VALUE;
        int maxProfit = 0;
        
        for (int i = 0; i < prices.length; i++) {
            minBuyPrice = Math.min(minBuyPrice, prices[i]);
            maxProfit = Math.max(maxProfit, prices[i] - minBuyPrice);
        }
        
        return maxProfit;
    }
}

Best time to buy and sell stock II
http://buttercola.blogspot.com/2014/09/leetcode-best-time-to-buy-and-sell.html
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) {
            return 0;
        }
        
        int start = 0;
        int maxProfit = 0;
        
        for (int i = 0; i < prices.length; i++) {
            if (i != 0 && prices[i] < prices[i - 1]) {
                maxProfit += prices[i - 1] - prices[start];
                start = i;
            }
        }
        
        maxProfit += prices[prices.length - 1] - prices[start];
        
        return maxProfit;
    }
}

13.  Best time to buy and sell stock III
http://buttercola.blogspot.com/2014/09/leetocde-best-time-to-buy-and-sell.html
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) {
            return 0;
        }
        
        int[] leftMax = new int[prices.length];
        int[] rightMax = new int[prices.length];
        
        int minBuy = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            minBuy = Math.min(minBuy, prices[i]);
            maxProfit = Math.max(maxProfit, prices[i] - minBuy);
            leftMax[i] = maxProfit;
        }
        
        int maxSell = Integer.MIN_VALUE;
        maxProfit = 0;
        for (int i = prices.length - 1; i >= 0; i--) {
            maxSell = Math.max(maxSell, prices[i]);
            maxProfit = Math.max(maxProfit, maxSell - prices[i]);
            rightMax[i] = maxProfit;
        }
        
        maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            maxProfit = Math.max(maxProfit, leftMax[i] + rightMax[i]);
        }
        
        return maxProfit;
    }
}

No comments:

Post a Comment