2024/9/24 Hot100 dp

This commit is contained in:
linlihong 2024-09-24 17:18:45 +08:00
parent 70decfdfba
commit 153bda20f5
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package com.cool.hot100.dp;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/24/14:10
* DayNumber 1
* Hard 2
* Level ?
*/
public class Num198 {
/**
* 滚动数组
* @param nums
* @return
*/
public int rob(int[] nums) {
int noSteal=0;
int steal=nums[0];
for(int i=1;i<nums.length;i++){
int noStealTemp=noSteal;
noSteal=Math.max(noSteal,steal);
steal=noStealTemp+nums[i];
}
return Math.max(noSteal,steal);
}
/**
* 非滚动数组
* @param nums
* @return
*/
public int rob1(int[] nums) {
int[][]dp=new int[nums.length][2];
dp[0][0]=0;
dp[0][1]=nums[0];
for(int i=1;i<nums.length;i++){
dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]);
dp[i][1]=dp[i-1][0]+nums[i];
}
return Math.max(dp[nums.length-1][0],dp[nums.length-1][1]);
}
}

View File

@ -0,0 +1,20 @@
package com.cool.hot100.dp;
import org.junit.Test;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/09/24/16:58
* DayNumber 2
* Hard 2
* Level ?
*/
public class Num279 {
public int numSquares(int n) {
}
}