_57爬楼梯_第八期模拟笔试
package 代码随想录.动态规划;
import java.util.Scanner;
public class _57爬楼梯_第八期模拟笔试 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
climbALadder(n,m);
}
private static void climbALadder(int n, int m) {
int dp [] = new int[n+1];
dp[0] = 1;
for (int j = 1;j<=n;j++){
for (int i=1;i<=m;i++){
if (j-i >= 0){
dp[j] += dp[j-i];
}
}
}
System.out.println(dp[n]);
}
}
_322零钱兑换_无基数_记忆化搜索
package 代码随想录.动态规划;
public class _322零钱兑换_无基数_记忆化搜索 {
public int coinChange(int[] coins, int amount) {
if (amount < 1){
return 0;
}
return coinsFind(coins,amount,new int[amount]);
}
private int coinsFind(int[] coins, int remb, int[] count) {
if (remb < 0){
return -1;
}
if (remb == 0){
return 0;
}
if (count[remb - 1] != 0){
return count[remb - 1];
}
int min = Integer.MAX_VALUE;
for (int coin :coins){
int res = coinsFind(coins,remb - coin,count);
if (res >= 0 && res < min){
min = res+1;
}
}
count[remb - 1] = (min == Integer.MAX_VALUE) ? -1 :min;
return count[remb - 1];
}
}
_322零钱兑换_无基数_dp
package 代码随想录.动态规划;
public class _322零钱兑换_无基数_dp {
public int coinChange(int[] coins, int amount) {
int max = Integer.MAX_VALUE;
int [] dp = new int[amount + 1];
for (int j = 0;j <dp.length;j++) {
dp[j] = max;
}
dp[0] = 0;
for (int i = 0; i < coins.length;i++) {
for (int j = coins[i];j<=amount;j++){
if (dp[j - coins[i]] != max){
dp[j] = Math.min(dp[j],dp[j-coins[i]] + 1);
}
}
}
return dp[amount] == max ? -1 :dp[amount];
}
}
_377组合总和Ⅳ
package 代码随想录.动态规划;
public class _377组合总和Ⅳ {
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[target];
}
}