2024/9/25 Hot100 dp

This commit is contained in:
linlihong 2024-09-25 17:09:58 +08:00
parent 89c197b9eb
commit d68c5b4356
3 changed files with 87 additions and 0 deletions

View File

@ -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<String> wordDict) {
Set<String> 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<s.length()+1;i++){
for(int j=0;j<i;j++){
if(dp[j]&&wordDictSet.contains(s.substring(j,i))){
dp[i]=true;
break;
}
}
}
return dp[s.length()];
}
@Test
public void test(){
List<String> list=new ArrayList<>();
list.add("leet");
list.add("code");
wordBreak("leetcode",list);
}
}

View File

@ -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 {
}

View File

@ -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];
}
}