LeetCode322零钱兑换

1,递归+记忆化搜索。

从amount开始向下查找,找到0以后回来更新步数。

我的第一个写法,错误的

java 复制代码
  public int f(int[] coins, int amount, int step){
        if(amount == 0) return step;

        int temp_step = Integer.MAX_VALUE;
        for(int i = 0; i < coins.length; i++){
            int last = amount - coins[i];
            if(last < 0)continue;
            temp_step = Math.min(temp_step, f(coins, last, step + 1));
        }

        return temp_step ;
    }
    public int coinChange(int[] coins, int amount) {
        int res = f(coins, amount, 0);
        return res == Integer.MAX_VALUE? -1 : res;
    }

错误原因

step 这个参数是"外部状态",它让子问题无法复用(也是你之前版本缓存语义错的根源)

你现在虽然没缓存,但 step 的存在本质上说明:

  • 你在求的是"从初始调用累计到现在的总步数"

  • 而经典 coinChange 子问题应该求的是:"凑出 amount 的最少硬币数"(与从哪一层进来无关)

所以只要你想优化(加 memo),就会立刻遇到语义问题:f(amount, step) 不能只用 amount 做 key(因为 step 变了返回值就变了),导致 memo 很难做、或者做了也错。

✅ 正确做法:让递归函数只依赖 amount,返回"从 amount 到 0 的最少硬币数",不要带 step。

用我们能听得懂的话来说,这个step的含义是从初始调用累计到现在的总步数,并不会被路径上的节点更新

正确解法

java 复制代码
    static final int N = (int) (1e4 + 10);
    int[] has_use = new int[N];

    public int coinChange(int[] coins, int amount){
        if(amount == 0) return 0;

        if(has_use[amount] != 0)return has_use[amount];

        int temp_step = Integer.MAX_VALUE;
        for(int i = 0; i < coins.length; i++){
            int last = amount - coins[i];
            if(last < 0)continue;
            int r = coinChange(coins, last);
            if(r == -1)continue;
            temp_step = Math.min(temp_step, r + 1);
        }


        temp_step = (temp_step == Integer.MAX_VALUE) ? -1 : temp_step;
        has_use[amount] = temp_step;

        return temp_step ;
    }

换成自底向上的解法,dp

java 复制代码
  public int coinChange2dp(int[] coins, int amount){
        int[] dp = new int[amount+1];

        for(int i = 1; i <= amount; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 0; j < coins.length; j++){
                int last = i - coins[j];
                if(last < 0)continue;
                if(dp[last] == -1)continue;
                dp[i] = Math.min(dp[i], dp[last] + 1);
            }
            dp[i] = (dp[i] == Integer.MAX_VALUE) ? -1 : dp[i];
        }
        return dp[amount];
    }
相关推荐
张人玉1 分钟前
VisionPro 药物检测工具 学习笔记
算法·c#·机器视觉·vsionpro
_深海凉_3 分钟前
LeetCode热题100-前 K 个高频元素
算法·leetcode·职场和发展
周末也要写八哥12 分钟前
深度剖析:动态规划的分类及实例
算法·动态规划
星马梦缘19 分钟前
离散数学——二元关系 作战记录
算法·离散数学
LDG_AGI23 分钟前
【搜索引擎】Elasticsearch(五):prefix前缀匹配方法大全(包含search_as_you_type等6种解法)
人工智能·深度学习·算法·elasticsearch·搜索引擎
她说彩礼65万26 分钟前
C语言 函数指针
c语言·开发语言·算法
王老师青少年编程26 分钟前
csp信奥赛C++高频考点专项训练之贪心算法 --【排序贪心】:纪念品分组
c++·算法·贪心·csp·信奥赛·排序贪心·纪念品分组
贾斯汀玛尔斯29 分钟前
每天学一个算法--贪心算法(Greedy Algorithm)
算法·贪心算法
前端摸鱼匠31 分钟前
【AI大模型春招面试题24】什么是“注意力分数”?如何计算?其大小反映了什么?
人工智能·算法·ai·面试·大模型·求职招聘
MicroTech202531 分钟前
融合残差结构的量子电路算法:MLGO微算法科技拓展量子机器学习频谱边界
科技·算法·机器学习