lintcode medium Best Time to Buy and Sell Stock I,II,III

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.

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.length==0 || prices==null) return 0;
        int n=prices.length;int max=0;
        int low=prices[0];
        
        for(int i=1;i<n;i++)
        {
            if(prices[i]<low)
            {
                low=prices[i];
            }
            else
            {
                max=Math.max(max,prices[i]-low);
            }
        }
        
        return max;
    }
}

 

Best Time to Buy and Sell Stock II

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

Example

Given an example [2,1,2,0,1], return 2

 

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

 

 

Best Time to Buy and Sell Stock III

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.

Example

Given an example [4,4,6,1,1,4,2,5], return 6.

Note

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

 

class Solution {
    /**
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    public int maxProfit(int[] prices) {
        // write your code here
        
        if(prices.length==0 || prices==null||prices.length==1) return 0;
        int n=prices.length;
        int[] opt=new int[n];
        int[] optReverse=new int[n];
        int ans=0;
        
        
        opt[0]=0;
        int low=prices[0];
        int result=0;
        for(int i=1;i<n;i++)
        {
            if(prices[i]<low)
            {
                low=prices[i];
            }
            else
            {
                result=Math.max(prices[i]-low,result);
            }
            opt[i]=result;
        }
        
        
        optReverse[n-1]=0;
        int high=prices[n-1];
        int result1=0;
        for(int i=n-2;i>=0;i--)
        {
            if(prices[i]>high)
            {
                high=prices[i];
            }
            else
            {
                result1=Math.max(high-prices[i],result1);
            }
            optReverse[i]=result1;
        }
        
        for(int i=0;i<n;i++)
        {
            int max=opt[i]+optReverse[i];
            ans=Math.max(max,ans);
        }
        
        return ans;
    }
};

 

posted on 2015-12-18 02:42  一心一念  阅读(133)  评论(0编辑  收藏  举报

导航