买股票问题

股票问题

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems

问题1:股票的最大利润
假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?

class Solution {
    public int maxProfit(int[] prices) {
        int length = prices.length;
        int min = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i = 0; i < length; i++) {
            //最小买入
            if (prices[i] < min){
                min = prices[i];
            }else if(prices[i] - min > maxProfit){
            	//最大卖出
                maxProfit = prices[i] - min;
            }
        }
        return maxProfit;
    }
}

问题2:买卖股票的最佳时机 II
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大利润 。

//针对当天不持有还是持有
class Solution {
    public int maxProfit(int[] prices) {
        int length = prices.length;
        int[][] dp = new int[length][2];
        //第一天不持有利润为0,持有
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < length; i++) {
        	//不持有
            dp[i][0] = Math.max(dp[i - 1][1] + prices[i], dp[i-1][0]);
            //持有
            dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]);
        }
        //不持有利润更大
        return dp[length - 1][0];
    }
}

问题3:买卖股票的最佳时机含手续费
给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。

class Solution {
    public int maxProfit(int[] prices, int fee) {
        int length = prices.length;
        int[][] dp = new int[length][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < 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[length-1][0];
    }
}

问题4:最佳买卖股票时机含冷冻期
给定一个整数数组prices,其中第 prices[i] 表示第 i 天的股票价格 。​

设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length == 0) {
            return 0;
        }

        int n = prices.length;
        // f[i][0]: 手上持有股票的最大收益
        // f[i][1]: 手上不持有股票,并且处于冷冻期中的累计最大收益
        // f[i][2]: 手上不持有股票,并且不在冷冻期中的累计最大收益
        int[][] f = new int[n][3];
        f[0][0] = -prices[0];
        for (int i = 1; i < n; ++i) {
            f[i][0] = Math.max(f[i - 1][0], f[i - 1][2] - prices[i]);
            f[i][1] = f[i - 1][0] + prices[i];
            f[i][2] = Math.max(f[i - 1][1], f[i - 1][2]);
        }
        return Math.max(f[n - 1][1], f[n - 1][2]);
    }
}

问题5:买卖股票的最佳时机 III(困难)
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

class Solution {
        public int maxProfit(int[] prices) {
            int length = prices.length;
            int b1 = -prices[0];
            int s1 = 0;
            int b2 = -prices[0];
            int s2 = 0;
            for (int i = 1; i < length; i++) {
                b1 = Math.max(b1, -prices[i]);
                s1 = Math.max(s1, b1 + prices[i]);
                b2 = Math.max(b2, s1 - prices[i]);
                s2 = Math.max(s2, b2 + prices[i]);
            }
            return s2;
        }
    }

问题6:买卖股票的最佳时机 IV(困难)
给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

/**
     * 1、确定状态
     * 用buy[i][j]表示对于数组prices[0..i]中的价格而言,进行恰好j笔交易,并且当前手上持有一支股票,这种情况下的最大利润。
     * 用sell[i][j]表示恰好进行j笔交易,并且当前手上不持有股票,这种情况下的最大利润。
     * 2、转态转移
     * 对于buy[i][j],我们考虑当前手上持有的股票是否是在第 i 天买入的
     * buy[i][j]=max{buy[i−1][j],sell[i−1][j]−price[i]}
     * 同理对于 sell[i][j],是否是在第 i 天卖出
     * 如果是第 i 天卖出的,那么在第 i−1 天时,我们手上持有股票,对应状态 buy[i−1][j−1],并且需要增加 prices[i] 的卖出收益;
     * 如果不是第 i 天卖出的,那么在第 i−1 天时,我们手上不持有股票,对应状态 sell[i−1][j]。那么我们可以得到状态转移方程:
     * sell[i][j]=max{sell[i−1][j],buy[i−1][j−1]+price[i]}
     * 3、边界条件
     */
    class Solution {
        public int maxProfit(int k, int[] prices) {
            if (prices.length == 0) {
                return 0;
            }

            int n = prices.length;
            //n天最多进行n/2笔交易
            k = Math.min(k, n / 2);
            int[][] buy = new int[n][k + 1];
            int[][] sell = new int[n][k + 1];

            buy[0][0] = -prices[0];
            sell[0][0] = 0;
            for (int i = 1; i <= k; ++i) {
                buy[0][i] = sell[0][i] = Integer.MIN_VALUE / 2;
            }

            for (int i = 1; i < n; ++i) {
                buy[i][0] = Math.max(buy[i - 1][0], sell[i - 1][0] - prices[i]);
                for (int j = 1; j <= k; ++j) {
                    buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j] - prices[i]);
                    sell[i][j] = Math.max(sell[i - 1][j], buy[i - 1][j - 1] + prices[i]);
                }
            }

            return Arrays.stream(sell[n - 1]).max().getAsInt();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值