LEETCODE动态规划中等篇

1

523. 连续的子数组和

给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数。

示例 1:

输入: [23,2,4,6,7], k = 6
输出: True
解释: [2,4] 是一个大小为 2 的子数组,并且和为 6。
示例 2:

输入: [23,2,6,4,7], k = 6
输出: True
解释: [23,2,6,4,7]是大小为 5 的子数组,并且和为 42。
说明:

数组的长度不会超过10,000。
你可以认为所有数字总和在 32 位有符号整数范围内。

思路:重点考察当k为0的时候,判断数组中连续0 的个数。

class Solution {
    public boolean checkSubarraySum(int[] nums, int k) {
        if(k==0){
            int cnt=0;
            int max=0;
            for(int i=0;i<nums.length;i++){
                if(nums[i]==0){
                    cnt++;
                    max=Math.max(max,cnt);
                }else{
                    cnt=0;
                }
            }
            if(max>=2)
            return true;
            else return false;
        }
        
        for(int i=0;i<nums.length;i++){
            int num=nums[i];
            for(int j=i+1;j<nums.length;j++){
                num+=nums[j];
                if(num%k==0)
                return true;
            }
        }
        return false;
    }
}

2

96. 不同的二叉搜索树

给定一个整数 n,求以 1 … n 为节点组成的二叉搜索树有多少种?

示例:

输入: 3
输出: 5
解释:
给定 n = 3, 一共有 5 种不同结构的二叉搜索树:

1 3 3 2 1
\ / / / \
3 2 1 1 3 2
/ / \
2 1 2 3

思路
标签:动态规划
假设n个节点存在二叉排序树的个数是G(n),令f(i)为以i为根的二叉搜索树的个数,则
G(n) = f(1) + f(2) + f(3) + f(4) + … + f(n)
G(n)=f(1)+f(2)+f(3)+f(4)+…+f(n)

当i为根节点时,其左子树节点个数为i-1个,右子树节点为n-i,则
f(i) = G(i-1)*G(n-i)f(i)=G(i−1)∗G(n−i)

综合两个公式可以得到 卡特兰数 公式
G(n) = G(0)G(n-1)+G(1)(n-2)+…+G(n-1)*G(0)G(n)=G(0)∗G(n−1)+G(1)∗(n−2)+…+G(n−1)∗G(0)

class Solution {
    public int numTrees(int n) {
        int[] dp = new int[n+1];
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=n;i++){
            for(int j=1;j<i+1;j++){
                dp[i]+=dp[j-1]*dp[i-j];
            }
        }
        return dp[n];
    }
}

3

  1. 单词拆分
    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

说明:

拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:

输入: s = “leetcode”, wordDict = [“leet”, “code”]
输出: true
解释: 返回 true 因为 “leetcode” 可以被拆分成 “leet code”。
示例 2:

输入: s = “applepenapple”, wordDict = [“apple”, “pen”]
输出: true
解释: 返回 true 因为 “applepenapple” 可以被拆分成 “apple pen apple”。
注意你可以重复使用字典中的单词。
示例 3:

输入: s = “catsandog”, wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
输出: false

思路:暴力搜索,利用单词的排列组合与字符串进行匹配,但时间复杂度太高,利用dp的思想,设置一个记录状态的数组,表示当前前i个字符是否可以进行匹配,最后结果记录在dp【length】中。

import java.util.LinkedList;
import java.util.HashSet;
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for(int i = 1; i<=s.length(); i++){
            for(int j = 0; j<i; j++){
                if(dp[j] && set.contains(s.substring(j,i))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}

4

343. 整数拆分

给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。

示例 1:

输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1。
示例 2:

输入: 10
输出: 36
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。

巧解:

class Solution {
    public int integerBreak(int n) {
        if(n==2)
            return 1;
        if(n==3)
            return 2;
        if(n==4)
            return 4;
        int ans=1;
        while(n>=5){
            ans*=3;
            n-=3;
        }
        return ans*n;
    }
}

暴力
F(n)=max{i∗F(n−i)},i=1,2,…,n−1

上述表达式是表明n - i需要继续分解的情况,但如果n - i比F(n - i)要大,显然就不用再继续分解了。故我们还需要比较i * (n - i)与i * F(n - i)的大小关系。所以完整的表达式应该为:

F(n)=max{i∗F(n−i), i∗(n−i)},i=1,2,…,n−1

// 暴力解法
public int integerBreak(int n) {
    if (n == 2) {
        return 1;
    }
    int res = -1;
    for (int i = 1; i <= n - 1; i++) {
        res = Math.max(res, Math.max(i * (n - i), i * integerBreak1(n - i)));
    }
    return res;
}

设置备忘录记录已得出的fi
对于暴力搜索,通过图1不难得出该方法的时间复杂度为指数级别,显然不能满足题目要求。那么如此耗时的原因也是因为在递归的过程中计算了很多重复值。例如,图1中F(n-2)F(n−2)和F(n-3)F(n−3)至少重复计算了两次,并且在后面会有更多次重复运算,而这部分重复运算完全是没有必要的,如果我们每次求完一个F(i)F(i),都将其保存起来,下次再求的时候直接读取保存的值就行了,这显然会节省大量时间。既然有这个想法,那么相应的代码应该也不难实现,我们只要用一个数组存放每次的F(i)F(i),记为memory,这个数组我们一般称之为备忘录数组,具体实现如下:

// 记忆化搜索-自顶向下
int[] memory;
public int integerBreak(int n) {
    memory = new int[n + 1];
    return integerBreakHelper(n);
}
public int integerBreakHelper(int n) {
    if (n == 2) {
        return 1;
    }
    // memory的初始值为0,如果它不为0,说明已经计算过了,直接返回即可
    if (memory[n] != 0) {
        return memory[n];
    }
    int res = -1;
    for (int i = 1; i <= n - 1; i++) {
        res = Math.max(res, Math.max(i * integerBreakHelper(n - i), i * (n - i)));
    }
    // 将每次计算的结果保存到备忘录数组中
    memory[n] = res;
    return res;
}

dp算法

// 动态规划
public int integerBreak3(int n) {
    memory[2] = 1;
    for (int i = 3; i <= n; i++) {
        for ( int j = 1; j <= i - 1; j++) {
            memory[i] = Math.max(memory[i], Math.max(j * memory[i - j], j * (i - j)));
        }
    }
    return memory[n];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值