300. 最长上升子序列
https://leetcode-cn.com/problems/longest-increasing-subsequence/
class Solution {
public int lengthOfLIS(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int n = nums.length;
int[] dp = new int[n];
dp[0] = 1;
int res = 1;
for(int i = 1; i < n; i++){
int max = 0;
for(int j = 0; j < i; j++){
if(nums[j] < nums[i]){
max = Math.max(max, dp[j]);
}
}
dp[i] = max + 1;
res = Math.max(res, dp[i]);
}
return res;
}
}
435. 无重叠区间
https://leetcode-cn.com/problems/non-overlapping-intervals/
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if(intervals == null || intervals.length == 0 || intervals[0].length == 0) return 0;
Arrays.sort(intervals, (a, b) -> (a[0] - b[0]));
int m = intervals.length;
int[] dp = new int[m];
dp[0] = 1;
int res = 1;
for(int i = 1; i < m; i++){
int max = 0;
for(int j = 0; j < i; j++){
if(intervals[i][0] >= intervals[j][1]){
max = Math.max(max, dp[j]);
}
}
dp[i] = max + 1;
res = Math.max(res, dp[i]);
}
return m - res;
}
}
646. 最长数对链
https://leetcode-cn.com/problems/maximum-length-of-pair-chain/
class Solution {
public int findLongestChain(int[][] pairs) {
if(pairs == null || pairs.length == 0 || pairs[0].length == 0) return 0;
Arrays.sort(pairs, (a, b) -> (a[0] - b[0]));
int m = pairs.length;
int[] dp = new int[m];
dp[0] = 1;
int res = 1;
for(int i = 1; i < m; i++){
int max = 0;
for(int j = 0; j < i; j++){
if(pairs[j][1] < pairs[i][0]){
max = Math.max(max, dp[j]);
}
}
dp[i] = max + 1;
res = Math.max(res, dp[i]);
}
return res;
}
}
- 用最少数量的箭引爆气球
https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/
class Solution {
public int findMinArrowShots(int[][] points) {
if(points == null || points.length == 0 || points[0].length == 0) return 0;
Arrays.sort(points, (a, b) -> (a[0] - b[0]));
int m = points.length;
int[] dp = new int[m];
dp[0] = 1;
int res = 1;
for(int i = 1; i < m; i++){
int max = 0;
for(int j = 0; j < i; j++){
if(points[j][1] < points[i][0]){
max = Math.max(max, dp[j]);
}
}
dp[i] = max + 1;
res = Math.max(res, dp[i]);
}
return res;
}
}
- 最长递增子序列的个数
https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/
class Solution {
public int findNumberOfLIS(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int n = nums.length;
int[] dp = new int[n];
int[] count = new int[n];
Arrays.fill(dp, 1);
Arrays.fill(count, 1);
int max = 1;
for(int i = 1; i < n; i++){
for(int j = 0; j < i; j++){
if(nums[j] < nums[i]){
if(dp[j] + 1 > dp[i]){
dp[i] = dp[j] + 1; //代表第一次遇到最长子序列
count[i] = count[j];
}else if(dp[j] + 1 == dp[i]){
count[i] += count[j]; //代表已经遇到最长子序列
}
}
}
max = Math.max(max, dp[i]);
}
int ans = 0;
for(int i = 0; i < n; i++){
if(dp[i] == max){
ans += count[i];
}
}
return ans;
}
}
- 递增子序列
https://leetcode-cn.com/problems/increasing-subsequences/
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null || nums.length == 0) return list;
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int index){
if(index >= nums.length){
if(tmpList.size() >= 2){
list.add(new ArrayList<>(tmpList));
}
return;
}
if(tmpList.isEmpty() || nums[index] >= tmpList.get(tmpList.size() - 1)){
tmpList.add(nums[index]);
backtrack(list, tmpList, nums, index + 1);
tmpList.remove(tmpList.size() - 1);
}
if(index > 0 && !tmpList.isEmpty() && nums[index] == tmpList.get(tmpList.size() - 1)){
return;
}
backtrack(list, tmpList, nums, index + 1);
}
}