当前位置: 首页 > wzjs >正文

重庆大渝网官网沈阳seo合作

重庆大渝网官网,沈阳seo合作,企业微信公众号,合肥专业做网站公司有哪些LCR 103. 零钱兑换 零钱找零 1.暴力 思路 大问题拆解子问题 边界条件: amount刚好等于0 说明方法起效(极限) amount小于0 说明价格不能再减 1 dp(coins, n - coin) 表示每次可以取的次数 class Solution {public int coinChange(int[] coins, in…

LCR 103. 零钱兑换

零钱找零

1.暴力

思路

大问题拆解子问题

边界条件: amount刚好等于0 说明方法起效(极限)

                amount小于0 说明价格不能再减

1 + dp(coins, n - coin) 表示每次可以取的次数

class Solution {public int coinChange(int[] coins, int amount) {return dp(coins, amount);}public int dp(int[] coins,int amount){if (amount == 0) return 0;if (amount < 0) return -1;int res = Integer.MAX_VALUE;for(int coin : coins){int subproblem = dp(coins, amount - coin);if (subproblem == -1) continue;res =  Math.min(res, subproblem + 1);}return res == Integer.MAX_VALUE ? -1 : res;}public static void main(String[] args) {int[] coins = {1,2,5};int amount = 11;Solution s = new Solution();System.out.println(s.coinChange(coins, amount));}
}

超时

2.剪枝

因为每个的amount都被重复计算

设置memo 如果memo被计算过直接返回

import java.util.Arrays;class Solution {int[] memo;public int coinChange(int[] coins, int amount) {memo  = new int[amount + 1];/*     for (int i = 0; i < amount + 1; i++) {memo[i] = -3;}*/Arrays.fill(memo,-3);return dp(coins, amount);}public int dp(int[] coins,int amount){if (amount == 0) return 0;if (amount < 0) return -1;int res = Integer.MAX_VALUE;if (memo[amount] != -3) return memo[amount];for(int coin : coins){int subproblem = dp(coins, amount - coin);if (subproblem == -1) continue;res =  Math.min(res, subproblem + 1);}return  memo[amount] = res == Integer.MAX_VALUE ? -1 : res;//   return res == Integer.MAX_VALUE ? -1 : res;}public static void main(String[] args) {int[] coins = {1,2,5};int amount = 11;Solution s = new Solution();System.out.println(s.coinChange(coins, amount));}
}

3.灵神算法

不一样的思路cache对应dfs

如果当前的节点费用小于当前coin 则返回上一个节点的相同费用

如果C小于0 则返回-1 说明不存在刚好的策略

而对每一个选项都有选和不选的两种选择

class Solution {private int[] coins;private int[][] cache;public int coinChange(int[] coins, int amount) {this.coins = coins;int n = coins.length;cache = new int[n][amount + 1];for (int i = 0; i < n; i++)Arrays.fill(cache[i], -1); // -1 表示没用访问过int ans = dfs(n - 1, amount);return ans < Integer.MAX_VALUE / 2 ? ans : -1;}private int dfs(int i, int c) {if (i < 0) return c == 0 ? 0 : Integer.MAX_VALUE / 2; // 除 2 是防止下面 + 1 溢出if (cache[i][c] != -1) return cache[i][c];if (c < coins[i]) return cache[i][c] = dfs(i - 1, c);return cache[i][c] = Math.min(dfs(i - 1, c), dfs(i, c - coins[i]) + 1);}
}作者:灵茶山艾府
链接:https://leetcode.cn/problems/gaM7Ch/solutions/2128055/jiao-ni-yi-bu-bu-si-kao-dong-tai-gui-hua-kmrs/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

核心思路

  1. 状态定义

    • cache[i][c] 表示使用前 i 种硬币组成金额 c 的最少硬币数量
    • 如果 cache[i][c] = -1,表示该状态还未被计算过
  2. 状态转移

    • 对于每种硬币 coins[i],有两种选择:
      1. 不使用当前硬币 coins[i],则状态转移为 dfs(i-1, c)
      2. 使用至少一枚当前硬币 coins[i],则状态转移为 dfs(i, c-coins[i]) + 1
    • 取两种选择中的较小值作为结果
  3. 初始条件

    • 当 i < 0(没有硬币可用)时:
      • 如果 c == 0,返回 0(不需要任何硬币)
      • 否则返回 Integer.MAX_VALUE / 2(表示无法组成该金额)
      • 除以 2 是为了防止后续计算中 +1 导致整数溢出
  4. 结果处理

    • 如果最终结果小于 Integer.MAX_VALUE / 2,说明存在解,返回该结果
    • 否则返回 -1 表示无法组成目标金额

关键细节解释

  1. Integer.MAX_VALUE/ 2 的作用

    • 在动态规划中,通常用无穷大表示不可达状态
    • 这里使用 Integer.MAX_VALUE / 2 而不是 Integer.MAX_VALUE 是为了避免在状态转移时发生整数溢出
    • 例如,如果 dfs(i-1, c) 或 dfs(i, c-coins[i]) 返回 Integer.MAX_VALUE,直接加 1 会导致溢出
  2. 二维数组的优势

    • 相比一维数组解法,二维数组可以更清晰地表达状态转移过程
    • 可以更方便地处理 “每种硬币无限使用” 的条件(通过 dfs(i, c-coins[i]) 实现)
  3. 记忆化的作用

    • 通过 cache 数组记录已计算的状态,避免重复计算
    • 时间复杂度从指数级降低到 O (n * amount),其中 n 是硬币种类

4.压缩dp数组()

动态规划解题套路框架 | labuladong 的算法笔记

class Solution {public int coinChange(int[] coins, int amount) {int[] dp = new int[amount + 1];// 数组大小为 amount + 1,初始值也为 amount + 1Arrays.fill(dp, amount + 1);// base casedp[0] = 0;// 外层 for 循环在遍历所有状态的所有取值for (int i = 0; i < dp.length; i++) {// 内层 for 循环在求所有选择的最小值for (int coin : coins) {// 子问题无解,跳过if (i - coin < 0) {continue;}dp[i] = Math.min(dp[i], 1 + dp[i - coin]);}}return (dp[amount] == amount + 1) ? -1 : dp[amount];}
}

http://www.dtcms.com/wzjs/269132.html

相关文章:

  • 景观设计师做交通分析常用网站新闻源软文发布平台
  • 搭建网站平台如何做分录百度一下首页官网
  • asp网站背景网络营销的十种方法
  • 专业做网站的公司有没有服务器备案查询平台官网
  • 专做商铺中介网站兰州模板网站seo价格
  • 网站免费正能量不下载企业网站开发公司
  • 贵港网站设计珠海百度seo
  • 什么网站上公司的评价最客观武汉seo认可搜点网络
  • 佛山做网站优化排名 生客seo
  • 做微信公众号用什么网站网站排名优化手机
  • 办公室设计方案windows优化大师卸载不掉
  • 渭南网站建设哪里便宜廊坊网站排名优化公司哪家好
  • 软件产品开发流程嘉兴百度seo
  • 小程序定制外包杭州百度推广优化排名
  • 中型网站开发语言搜狗seo查询
  • 昆山建设招标信息网站新闻投稿
  • 做网站哪个便宜企业软文营销
  • 网站上的3d产品展示怎么做最近实时热点事件
  • 麦客crmseo优化培训公司
  • 山东住房和城乡建设局网站首页seo优化网站推广专员招聘
  • 天河建设网站多少钱太仓seo网站优化软件
  • 恐龙网站建设最常见企业网站有哪些
  • 怎么才能把网站优化做好有哪些网络推广平台
  • 学网站建设怎么在网上做网络营销
  • 为什么自己花钱做的网站竟然不是自己的 (谷歌seo搜索引擎下载
  • 广东三库一平台登录seo关键词优化是什么意思
  • 给企业做网站用什么程序上海网站推广广告
  • 网站应用软件设计老铁外链工具
  • 网站认证必须做吗广东疫情最新通报
  • 深圳网站制作公司信息日本进口yamawa