首页 > 其他分享 >Leetcode(剑指offer专项训练)——DP专项(5)

Leetcode(剑指offer专项训练)——DP专项(5)

时间:2023-04-02 16:44:52浏览次数:78  
标签:专项 return 硬币 int coins amount dp Leetcode DP

最少的硬币数目

给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
你可以认为每种硬币的数量是无限的。
链接

完全背包问题

思路:主要是要自己推出动态转移方程

\[F(i)=min_{j=0...n-1}F(i-c_j)+1 \]

其中\(c_j\)代表的是第\(j\)枚硬币的面额,\(i\)代表总的金额

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        if(amount==0){
            return 0;
        }
        //完全背包问题
        int n=coins.size();
        int inf=9999;
        vector<int>dp(amount+1,inf);
        dp[0]=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<=amount;j++){
                if(j>=coins[i]){
                    dp[j]=min(dp[j],dp[j-coins[i]]+1);
                }else{
                    continue;
                }
            }
        }
       if(dp[amount]==inf){
            return -1;
       }else{
            return dp[amount];
       }
    }
};

题解

记忆化搜索

class Solution {
public:
    vector<int>count;
    int inf=INT_MAX;
    int dfs(vector<int>&coins,int rem){
        if(rem<0){
            return -1;
        }else if(rem==0){
            return 0;
        }else if(count[rem-1]!=0){
            //记忆化dfs
            return count[rem-1];
        }else{
            int ans=inf;
            for(auto coin:coins){
                int num=dfs(coins,rem-coin);
                if(num>=0&&num<ans){
                    ans=num+1;
                }
            }
            count[rem-1]=ans==inf?-1:ans;
            return count[rem-1];
        }
    }
    int coinChange(vector<int>& coins, int amount) {
        if(amount==0){
            return 0;
        }
        count.resize(amount);
        return dfs(coins,amount);
    }
};

标签:专项,return,硬币,int,coins,amount,dp,Leetcode,DP
From: https://www.cnblogs.com/SaltyCheese/p/17280735.html

相关文章

  • 【DP】LeetCode 64. 最小路径和
    题目链接64.最小路径和思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律表示状态假设到了右下角,考虑一下我们要存储的信息走到最后坐标的最小步数当前坐标的信息,用来判断是否走到了右下角很容易联想到使用......
  • 【DP】LeetCode 70. 爬楼梯
    题目链接70.爬楼梯思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律表示状态假设走到了最后一层台阶,考虑一下我们要存储的信息:走到这最后一层台阶的方法数当前台阶数,用于判断是否走到了最后一层台阶这时候......
  • [LeetCode] 1338. Reduce Array Size to The Half 数组大小减半
    Youaregivenanintegerarray arr.Youcanchooseasetofintegersandremovealltheoccurrencesoftheseintegersinthearray.Return theminimumsizeofthesetsothat atleast halfoftheintegersofthearrayareremoved.Example1:Input:arr=......
  • Leetcode(剑指offer专项训练)——DP专项(4)
    加减的目标值给定一个正整数数组nums和一个整数target。向数组中的每个整数前添加 '+'或'-',然后串联起所有整数,可以构造一个表达式:例如,nums=[2,1],可以在2之前添加'+',在1之前添加'-',然后串联起来得到表达式"+2-1"。返回可以通过上述方法构造的、运算......
  • 代码随想录Day17-Leetcode110.平衡二叉树,257. 二叉树的所有路径,404.左叶子之和
    110.平衡二叉树题目链接:https://leetcode.cn/problems/balanced-binary-tree/一个显然但似乎不太高效的方法是:通过递归获取左右子树高度,判断差;然后递归判断左右结点;那么一个显然的改进就是后序遍历/***Definitionforabinarytreenode.*functionTreeNode(val......
  • 关于网络通信中TCP/UDP的端口范围-以及在Linux系统中的使用权限说明
    关于TCP/UDP的端口号的范围都是0~65535 根据IANA定义,可以参考如下链接:https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtmlIANA将这些端口分成了3类,LastUpdated2023-03-30Portnumbersareassignedinvariousways,based......
  • 调试freeradius时遇到的 线程池以及udp相关问题
    调试线程池过程中遇到了一个return和pthread_exit的问题;google一下发现右如下概念首先,return语句和pthread_exit()函数的含义不同,return的含义是返回,它不仅可以用于线程执行的函数,普通函数也可以使用;pthread_exit()函数的含义是线程退出,它专门用于结束某个线程的执行。在主......
  • leetcode876. 链表的中间结点
      876. 链表的中间结点方法一:最简单的做法,先求出整个链表的长度,再求1/2处节点的位置。/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListNode(intx)......
  • leetcode-733-easy
    FloodFillAnimageisrepresentedbyanmxnintegergridimagewhereimage[i][j]representsthepixelvalueoftheimage.Youarealsogiventhreeintegerssr,sc,andcolor.Youshouldperformafloodfillontheimagestartingfromthepixelimage[sr......
  • leetcode-724-easy
    FindPivotIndexGivenanarrayofintegersnums,calculatethepivotindexofthisarray.Thepivotindexistheindexwherethesumofallthenumbersstrictlytotheleftoftheindexisequaltothesumofallthenumbersstrictlytotheindex's......