LeetCode:Best Time to Buy and Sell Stock I & II & III

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) {
    int len=prices.length;
    if(len==0) return 0;
    int minn=prices[0],profit=0;
    for(int i=1;i<len;i++)
    {
        if(prices[i]<minn)
        {    
            minn=prices[i];
            continue;
        }
        if(prices[i]-minn>profit)profit=prices[i]-minn;
    }
    return profit;
}
}

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

與第一個不同的是允許多次買賣,因此只要是遞增序列就就將差值累計,最後得到結果

public class Solution {
    public int maxProfit(int[] prices) {
        int len=prices.length;
        int i,res=0;
        if(len<1)return 0;
        for(i=0;i<len-1;i++)
            {
                if(prices[i]<prices[i+1])res+=prices[i+1]-prices[i];
            }
        return res;
    }
}

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.

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 divProfit(int [] prices,int low,int hight)
    {
        int minn=prices[low],profit=0;
        for(int i=low;i<=hight;i++)
        {
            if(minn>prices[i])
            {
                minn=prices[i];
                continue;
            }
            if(profit<prices[i]-minn)profit=prices[i]-minn;
            
        }
        return profit;
    }
    
    public int maxProfit(int[] prices) {
        int len=prices.length;
        if(len<=1)return 0;
        int maxn=0,profit;
        for(int i=1;i<len-1;i++)
        {
            profit=divProfit(prices,0,i)+divProfit(prices,i,len-1);
            
            if(maxn<profit)maxn=profit;
        }
        return maxn;
    }
}
在此之上進行改進,記錄中間數據,減少遍歷次數:

public class Solution {
    public int maxProfit(int[] prices) {
        final int len=prices.length;
        if(len<=1)return 0;
        int [] maxHead =new int [len];
        maxHead[0]=0;
        int minPrice = prices[0], maxProfit = 0;
        for(int i=1;i<len;i++)
        {
            minPrice=Math.min(minPrice,prices[i-1]);
            if(maxProfit<prices[i]-minPrice)
            {
                maxProfit=prices[i]-minPrice;
                maxHead[i]=maxProfit;
            }
        }
        int maxPrice=prices[len-1];
        int profit=maxHead[len-1];
        maxProfit=0;
        for(int i=len-2;i>=0;i--)
        {
            maxPrice=Math.max(maxPrice,prices[i+1]);
            if(maxProfit<maxPrice-prices[i])
            {
                maxProfit=maxPrice-prices[i];
            }
            if(profit<maxHead[i]+maxProfit)
            {
                profit=maxHead[i]+maxProfit;
            }
        }
        return profit;
    }
}


發佈了34 篇原創文章 · 獲贊 0 · 訪問量 9656
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章