2024/9/26 LeetCode Hot100 dp
This commit is contained in:
parent
d68c5b4356
commit
3a82396c07
|
@ -0,0 +1,79 @@
|
|||
package com.cool.hot100.dp;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/25/23:23
|
||||
* @Description: 152. 乘积最大子数组
|
||||
* Level 4
|
||||
* DayNumber 4
|
||||
* Hard 2
|
||||
* @Link https://leetcode.cn/problems/maximum-product-subarray/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num152 {
|
||||
|
||||
public int maxProduct(int[] nums) {
|
||||
int length = nums.length;
|
||||
long[] maxF = new long[length];
|
||||
long[] minF = new long[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
maxF[i] = nums[i];
|
||||
minF[i] = nums[i];
|
||||
}
|
||||
for (int i = 1; i < length; ++i) {
|
||||
maxF[i] = Math.max(maxF[i - 1] * nums[i], Math.max(nums[i], minF[i - 1] * nums[i]));
|
||||
minF[i] = Math.min(minF[i - 1] * nums[i], Math.min(nums[i], maxF[i - 1] * nums[i]));
|
||||
if (minF[i] < (-1 << 31)) {
|
||||
minF[i] = nums[i];
|
||||
}
|
||||
}
|
||||
int ans = (int) maxF[0];
|
||||
for (int i = 1; i < length; ++i) {
|
||||
ans = Math.max(ans, (int) maxF[i]);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
public int maxProduct1(int[] nums) {
|
||||
int length=nums.length;
|
||||
int[] maxArr=new int[length];
|
||||
int[] minArr=new int[length];
|
||||
for(int i=0;i<length;i++){
|
||||
minArr[i]=nums[i];
|
||||
maxArr[i]=nums[i];
|
||||
}
|
||||
for(int i=1;i<length;i++){
|
||||
minArr[i]=Math.min(minArr[i-1]*nums[i],Math.min(nums[i],maxArr[i-1]*nums[i]));
|
||||
maxArr[i]=Math.max(maxArr[i-1]*nums[i],Math.max(nums[i],minArr[i-1]*nums[i]));
|
||||
}
|
||||
int res=maxArr[0];
|
||||
for(int i=1;i<length;i++){
|
||||
res=Math.max(res,maxArr[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
/**
|
||||
* @Author Cool
|
||||
* @Date 0:35 2024/9/26
|
||||
* 滚动数组
|
||||
**/
|
||||
public int maxProduct2(int[] nums) {
|
||||
int length=nums.length;
|
||||
int maxArr=nums[0];
|
||||
int minArr=nums[0];
|
||||
int res=nums[0];
|
||||
for(int i=1;i<length;i++){
|
||||
int maxTemp=maxArr,minTemp=minArr;
|
||||
minArr=Math.min(minTemp*nums[i],Math.min(nums[i],maxTemp*nums[i]));
|
||||
maxArr=Math.max(maxTemp*nums[i],Math.max(nums[i],minTemp*nums[i]));
|
||||
res=Math.max(res,maxArr);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@Test
|
||||
public void test(){
|
||||
maxProduct2(new int[]{2,3,-2,4});
|
||||
}
|
||||
}
|
|
@ -12,4 +12,54 @@ package com.cool.hot100.dp;
|
|||
* @Link https://leetcode.cn/problems/longest-increasing-subsequence/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num300 {
|
||||
public int lengthOfLIS(int[] nums) {
|
||||
if(nums.length==0){
|
||||
return 0;
|
||||
}
|
||||
int[]dp=new int[nums.length];
|
||||
dp[0]=1;
|
||||
int res=1;
|
||||
for(int i=1;i<nums.length;i++){
|
||||
dp[i]=1;
|
||||
for(int j=0;j<i;j++){
|
||||
if(nums[j]<nums[i]){
|
||||
dp[i]=Math.max(dp[i],dp[j]+1);
|
||||
}
|
||||
}
|
||||
res=Math.max(res,dp[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
/**
|
||||
* @Author Cool
|
||||
* @Date 23:15 2024/9/25
|
||||
* 贪心+二分解法(看不懂)
|
||||
**/
|
||||
public int lengthOfLIS1(int[] nums) {
|
||||
int len = 1, n = nums.length;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
int[] d = new int[n + 1];
|
||||
d[len] = nums[0];
|
||||
for (int i = 1; i < n; ++i) {
|
||||
if (nums[i] > d[len]) {
|
||||
d[++len] = nums[i];
|
||||
} else {
|
||||
int l = 1, r = len, pos = 0; // 如果找不到说明所有的数都比 nums[i] 大,此时要更新 d[1],所以这里将 pos 设为 0
|
||||
while (l <= r) {
|
||||
int mid = (l + r) >> 1;
|
||||
if (d[mid] < nums[i]) {
|
||||
pos = mid;
|
||||
l = mid + 1;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
d[pos + 1] = nums[i];
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue