DP (6) -- Best Time to Buy and Sell Stock with Cooldown,Range Sum Query 2D - Immutable

来源:互联网 发布:行知外国语是区重点吗 编辑:程序博客网 时间:2024/06/17 10:16

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]


思路1:在每一天都有三种状态,have a stock, sell a stock, do nothing。三种状态的转移如下图:

nothing --> have a stock (buy)

nothing --> nothing

sell a stock --> nothing (cooldown)

have a stock --> have a stock

have a stock --> sell a stock

我们使用三个数组来记录每天三种状态可获得的最大收益。下面的解法也可以改成O(1)

    int maxProfit(vector<int>& prices) {        if(prices.size() < 2) return 0;        int cool[prices.size()] = {0};        int buy[prices.size()] = {0};        int sell[prices.size()] = {0};        cool[0] = 0;        buy[0] = - prices[0];        sell[0] = INT_MIN;        for(int i = 1; i < prices.size(); i++){            cool[i] = max(cool[i-1], sell[i-1]);            buy[i] = max(buy[i-1], cool[i-1] - prices[i]);            sell[i] = buy[i-1] + prices[i];        }        return max(sell[prices.size()-1], cool[prices.size()-1]);    }

思路2:使用两个数组来记录每一天的状态buy[]和sell[],buy和sell是指到第i天为止,所进行的最后一笔交易是buy/sell,可以获得的最大收益,所以我们有:

buy[i] = max(buy[i-1], sell[i-2] - prices[i])

sell[i] = max(sell[i-1], buy[i-1] + prices[i])

    int maxProfit(vector<int>& prices) {        if(prices.size() < 2) return 0;        int buy = max(-prices[0], -prices[1]);        int sell = max(prices[1] - prices[0], 0);        int formerSell = 0;        for(int i = 2; i < prices.size(); i++){            int tmp = sell;            sell = max(sell, buy + prices[i]);            buy = max(buy, formerSell - prices[i]);            formerSell = tmp;        }        return sell;    }


Range Sum Query 2D - Immutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Range Sum Query 2D
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:

Given matrix = [  [3, 0, 1, 4, 2],  [5, 6, 3, 2, 1],  [1, 2, 0, 1, 5],  [4, 1, 0, 1, 7],  [1, 0, 3, 0, 5]]sumRegion(2, 1, 4, 3) -> 8sumRegion(1, 1, 2, 2) -> 11


1. 用一个二维数组dp[i][j]记录以matrix[i-1][j-1]为右下角,matrix[0][0]为左上角的方块的和。

3. sumRegion = dp[row2+1][col2+1] - dp[row1][col2+1] - dp[row2+1][col1] + dp[row1][col1];

2. 声明dp[m+1][n+1]是为了省去边界条件的判断,简化代码。

class NumMatrix {public:    int** dp;    bool empty = false;    NumMatrix(vector<vector<int>> &matrix) {        if(matrix.empty()){            empty = true;            return;        }        int m = matrix.size(), n = matrix[0].size();        dp = new int* [m+1];        for(int i = 0; i < m+1; i++){            dp[i] = new int[n+1];            dp[i][0] = 0;        }        for(int j = 0; j < n+1; j++) dp[0][j] = 0;        for(int i = 1; i < m+1; i++){            int rowSum = 0;            for(int j = 1; j < n+1; j++){                rowSum += matrix[i-1][j-1];                int tmp = dp[i-1][j] + rowSum;                dp[i][j] = dp[i-1][j] + rowSum;            }        }    }    int sumRegion(int row1, int col1, int row2, int col2) {        if(empty) return 0;        return dp[row2+1][col2+1] - dp[row1][col2+1] - dp[row2+1][col1] + dp[row1][col1];    }};


0 0