Best Time to Buy and Sell Stock 주식 매매 문제의 몇 가지 변형

4003 단어
내 Letcode 질문에 대한 해답은 코드가 Github로 이동합니다.https://github.com/chenxiangcyr/leetcode-answers
LeetCode 121. Best Time to Buy and Sell Stock
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.
class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length < 2) {
            return 0;
        }
        
        int profit = 0;
        
        // two iteration solution, time limitation exceed
        /*
        for(int i = 0; i < prices.length - 1; i++) {
            for(int j = i + 1; j < prices.length; j++) {
                if(prices[j] - prices[i] > profit) {
                    profit = prices[j] - prices[i];
                }
            }
        }
        */
        
        int buyIdx = 0;
        int sellIdx = 1;
        while(sellIdx < prices.length) {
            if(prices[buyIdx] > prices[sellIdx]) {
                buyIdx = sellIdx;
            } else {
                int t = prices[sellIdx] - prices[buyIdx];
                if(t > profit) {
                    profit = t;
                }
            }
            
            sellIdx++;
        }
        
        return profit;
    }
}

LeetCode 122. Best Time to Buy and Sell Stock II
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(int[] prices) {
        int profit = 0;
        
        // null pointer check
        if(prices == null) {
            return profit;
        }
        
        for(int i = 0; i < prices.length - 1; i++) {
            if(prices[i + 1] > prices[i]) {
                profit = profit + (prices[i + 1] - prices[i]);
            }
        }
        
        return profit;
    }
}

LeetCode 123. Best Time to Buy and Sell Stock III
Design an algorithm to find the maximum profit. You may complete at most two transactions.
class Solution {
    public int maxProfit(int[] prices) {
        int buy1 = Integer.MAX_VALUE;
        int buy2 = Integer.MAX_VALUE;
        int sell1 = 0;
        int sell2 = 0;

        for (int i = 0; i < prices.length; i++) {
            buy1 = Math.min(buy1, prices[i]);
            sell1 = Math.max(sell1, prices[i] - buy1);
            
            buy2 = Math.min(buy2, prices[i] - sell1);
            sell2 = Math.max(sell2, prices[i] - buy2);
        }

        return sell2;
    }
}

LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
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) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length < 2) return 0;
        
        // DP - interative solution
        // profit1[i]: the profit if sell on day i
        // profit2[i]: the profit if do nothing on day i
        int[] profit1 = new int[prices.length];
        int[] profit2 = new int[prices.length];
        
        profit1[0] = 0;
        profit2[0] = 0;
        
        for(int i = 1; i < prices.length; i++) {
            profit1[i] = Math.max(profit1[i - 1] + prices[i] - prices[i - 1], profit2[i - 1]);
            profit2[i] = Math.max(profit1[i - 1], profit2[i - 1]);
        }
        
        return Math.max(profit1[profit1.length - 1], profit2[profit2.length - 1]);
    }
}

좋은 웹페이지 즐겨찾기