121. Best Time to Buy and Sell Stock & 122 II & 123 III & 188 IV

来源:互联网 发布:python 软件开发 编辑:程序博客网 时间:2024/06/06 19:59

一系列问题。数组prices[i]代表第i天股票的价格。
1. Best Time to Buy and Sell Stock

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.

第一个问题,只允许进行一次交易,即一次买进和一次卖出,找出最大获利。这个问题相当于寻找max{price[j]-price[i]},i < j(先买进才能卖出)。如果对于i=0~j-1,都计算price[j]-price[i],时间复杂度为O(n^2)。其实对于每个j,price[j]-price[i]的最大值为min{price[i]},i=0~j-1. 所以用low记录目前最小值,如果当前j大于最小值,就更新最大收益;如果小于最小值,则更新最小值。

public class Solution {    public int maxProfit(int[] prices) {        if(prices.length == 0) return 0;        int low = prices[0],max = 0;   //low记录当前最小值        for(int i = 0; i < prices.length ;i++){            if(low > prices[i]){                low = prices[i];            }            else if(prices[i] - low > max) max = prices[i] - low;        }        return max;    }}

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

与上一题不同,允许多次买入卖出。
贪婪。如果price[i]-price[i-1] > 0,则ans += price[i]-price[i-1]

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

3、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.

不同之处,只允许最多两次完整的交易。
j = 0~n-1相当于在0~j,进行一次完整的交易;j~n-1进行一次完整的交易,两者之和即为收益,求最大收益。
对于0~j,需要前向求取最大收益;对于j~n-1,需要逆向求取最大值,然后合并求取最大增益。代码如下:

public class Solution {    public int maxProfit(int[] prices) {       if(prices.length == 0) return 0;       int n = prices.length;       int ans = 0;       //正向       int[] pro = new int[n];       int low = prices[0];       for(int i = 1;i < n;i++){           if(prices[i] < low){               low = prices[i];           }           pro[i] = Math.max(pro[i-1], prices[i] - low);       }        //逆向       int[] proReverse = new int[n];       int high = prices[n-1];       for(int i = n-2;i >= 0;i--){           if(prices[i] > high){               high = prices[i];           }           proReverse[i] = Math.max(proReverse[i+1],high - prices[i]);       }       //整合       for( int i = 0;i < n;i++){           ans = Math.max(ans,pro[i]+proReverse[i]);       }       return ans;    }}

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

最多进行k次交易。
1、如果k > prices.length/2,就和第二题是一样的,没有次数限制。
2、k <= prices.length/2.动态规划。dp[i][j]代表到prices[j]时,至多进行了i次交易的最大收益。
dp[i][j] = max{dp[i][j-1],prices[j]+dp[i-1][x]-prices[x]},x = 0~j-1
=max{dp[i][j-1],prices[j]+max{dp[i-1][x]-prices[x]}};
dp[0][i] = dp[i][0] = 0;

    public int maxProfit(int k, int[] prices) {        int n = prices.length;        if(k <= 0 || n <= 0) return 0;        if(k > n/2){   //同2            int ans = 0;            for(int i = 1;i < n;i++){                if(prices[i] - prices[i-1] > 0) ans += prices[i]-prices[i-1];            }            return ans;        }        //k次        int[][] dp = new int[k+1][n];        for(int i = 1; i <= k;i++){            int local = -prices[0];            for(int j = 1;j < n;j++){                dp[i][j] = Math.max(dp[i][j-1],prices[j]+local);                local = Math.max(local,dp[i-1][j]-prices[j]);            }        }        return dp[k][n-1];    }
0 0
原创粉丝点击