LeetCode解題(20200630)——股票問題大集合

劍指 Offer 63. 股票的最大利潤

/**
 * Copyright (C), 2018-2020
 * FileName: maxProfit63
 * Author:   xjl
 * Date:     2020/6/30 10:29
 * Description: 劍指 Offer 63. 股票的最大利潤
 */
package Array;

public class maxProfit63 {

    public int maxProfit(int[] prices) {
        if (prices.length == 0) return 0;
        int max = 0;
        int min = prices[0];
        for (int i = 1; i < prices.length; i++) {
            max = Math.max(max, prices[i] - min);
            min = Math.min(min, prices[i]);
        }
        return max;
    }

    public int maxProfit2(int[] prices) {
        if (prices.length == 0) return 0;

        int max = 0;
        int min = Integer.MAX_VALUE;

        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < min) {
                min = prices[i];
            }
            max = Math.max(prices[i] - min, max);
        }
        return max;
    }


}

901. 股票價格跨度

class StockSpanner {
    Stack<Integer> prices, weights;

    public StockSpanner() {
        prices = new Stack();
        weights = new Stack();
    }

    public int next(int price) {

        int w = 1;
        while (!prices.isEmpty() && prices.peek() <= price) {
            prices.pop();
            w += weights.pop();
        }
        
        prices.push(price);
        weights.push(w);
        return w;
    }
}

121. 買賣股票的最佳時機

/**
 * Copyright (C), 2018-2020
 * FileName: maxProfit
 * Author:   xjl
 * Date:     2020/6/30 10:21
 * Description: 121. 買賣股票的最佳時機
 */
package Array;

import org.junit.Test;

public class maxProfit {

    public int maxProfit(int[] prices) {
        int min = Integer.MAX_VALUE;
        int max = 0;

        for (int i = 0; i < prices.length; i++) {
            if (prices[i] <= min) {
                min = prices[i];
            }
            max = Math.max(prices[i] - min, max);
        }
        return max;
    }

    @Test
    public void test() {
        int[] array={7,1,5,3,6,4};
        int i = maxProfit(array);
        System.out.println(i);
    }

}

122. 買賣股票的最佳時機 II

/**
 * Copyright (C), 2018-2020
 * FileName: maxProfit122
 * Author:   xjl
 * Date:     2020/6/30 10:36
 * Description: 122. 買賣股票的最佳時機 II
 */
package Array;

import org.junit.Test;

public class maxProfit122 {

    public int maxProfit(int[] prices) {
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
            int tmp = prices[i] - prices[i - 1];
            if (tmp > 0) {
                profit += tmp;
            }
        }
        return profit;
    }

    @Test
    public void test() {
        int[] array = {7,6,4,3,1};
        int i = maxProfit(array);
        System.out.println(i);

    }
}

123. 買賣股票的最佳時機 III

難度有點大

188. 買賣股票的最佳時機 IV

 

309. 最佳買賣股票時機含冷凍期

 

714. 買賣股票的最佳時機含手續費

/**
 * Copyright (C), 2018-2020
 * FileName: maxProfit714
 * Author:   xjl
 * Date:     2020/6/30 10:53
 * Description: 714. 買賣股票的最佳時機含手續費
 */
package Array;

public class maxProfit714 {
    public int maxProfit(int[] prices, int fee) {
        int cash = 0;
        int hold = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            cash = Math.max(cash, hold + prices[i] - fee);
            hold = Math.max(hold, cash - prices[i]);
        }
        return cash;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章