Wednesday, March 20, 2013

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.


public class Solution {
    public int maxProfit(int[] prices) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int ret = 0;
        int preFit = 0;
        for(int i=1; i<prices.length;i++){
            int low = prices[i-1] - preFit;
            if(low > prices[i]) low = prices[i];
            preFit = prices[i] - low;
            if(preFit > ret) ret = preFit;
        }
     
        return ret;
    }
}


class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int ret = 0;
        int preFit = 0;
        for(int i = 1; i < prices.size();i++){
            int low = prices[i-1] - preFit;
            if(low > prices[i]) low = prices[i];
            preFit = prices[i] - low;
            if(ret < preFit) ret = preFit;
        }
        return ret;
    }
};



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).



class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int ret = 0;
        for(int i = 1; i < prices.size(); i++){
            if(prices[i] > prices[i-1]) ret += prices[i] - prices[i-1];
        }
        return ret;
    }
};


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).


Solution:

Let preOneSell be the maximum earning if we have only one transaction and we sell the stock at day i
Let preTwoSell be the maximum earning if we have two transactions and we sell the stock at day i


class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int preOneSell = 0;
        int preTwoSell = 0;
        int maxOneSell = 0;
        int maxTwoSell = 0;
     
        for(int i = 1; i < prices.size();i++){
            int diff = prices[i] - prices[i-1];
            preTwoSell = max( maxOneSell+diff, preTwoSell + diff);
            preOneSell = max( preOneSell + diff, 0);
         
            maxOneSell = max(maxOneSell, preOneSell);
            maxTwoSell = max(maxTwoSell, preTwoSell);
        }
     
     
        return max(maxOneSell,maxTwoSell);
    }
};

No comments:

Post a Comment

Manacher's Longest Palindromic Substring Algorithm

http://manacher-viz.s3-website-us-east-1.amazonaws.com/#/