最长上升子序列

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;
    }
}
  1. 用最少数量的箭引爆气球
    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;
    }
}
  1. 最长递增子序列的个数
    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;
        
    }
}
  1. 递增子序列
    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);
  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值