代码随想三刷动态规划篇5

代码随想三刷动态规划篇5

  • [377. 组合总和 Ⅳ](#377. 组合总和 Ⅳ)
  • [57. 爬楼梯(第八期模拟笔试)](#57. 爬楼梯(第八期模拟笔试))
  • [322. 零钱兑换](#322. 零钱兑换)
  • [279. 完全平方数](#279. 完全平方数)

377. 组合总和 Ⅳ

题目

链接

代码

java 复制代码
class Solution {
    public int combinationSum4(int[] nums, int target) {
        int[] dp = new int[target+1];
        dp[0] = 1;
        for (int i = 0; i <= target; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i >= nums[j]) {
                    dp[i] += dp[i-nums[j]];
                }
            }
        }
        return dp[dp.length-1];
    }
}

57. 爬楼梯(第八期模拟笔试)

题目

链接

代码

java 复制代码
import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//层数
        int m = sc.nextInt();//一次几楼
        int[] dp = new int[n+1];
        dp[0] = 1;
        for(int j=0;j<dp.length;j++){
            for(int i =1;i<=m;i++){
                if(j>=i){
                    dp[j] += dp[j-i];
                }
            }
        }
        System.out.println(dp[n]);
    }
}

322. 零钱兑换

题目

链接

代码

java 复制代码
class Solution {
    public int coinChange(int[] coins, int amount) {
        //dp[i] 表示 揍到i的最小硬币数
        int[] dp = new int[amount+1];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        for(int i =0;i<coins.length;i++){
            for(int j = coins[i];j<=amount;j++){
                dp[j] = (int) Math.min(Long.valueOf(dp[j]),dp[j-coins[i]]+1L);
            }
        }
        return dp[amount]==Integer.MAX_VALUE?-1:dp[amount];
    }
}

279. 完全平方数

题目

链接

代码

java 复制代码
class Solution {
    public int numSquares(int n) {
        //1,4,9.。。凑到n  
        //可以重复使用,完全背包。
        //组合数,和排序无关。先物品,再背包
        int[] dp = new int[n+1];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        for(int i =1;i*i<=n;i++){
            for(int j = i*i;j<=n;j++){
                dp[j] = Math.min(dp[j],dp[j-i*i]+1);
            }
        }
        return dp[n];
    }
}
相关推荐
艾莉丝努力练剑32 分钟前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
C++、Java和Python的菜鸟2 小时前
第六章 统计初步
算法·机器学习·概率论
Cx330❀2 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
散1122 小时前
01数据结构-Prim算法
数据结构·算法·图论
起个昵称吧2 小时前
线程相关编程、线程间通信、互斥锁
linux·算法
myzzb3 小时前
基于uiautomation的自动化流程RPA开源开发演示
运维·python·学习·算法·自动化·rpa
旺小仔.4 小时前
双指针和codetop复习
数据结构·c++·算法
jingfeng5144 小时前
C++ STL-string类底层实现
前端·c++·算法
雲墨款哥5 小时前
JS算法练习-Day10-判断单调数列
前端·javascript·算法
FPGA5 小时前
CRC校验原理及其FPGA实现
算法