2024/9/27 Hot100 多维dp和每日一题
This commit is contained in:
parent
615730e9b9
commit
ce5ee6905b
|
@ -0,0 +1,62 @@
|
|||
package com.cool.hot100.multi_dp;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/27/14:28
|
||||
* DayNumber 2
|
||||
* Hard 2
|
||||
* Level ?
|
||||
* @Description 64. 最小路径和
|
||||
* @Link https://leetcode.cn/problems/minimum-path-sum/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num64 {
|
||||
|
||||
public int minPathSum(int[][] grid) {
|
||||
if(grid.length==0){
|
||||
return 0;
|
||||
}
|
||||
int[][]dp=new int[grid.length][grid[0].length];
|
||||
dp[0][0]=grid[0][0];
|
||||
for(int i=1;i<grid.length;i++){
|
||||
dp[i][0]=dp[i-1][0]+grid[i][0];
|
||||
}
|
||||
for(int i=1;i<grid[0].length;i++){
|
||||
dp[0][i]=dp[0][i-1]+grid[0][i];
|
||||
}
|
||||
for(int i=1;i<grid.length;i++){
|
||||
for(int j=1;j<grid[0].length;j++){
|
||||
dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];
|
||||
}
|
||||
}
|
||||
return dp[grid.length-1][grid[0].length-1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 空间优化
|
||||
* @param grid
|
||||
* @return
|
||||
*/
|
||||
public int minPathSum1(int[][] grid) {
|
||||
if (grid.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rows = grid.length;
|
||||
int cols = grid[0].length;
|
||||
int[] dp = new int[cols];
|
||||
|
||||
dp[0] = grid[0][0];
|
||||
for (int j = 1; j < cols; j++) {
|
||||
dp[j] = dp[j - 1] + grid[0][j];
|
||||
}
|
||||
for (int i = 1; i < rows; i++) {
|
||||
dp[0] += grid[i][0];
|
||||
for (int j = 1; j < cols; j++) {
|
||||
dp[j] = Math.min(dp[j], dp[j - 1]) + grid[i][j];
|
||||
}
|
||||
}
|
||||
return dp[cols - 1];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.cool.one_question_per_day;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/27/09:02
|
||||
* DayNumber 1
|
||||
* Hard 2
|
||||
* Level ?
|
||||
* Score 1947
|
||||
* @Description 2516. 每种字符至少取 K 个
|
||||
* @Link https://leetcode.cn/problems/take-k-of-each-character-from-left-and-right/?envType=daily-question&envId=2024-09-27
|
||||
*/
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class LeetCode20240927 {
|
||||
public int takeCharacters(String s, int k) {
|
||||
char[] chars=s.toCharArray();
|
||||
int[] arr = new int[3];
|
||||
for(char c:chars){
|
||||
//取走所有字符
|
||||
arr[c-'a']++;
|
||||
}
|
||||
for(int num:arr){
|
||||
if(num<k){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int res = 0;
|
||||
for(int right=0;right<chars.length;right++){
|
||||
int index=chars[right]-'a';
|
||||
arr[index]--;
|
||||
while(arr[index]<k){
|
||||
arr[chars[left++]-'a']++;
|
||||
}
|
||||
res=Math.max(res,right-left+1);
|
||||
}
|
||||
return chars.length-res;
|
||||
}
|
||||
@Test
|
||||
public void test(){
|
||||
takeCharacters("aabaaaacaabc",2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue