Leetcode Best Time to Buy and Sell Stock 全集

来源:互联网 发布:hadoop作业调度算法 编辑:程序博客网 时间:2024/06/16 17:11

Best Time to Buy and Sell Stock I

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 1:

Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]Output: 0In this case, no transaction is done, i.e. max profit = 0.

public int maxProfit(int[] prices) {        int min = Integer.MAX_VALUE;        int max = 0;        for (int i =0; i < prices.length; i++) {            min = Math.min(min,prices[i]);            max = Math.max(max,prices[i] - min);        }        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).

public int maxProfit(int[] prices) {        int sum = 0;        for (int i = 0; i < prices.length - 1 ; i++) {            if (prices[i+1] > prices[i]) 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.

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

public int maxProfit(int[] prices) {        int first_buy = Integer.MIN_VALUE, first_sell = 0;        int second_buy = Integer.MIN_VALUE, second_sell = 0;                for (int price : prices) {            if (first_buy < -price) first_buy = -price;            if (first_sell < first_buy + price) first_sell = first_buy + price;            if (second_buy < first_sell - price) second_buy = first_sell - price;            if (second_sell < price + second_buy) second_sell = price + second_buy;        }        return second_sell;    }


Best Time to Buy and Sell Stock IV

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 k transactions.

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


一次交易就是 一次买和卖。如果 交易数 等于 len /2 的话,就是说 我们可以随意交易

定义一个二维数组DP【多少次交易】【价格】

dp[i][j] = Math.max(dp[i][j - 1], tmp_max + prices[j]);
表示 如果经行 i 次 交易的话, 最大 的收益

tmp_max = Math.max(tmp_max, dp[i - 1][j - 1] - prices[j]);
表示 是否要 买 现在的price, 还是 留着 现在的 profit 

public int maxProfit(int k, int[] prices) {        int len = prices.length;        if (k > len / 2) return as_many_as_transactions_we_want(k, prices);        int[][] dp = new int[k + 1][len];        for (int i = 1; i <= k; i++) {            int tmp_max = -prices[0];            for (int j = 1; j < len; j++) {                dp[i][j] = Math.max(dp[i][j - 1], tmp_max + prices[j]);                tmp_max = Math.max(tmp_max, dp[i - 1][j - 1] - prices[j]);            }        }        return dp[k][len - 1];    }        private int as_many_as_transactions_we_want(int k, int[] prices) {        int profit = 0;        for (int i = 0; i < prices.length - 1; i++) {            if (prices[i + 1] > prices[i]) profit += prices[i + 1] - prices[i];        }        return profit;    }

Best Time to Buy and Sell Stock with Cooldown

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

Example:

prices = [1, 2, 3, 0, 2]maxProfit = 3transactions = [buy, sell, cooldown, buy, sell]

public int maxProfit(int[] prices) {        if (prices == null || prices.length < 2) return 0;        int len = prices.length;        int[] buy = new int[len];        int[] sell = new int[len];        buy[0] = -prices[0];        buy[1] = Math.max(-prices[0], -prices[1]);        sell[0] = 0;        sell[1] = Math.max(sell[0], prices[1] - prices[0]);                for (int i = 2; i < len; i++) {            buy[i] = Math.max(buy[i - 1], sell[i - 2] - prices[i]);            sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);        }        return sell[len - 1];    }








原创粉丝点击