码迷,mamicode.com
首页 > 其他好文 > 详细

673. Number of Longest Increasing Subsequence

时间:2018-10-30 11:20:50      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:enc   return   .com   pre   sub   system   else   int   print   

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

标签:enc   return   .com   pre   sub   system   else   int   print   

原文地址:https://www.cnblogs.com/goPanama/p/9875236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!