leetcode打卡#day46 322. 零钱兑换、279. 完全平方数、139. 单词拆分

322. 零钱兑换

c++ 复制代码
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        //min count
        vector<int> dp(amount+1, INT_MAX);
        int n = coins.size();
        //init
        dp[0] = 0;
        //不包含顺序, 先物品再容量
        for (int i = 0; i < n; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                if (dp[j - coins[i]] != INT_MAX)
                    dp[j] = min(dp[j], dp[j - coins[i]]+1);
            }
        }
        if (dp[amount] == INT_MAX) return -1;
        return dp[amount];
    }

};

279. 完全平方数

c++ 复制代码
class Solution {
public:
    int numSquares(int n) {
        //dp 和为i的完全平方数的最少数量
        vector<int> dp(n+1, INT_MAX);
        dp[0] = 0;
        //没有顺序,先物品再容量
        for (int i = 1; i*i <= n; i++) {
            for (int j = i*i; j <= n; j++ ) {
                dp[j] = min(dp[j - i*i]+1, dp[j]);
            }
        }
        return dp[n];
    }
};

139. 单词拆分

c++ 复制代码
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> words(wordDict.begin(), wordDict.end());
        vector<bool> dp(s.length()+1, false);
        dp[0] = true;
        //有顺序,先容量再物品
        for (int i = 0; i <= s.size(); i++) {
            for (int j = 0; j < i; j++) {
                //取字串
                string str = s.substr(j, i - j);
                //若存在, 则返回true
                if (words.find(str) != words.end() && dp[j]) {
                    dp[i] = true;
                }
            }
        }
        return dp[s.size()];
    }
};
相关推荐
CoovallyAIHub9 小时前
语音AI Agent编排框架!Pipecat斩获10K+ Star,60+集成开箱即用,亚秒级对话延迟接近真人反应速度!
深度学习·算法·计算机视觉
木心月转码ing12 小时前
Hot100-Day14-T33搜索旋转排序数组
算法
会员源码网14 小时前
内存泄漏(如未关闭流、缓存无限增长)
算法
颜酱15 小时前
从0到1实现LFU缓存:思路拆解+代码落地
javascript·后端·算法
颜酱16 小时前
从0到1实现LRU缓存:思路拆解+代码落地
javascript·后端·算法
CoovallyAIHub1 天前
Moonshine:比 Whisper 快 100 倍的端侧语音识别神器,Star 6.6K!
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
速度暴涨10倍、成本暴降6倍!Mercury 2用扩散取代自回归,重新定义LLM推理速度
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
实时视觉AI智能体框架来了!Vision Agents 狂揽7K Star,延迟低至30ms,YOLO+Gemini实时联动!
算法·架构·github
CoovallyAIHub1 天前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github