4.17 最长子序列
This commit is contained in:
parent
c44787299a
commit
f29842be95
17
src/main/java/dynamic_programming/FindLength.java
Normal file
17
src/main/java/dynamic_programming/FindLength.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package dynamic_programming;
|
||||||
|
/**
|
||||||
|
* 题目: 718. 最长重复子数组 (findLength)
|
||||||
|
* 描述:给两个整数数组 nums1 和 nums2 ,返回 两个数组中 公共的 、长度最长的子数组的长度 。
|
||||||
|
|
||||||
|
* 示例 1:
|
||||||
|
输入:nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
|
||||||
|
输出:3
|
||||||
|
解释:长度最长的公共子数组是 [3,2,1] 。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/maximum-length-of-repeated-subarray/
|
||||||
|
*/
|
||||||
|
public class FindLength {
|
||||||
|
public int findLength(int[] nums1, int[] nums2) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
31
src/main/java/dynamic_programming/FindLengthOfLCIS.java
Normal file
31
src/main/java/dynamic_programming/FindLengthOfLCIS.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package dynamic_programming;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 题目: 674. 最长连续递增序列 (MaxProduct)
|
||||||
|
* 描述:给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
|
||||||
|
* 连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。
|
||||||
|
|
||||||
|
* 示例 1:
|
||||||
|
输入:nums = [1,3,5,4,7]
|
||||||
|
输出:3
|
||||||
|
解释:最长连续递增序列是 [1,3,5], 长度为3。
|
||||||
|
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
|
||||||
|
*/
|
||||||
|
public class FindLengthOfLCIS {
|
||||||
|
public int findLengthOfLCIS(int[] nums) {
|
||||||
|
int sz=nums.length;
|
||||||
|
int[]dp=new int[sz];
|
||||||
|
int ans=1;
|
||||||
|
Arrays.fill(dp,1);
|
||||||
|
for (int i = 1; i < sz; i++) {
|
||||||
|
if(nums[i]>nums[i-1])
|
||||||
|
dp[i]=dp[i-1]+1;
|
||||||
|
ans=Math.max(ans,dp[i]);
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
}
|
33
src/main/java/dynamic_programming/LengthOfLIS.java
Normal file
33
src/main/java/dynamic_programming/LengthOfLIS.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package dynamic_programming;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.PriorityQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 题目: 300. 最长递增子序列 (lengthOfLIS)
|
||||||
|
* 描述:给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
|
||||||
|
* 子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
|
||||||
|
|
||||||
|
* 示例 1:
|
||||||
|
输入:nums = [10,9,2,5,3,7,101,18]
|
||||||
|
输出:4
|
||||||
|
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/longest-increasing-subsequence/
|
||||||
|
*/
|
||||||
|
//没做出来
|
||||||
|
public class LengthOfLIS {
|
||||||
|
public int lengthOfLIS(int[] nums) {
|
||||||
|
if(nums.length == 0) return 0;
|
||||||
|
int[] dp = new int[nums.length];
|
||||||
|
int res = 0;
|
||||||
|
Arrays.fill(dp, 1);
|
||||||
|
for(int i = 0; i < nums.length; i++) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
53
src/main/java/dynamic_programming/MaxProduct.java
Normal file
53
src/main/java/dynamic_programming/MaxProduct.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package dynamic_programming;
|
||||||
|
/**
|
||||||
|
* 题目: 152. 乘积最大子数组 (MaxProduct)
|
||||||
|
* 描述:给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续 子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
|
||||||
|
* 测试用例的答案是一个 32-位 整数。
|
||||||
|
|
||||||
|
* 示例 1:
|
||||||
|
输入: nums = [2,3,-2,4]
|
||||||
|
输出: 6
|
||||||
|
解释: 子数组 [2,3] 有最大乘积 6。
|
||||||
|
|
||||||
|
* 链接:https://leetcode.cn/problems/maximum-product-subarray/
|
||||||
|
*/
|
||||||
|
//不会
|
||||||
|
public class MaxProduct {
|
||||||
|
public int maxProduct(int[] nums) {
|
||||||
|
if (nums == null || nums.length == 0) {
|
||||||
|
throw new IllegalArgumentException("数组不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化:第 0 个位置的最大/最小乘积都等于 nums[0]
|
||||||
|
int prevMax = nums[0];
|
||||||
|
int prevMin = nums[0];
|
||||||
|
int ans = nums[0];
|
||||||
|
|
||||||
|
// 从 i=1 开始遍历
|
||||||
|
for (int i = 1; i < nums.length; i++) {
|
||||||
|
int x = nums[i];
|
||||||
|
// 三个候选值:x、x*prevMax、x*prevMin
|
||||||
|
int candidateMax = Math.max(x, Math.max(x * prevMax, x * prevMin));
|
||||||
|
int candidateMin = Math.min(x, Math.min(x * prevMax, x * prevMin));
|
||||||
|
|
||||||
|
// 更新当前的最大/最小乘积
|
||||||
|
prevMax = candidateMax;
|
||||||
|
prevMin = candidateMin;
|
||||||
|
|
||||||
|
// 更新全局答案
|
||||||
|
ans = Math.max(ans, prevMax);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 简单测试
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MaxProduct sol = new MaxProduct();
|
||||||
|
System.out.println(sol.maxProduct(new int[]{2, 3, -2, 4})); // 输出 6
|
||||||
|
System.out.println(sol.maxProduct(new int[]{-2, 0, -1})); // 输出 0
|
||||||
|
System.out.println(sol.maxProduct(new int[]{-2, 3, -4})); // 输出 24
|
||||||
|
System.out.println(sol.maxProduct(new int[]{-1, -3, -10, 0, 60})); // 输出 60
|
||||||
|
System.out.println(sol.maxProduct(new int[]{-2, -5, -2, -4, 3})); // 输出 240
|
||||||
|
}
|
||||||
|
}
|
@ -43,6 +43,7 @@ public class Rob3 {
|
|||||||
return 0;
|
return 0;
|
||||||
if (memo.containsKey(root))
|
if (memo.containsKey(root))
|
||||||
return memo.get(root);
|
return memo.get(root);
|
||||||
|
//偷父节点
|
||||||
int money = root.val;
|
int money = root.val;
|
||||||
if (root.left != null) {
|
if (root.left != null) {
|
||||||
money += robAction(root.left.left, memo) + robAction(root.left.right, memo);
|
money += robAction(root.left.left, memo) + robAction(root.left.right, memo);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user