673. Number of Longest Increasing Subsequence

Posted gopanama

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了673. Number of Longest Increasing Subsequence相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/107293/JavaC++-Simple-dp-solution-with-explanation

每次记载最大长度 然后最后遍历 把等于最大长度的index对应的record里记载的个数加起来

 

 1 class Solution {
 2     public int findNumberOfLIS(int[] nums) {
 3         if(nums.length == 0) return 0;
 4         if(nums.length == 1) return 1;
 5         int[] dp = new int[nums.length];
 6         int[] record = new int[nums.length];
 7         dp[0] = 1;
 8         record[0]++;
 9         int max = 1;
10         int numLen = 0;
11         for(int i = 1; i < nums.length; i++){
12             dp[i] = 1;
13             record[i] = 1;
14             for(int j = i-1; j >= 0; j--){
15                 if(nums[j] < nums[i]){
16                     if(dp[i] == dp[j]+1){
17                         record[i] += record[j];
18                     }else if(dp[j]+1 > dp[i]){
19                         record[i] = record[j];
20                         dp[i] = dp[j] + 1;
21                     }
22                 }      
23             }
24             max = Math.max(max, dp[i]);
25         }
26         int res = 0;
27         System.out.println(max);
28         for(int i = dp.length-1; i >= 0; i--){
29             if(dp[i] == max){
30                 res += record[i];
31             }
32         }
33         return res;
34         
35     }
36 }

 

以上是关于673. Number of Longest Increasing Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

673. Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

Leetcode-673 (Number of Longest Increasing Subsequence)最长递增子序列的个数

1. BinaryGap Find longest sequence of zeros in binary representation of an integer.