代码随想录算法训练营第33天 | 第九章动态规划 part06

第九章 动态规划 part06

322. 零钱兑换

如果求组合数就是外层 for 循环遍历物品,内层 for 遍历背包。
如果求排列数就是外层 for 遍历背包,内层 for 循环遍历物品。

这句话结合本题,大家要好好理解。

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> dp(amount + 1, INT_MAX));
         dp[0]=0;
         for (int coin : coins) { // 遍历物品   
            for (int j = coin; j <= amount; j++) { 
                    dp[j] = min(dp[j], dp[j - coin] + 1);
                }
            }
            if(dp[amount]==INT_MAX))
            return -1;
            return dp[amount];
    }
    
};

首先凑足总金额为0所需钱币的个数一定是0,那么dp[0] = 0;

其他下标对应的数值呢?

考虑到递推公式的特性,dp[j]必须初始化为一个最大的数,否则就会在min(dp[j - coins[i]] + 1, dp[j])比较的过程中被初始值覆盖。

279. 完全平方数

本题和 322. 零钱兑换 基本是一样的,大家可以先自己尝试做一做。

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n + 1, INT_MAX);
        std::vector<int> squares;// 将 1 的平方到 100 的平方依次添加到 vector 中
    for (int i = 1; i*i <= n; ++i) 
        squares.push_back(i * i);
        dp[0]=0;
        for (int square : squares) { // 遍历物品   
            for (int j = square; j <= n; j++) { 
                    dp[j] = min(dp[j], dp[j - square] + 1);
                }
            }
            return dp[n];
    }
};

这题和上一题几乎完全一模一样,不过平方数数组需要自己去设计,整体难度不高。

139. 单词拆分


这题的思路还是很清晰的,就是很直观的想法,其实感觉动态规划都没有用多少,就是单纯用BOOL数组标记点,一遍遍的去遍历。

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        vector<bool> dp(s.size() + 1, false);
        dp[0] = true;
        for (int i = 1; i <= s.size(); i++) {   // 遍历背包
            for (int j = 0; j < i; j++) {       // 遍历物品
                string word = s.substr(j, i - j); //substr(起始位置,截取的个数)
                if (wordSet.find(word) != wordSet.end() && dp[j]) {
                    dp[i] = true;
                }
            }
        }
        return dp[s.size()];
    }
};

回溯法:回溯法通过递归尝试每一个可能的分割点。我们从字符串的第一个字符开始,尝试所有可能的前缀是否在字典中。如果找到一个前缀在字典中,则递归继续检查剩余部分的字符串,直到字符串的每一部分都能在字典中找到,或者尝试所有可能的分割失败。

class Solution {
private:
    bool backtracking (const string& s, const unordered_set<string>& wordSet, int startIndex) {
        if (startIndex >= s.size()) {
            return true;
        }
        for (int i = startIndex; i < s.size(); i++) {
            string word = s.substr(startIndex, i - startIndex + 1);
            if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, i + 1)) {
                return true;
            }
        }
        return false;
    }
public:

    bool wordBreak(string s, vector<string>& wordDict) {
       unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        return backtracking(s, wordSet, 0);
    }
};

函数 backtracking 通过递归的方式,检查从 startIndex 开始的字符串能否通过字典中的单词拼接。
从 startIndex 开始,遍历到字符串末尾,逐渐增加子串长度。s.substr(startIndex, i - startIndex + 1) 提取从 startIndex 到 i 的子串。
例如,对于 s = “leetcode”,当 startIndex = 0 时,子串依次为 “l”, “le”, “lee”, “leet” 等。如果提取的子串 word 在字典中,则进一步递归调用 backtracking(s, wordSet, i + 1),检查剩下的部分是否也能通过字典中的单词拼接。
如果递归返回 true,意味着找到了一条完整的分割路径,因此立即返回 true。
太久没看回溯算法,生疏的很。
递归的过程中有很多重复计算,可以使用数组保存一下递归过程中计算的结果。

这个叫做记忆化递归,这种方法我们之前已经提过很多次了。

