• 股票问题专栏


    一、买卖股票的最佳时机Ⅰ-No.121

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
    如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。
    注意:你不能在买入股票前卖出股票。
    输入: [7,1,5,3,6,4]
    输出: 5
    解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
    注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
    class Solution {
        //只允许交易一次
        public int maxProfit(int[] prices) {
            if(prices == null || prices.length == 0) return 0;
            int min = prices[0];//表示第i天之前的股票最低价
            for(int i=0;i<prices.length;i++){
                min = Math.min(min,prices[i]);
                prices[i] = prices[i]-min;    
            }
            prices[0] = 0;
            int res = 0;
            for(int price : prices){
                res = Math.max(price,res);
            }
            return res;
        }
    }

    二、买卖股票的最佳时机Ⅱ-No.122

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
    设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
    注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

    输入: [7,1,5,3,6,4]
    输出: 5
    解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
    注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

    class Solution {
        //只允许交易一次
        public int maxProfit(int[] prices) {
            if(prices == null || prices.length == 0) return 0;
            int min = prices[0];//表示第i天之前的股票最低价
            for(int i=0;i<prices.length;i++){
                min = Math.min(min,prices[i]);
                prices[i] = prices[i]-min;    
            }
            prices[0] = 0;
            int res = 0;
            for(int price : prices){
                res = Math.max(price,res);
            }
            return res;
        }
    }

    三、买卖股票的最佳时机Ⅲ-No.123

    给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
    设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
    注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    输入: [3,3,5,0,0,3,1,4]
    输出: 6
    解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
    随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
    class Solution {
        //最多完成两笔交易,只要买入了就算交易
        public int maxProfit(int[] prices) {
            if(prices == null || prices.length == 0) return 0;
            //dp[n][k<2][0/1]:第i天持有/非持有股票并交易k次情况下的最大利润
            //dp[i][k][0]:前一天非持有交易k次;前一天持有交易k次+当天卖出
            //dp[i][k][1]:前一天持有交易k次;前一天非持有交易k-1次-当天买入
            int[][][] dp = new int[prices.length][3][2];
            dp[0][0][0] = 0;
            dp[0][0][1] = Integer.MIN_VALUE;
            dp[0][1][0] = 0;
            dp[0][1][1] = -prices[0];
            dp[0][2][0] = 0;
            dp[0][2][1] = Integer.MIN_VALUE;
            for(int i=1;i<prices.length;i++){
                for(int k=2;k>=1;k--){
                    dp[i][k][0] = Math.max(dp[i-1][k][0], dp[i-1][k][1] + prices[i]);
                    dp[i][k][1] = Math.max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i]);
                }
            }
            return Math.max(dp[prices.length-1][2][0],dp[prices.length-1][1][0]);
        }
    }

    四、买卖股票的最佳时机Ⅳ-No.188

    给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
    设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
    注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    输入: [2,4,1], k = 2
    输出: 2
    解释: 在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
    public class Solution {
    
        public int maxProfit(int k, int[] prices) {
            int len = prices.length;
            // 特判
            if (k == 0 || len < 2) {
                return 0;
            }
            if (k >= len / 2) {
                return greedy(prices, len);
            }
    
            // dp[i][j][K]:到下标为 i 的天数为止(从 0 开始),到下标为 j 的交易次数(从 0 开始)
            // 状态为 K 的最大利润,K = 0 表示不持股,K = 1 表示持股
            int[][][] dp = new int[len][k][2];
    
            // 初始化:把持股的部分都设置为一个较大的负值
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < k; j++) {
                    dp[i][j][1] = -9999;
                }
            }
    
            // 编写正确代码的方法:对两个"基本状态转移方程"当 i - 1 和 j - 1 分别越界的时候,做特殊判断,赋值为 0 即可
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < k; j++) {
                    if (i == 0) {
                        dp[i][j][1] = -prices[0];
                        dp[i][j][0] = 0;
                    } else {
                        if (j == 0) {
                            dp[i][j][1] = Math.max(dp[i - 1][j][1], -prices[i]);
                        } else {
                            // 基本状态转移方程 1
                            dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
                        }
                        // 基本状态转移方程 2
                        dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
                    }
                }
            }
            // 说明:i、j 状态都是前缀性质的,只需返回最后一个状态
            return dp[len - 1][k - 1][0];
        }
    
        private int greedy(int[] prices, int len) {
            // 转换为股票系列的第 2 题,使用贪心算法完成,思路是只要有利润,就交易
            int res = 0;
            for (int i = 1; i < len; i++) {
                if (prices[i - 1] < prices[i]) {
                    res += prices[i] - prices[i - 1];
                }
            }
            return res;
        }
    }

    五、最佳买卖股票时机含冷冻期-No.309

    给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。​
    设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
    你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
    输入: [1,2,3,0,2]
    输出: 3 
    解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
    class Solution {
        public int maxProfit(int[] prices) {
            if(prices ==  null || prices.length <= 1) return 0;
            //每天有两种状态:持有股票和非持有股票
            //非持有:前一天非持有,今天不买不卖;前一天持有,今天卖出;
            //持有:前一天持有,今天不买不卖;前一天非持有,今天买入;    
            //dp[n][0/1/2] (0:非持有 1:持有 2:)表示第i天两种状态下的最大利润
            int[][] dp = new int[prices.length][2];
            dp[0][0] = 0; 
            dp[0][1] = -prices[0];
    
            dp[1][0] = Math.max(dp[0][0], dp[0][1] + prices[1]);
            dp[1][1] = Math.max(dp[0][1], dp[0][0] - prices[1]);
            for(int i=2;i<prices.length;i++){
                dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]);
                dp[i][1] = Math.max(dp[i-1][1], dp[i-2][0] - prices[i]);
            }
            return dp[prices.length-1][0];
        }
    }

    六、买卖股票的最佳时机含手续费-No.714

    给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
    你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
    返回获得利润的最大值。
    注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费
    输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
    输出: 8
    解释: 能够达到的最大利润:  
    在此处买入 prices[0] = 1
    在此处卖出 prices[3] = 8
    在此处买入 prices[4] = 4
    在此处卖出 prices[5] = 9
    总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
    class Solution {
        public int maxProfit(int[] prices, int fee) {
            if(prices ==  null || prices.length == 0) return 0;
            
            int[][] dp = new int[prices.length][2];
            dp[0][0] = 0; 
            dp[0][1] = -prices[0];
            for(int i=1;i<prices.length;i++){
                dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]-fee);
                dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]);
            }
            return dp[prices.length-1][0];
        }
    }
  • 相关阅读:
    POJ 2955 Brackets 区间DP
    POJ 3311 Hie with the Pie 最短路+状压DP
    POJ 3615 Cow Hurdles(最短路径flyod)
    hdu 3790 最短路径dijkstra(多重权值)
    poj 3254 Corn Fields 状压DP
    状压DP
    poj2411 Mondriaan's Dream 状压DP
    M: Mysterious Conch 字符串哈希
    哈希(hash)理解
    域渗透:GPP(Group Policy Preferences)漏洞
  • 原文地址:https://www.cnblogs.com/qmillet/p/13100758.html
Copyright © 2020-2023  润新知