From d68c5b4356de41dd2da0d3b2e4f96679631630ec Mon Sep 17 00:00:00 2001 From: linlihong <747682928@qq.com> Date: Wed, 25 Sep 2024 17:09:58 +0800 Subject: [PATCH] 2024/9/25 Hot100 dp --- src/main/java/com/cool/hot100/dp/Num139.java | 44 ++++++++++++++++++++ src/main/java/com/cool/hot100/dp/Num300.java | 15 +++++++ src/main/java/com/cool/hot100/dp/Num322.java | 28 +++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/main/java/com/cool/hot100/dp/Num139.java create mode 100644 src/main/java/com/cool/hot100/dp/Num300.java create mode 100644 src/main/java/com/cool/hot100/dp/Num322.java diff --git a/src/main/java/com/cool/hot100/dp/Num139.java b/src/main/java/com/cool/hot100/dp/Num139.java new file mode 100644 index 0000000..97cdeea --- /dev/null +++ b/src/main/java/com/cool/hot100/dp/Num139.java @@ -0,0 +1,44 @@ +package com.cool.hot100.dp; + +import org.junit.Test; + +import java.util.*; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/09/24/16:11 + * DayNumber 2 + * Hard 2 + * Level ? + * @Description 139. 单词拆分 + * @Link https://leetcode.cn/problems/word-break/description/?envType=study-plan-v2&envId=top-100-liked + */ +public class Num139 { + public boolean wordBreak(String s, List wordDict) { + Set wordDictSet = new HashSet(wordDict); + boolean[] dp = new boolean[s.length() + 1]; + Arrays.fill(dp, false); + dp[0] = true; + // dp[i]表示从0-i的字符串,是否可以用wordDict来表示 + for(int i=1;i list=new ArrayList<>(); + list.add("leet"); + list.add("code"); + + wordBreak("leetcode",list); + } +} diff --git a/src/main/java/com/cool/hot100/dp/Num300.java b/src/main/java/com/cool/hot100/dp/Num300.java new file mode 100644 index 0000000..ac3a656 --- /dev/null +++ b/src/main/java/com/cool/hot100/dp/Num300.java @@ -0,0 +1,15 @@ +package com.cool.hot100.dp; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/09/24/16:45 + * DayNumber 3 + * Hard 2 + * Level ? + * @Description 300. 最长递增子序列 + * @Link https://leetcode.cn/problems/longest-increasing-subsequence/description/?envType=study-plan-v2&envId=top-100-liked + */ +public class Num300 { +} diff --git a/src/main/java/com/cool/hot100/dp/Num322.java b/src/main/java/com/cool/hot100/dp/Num322.java new file mode 100644 index 0000000..71cdf30 --- /dev/null +++ b/src/main/java/com/cool/hot100/dp/Num322.java @@ -0,0 +1,28 @@ +package com.cool.hot100.dp; + +import java.util.Arrays; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/09/24/15:31 + * DayNumber 1 + * Hard 2 + * Level ? + * @Description 322. 零钱兑换 + * @Link https://leetcode.cn/problems/coin-change/description/?envType=study-plan-v2&envId=top-100-liked + */ +public class Num322 { + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, amount + 1); + dp[0] = 0; + for (int i = 0; i < coins.length; i++) { + for (int j = coins[i]; j <= amount; j++) { + dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1); + } + } + return dp[amount] == amount + 1 ? -1 : dp[amount]; + } +}