使用memory数组保存每次计算的以startIndex起始的计算结果,如果memory[startIndex]里已经被赋值了,直接用memory[startIndex]的结果。

class Solution {
private:
    bool backtracking (const string& s,
            const unordered_set<string>& wordSet,
            vector<bool>& memory,
            int startIndex) {
        if (startIndex >= s.size()) {
            return true;
        }
        // 如果memory[startIndex]不是初始值了,直接使用memory[startIndex]的结果
        if (!memory[startIndex]) return memory[startIndex];
        for (int i = startIndex; i < s.size(); i++) {
            string word = s.substr(startIndex, i - startIndex + 1);
            if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, memory, i + 1)) {
                return true;
            }
        }
        memory[startIndex] = false; // 记录以startIndex开始的子串是不可以被拆分的
        return false;
    }
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        vector<bool> memory(s.size(), 1); // -1 表示初始化状态
        return backtracking(s, wordSet, memory, 0);
    }
};

memory 数组是用来记录从某个位置 startIndex 开始的子字符串是否可以成功拆分成字典中的单词组合。它的作用主要是缓存中间结果,避免重复计算。

在 wordBreak 函数中,memory 被初始化为一个大小为 s.size() 的布尔数组,初始值为 true。这里的 true 并不表示某个位置的子串可以被拆分,而是表示这个位置的子问题尚未被计算过。

当回溯函数发现从 startIndex 开始的子串无法被有效拆分时,它会将 memory[startIndex] 设置为 false,表示从该位置无法完成拆分,后续不需要再尝试。
如果某个位置可以成功拆分,则直接返回 true,后续如果再次回到该位置,可以立即使用之前的计算结果。

背包问题总结篇!

在这里插入图片描述
在这里插入图片描述

第二十二算法训练营主要涵盖了Leetcode题目中的三道题目,分别是Leetcode 28 "Find the Index of the First Occurrence in a String",Leetcode 977 "有序数组的平方",和Leetcode 209 "长度最小的子数组"。 首先是Leetcode 28题,题目要求在给定的字符串中找到第一个出现的字符的索引。思路是使用双指针来遍历字符串,一个指向字符串的开头,另一个指向字符串的结尾。通过比较两个指针所指向的字符是否相等来判断是否找到了第一个出现的字符。具体实现的代码如下: ```python def findIndex(self, s: str) -> int: left = 0 right = len(s) - 1 while left <= right: if s[left == s[right]: return left left += 1 right -= 1 return -1 ``` 接下来是Leetcode 977题,题目要求对给定的有序数组中的元素进行平方,并按照非递减的顺序返回结果。这里由于数组已经是有序的,所以可以使用双指针的方法来解决问题。一个指针指向数组的开头,另一个指针指向数组的末尾。通过比较两个指针所指向的元素的绝对值的大小来确定哪个元素的平方应该放在结果数组的末尾。具体实现的代码如下: ```python def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 right = len(nums) - 1 ans = [] while left <= right: if abs(nums[left]) >= abs(nums[right]): ans.append(nums[left ** 2) left += 1 else: ans.append(nums[right ** 2) right -= 1 return ans[::-1] ``` 最后是Leetcode 209题,题目要求在给定的数组中找到长度最小的子数组,使得子数组的和大于等于给定的目标值。这里可以使用滑动窗口的方法来解决问题。使用两个指针来表示滑动窗口的左边界和右边界,通过移动指针来调整滑动窗口的大小,使得滑动窗口中的元素的和满足题目要求。具体实现的代码如下: ```python def minSubArrayLen(self, target: int, nums: List[int]) -> int: left = 0 right = 0 ans = float('inf') total = 0 while right < len(nums): total += nums[right] while total >= target: ans = min(ans, right - left + 1) total -= nums[left] left += 1 right += 1 return ans if ans != float('inf') else 0 ``` 以上就是第二十二算法训练营的内容。通过这些题目的练习,可以提升对双指针和滑动窗口等算法的理解和应用能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值