LeetCode-动态规划总结(四)

股票交易

需要冷却期的股票交易

121.Best Time to Buy and Sell Stock

题目描述:只有一次交易机会,找出使得利润最大的价值。

class Solution {
    public int maxProfit(int[] prices) {
        if (null == prices || 0 == prices.length) {
            return 0;
        }
        int n = prices.length;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int price = prices[i];
            min = Math.min(min, price);
            max = Math.max(max, price - min);
        }
        return max;
    }
}

122. Best Time to Buy and Sell Stock II

题目描述:交易不限次数,但在下一次买之前一定要卖。

class Solution {
    public int maxProfit(int[] prices) {
        if (null == prices || 0 == prices.length) {
            return 0;
        }
        int n = prices.length;
        int[] s0 = new int[n];
        int[] s1 = new int[n];
        s0[0] = 0;
        s1[0] = - prices[0];
        for (int i = 1; i < n; i++) {
            int price = prices[i];
            s0[i] = Math.max(s0[i - 1], s1[i - 1] + price);
            s1[i] = Math.max(s1[i - 1], s0[i - 1] - price);
        }
        return Math.max(s0[n - 1], s1[n - 1]);
    }
}

309. Best Time to Buy and Sell Stock with Cooldown(Medium)

题目描述:交易之后需要有一天的冷却时间。

class Solution {
    public int maxProfit(int[] prices) {
        if (null == prices || 0 == prices.length) {
            return 0;
        }
        int n = prices.length;
        int[] s0 = new int[n];
        int[] s1 = new int[n];
        int[] s2 = new int[n];
        s0[0] = 0;
        s1[0] = - prices[0];
        s2[0] = Integer.MIN_VALUE;
        for (int i = 1; i < n; i++) {
            int price = prices[i];
            s0[i] = Math.max(s0[i - 1], s2[i - 1]);
            s1[i] = Math.max(s1[i - 1], s0[i - 1] - price);
            s2[i] = s1[i - 1] + price;
        }
        return Math.max(s0[n - 1], s2[n - 1]);
    }
}

需要交易费用的股票交易

714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
Buying at prices[0] = 1
Selling at prices[3] = 8
Buying at prices[4] = 4
Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

题目描述:每交易一次,都要支付一定的费用。

class Solution {
    public int maxProfit(int[] prices, int fee) {
        if (null == prices || 0 == prices.length) {
            return 0;
        }
        int n = prices.length;
        int[] s0 = new int[n];
        int[] s1 = new int[n];
        s0[0] = 0;
        s1[0] = - prices[0];
        for (int i = 1; i < n; i++) {
            int price = prices[i];
            s0[i] = Math.max(s0[i - 1], s1[i - 1] + price - fee);
            s1[i] = Math.max(s1[i - 1], s0[i - 1] - price);
        }
        return Math.max(s0[n - 1], s1[n - 1]);
    }
}

字符串编辑

删除两个字符串的字符使它们相等

583. Delete Operation for Two Strings (Medium)

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".

可以转换为求两个字符串的最长公共子序列问题。

class Solution {
    public int minDistance(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        int[][] dp = new int[len1 + 1][len2 + 1];
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        int commonList = dp[len1][len2];
        int delWord1 = len1 - commonList;
        int addWord2 = len2 - commonList;
        return delWord1 + addWord2;
    }
}

复制粘贴字符

650. 2 Keys Keyboard (Medium)

题目描述:最开始只有一个字符 A,问需要多少次操作能够得到 n 个字符 A,每次操作可以复制当前所有的字符,或者粘贴。

Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.
class Solution {
    public int minSteps(int n) {
        if (n <= 1) {
            return 0;
        }
        int[] dp = new int[n + 1];
        for (int i = 2; i <= n; i++) {
            dp[i] = i;
            for (int j = i / 2; j > 1; j--) {
                if (i % j == 0 && dp[j] + i / j < dp[i]) {
                    dp[i] = dp[j] + i / j;
                    break;
                }
            }
        }
        return dp[n];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值