Skip to content

Commit a017894

Browse files
authored
Create Number of Longest Increasing Subsequence.java
1 parent 29b2d9f commit a017894

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/*
3+
673. Number of Longest Increasing Subsequence
4+
Medium
5+
6+
Given an integer array nums, return the number of longest increasing subsequences.
7+
Notice that the sequence has to be strictly increasing.
8+
9+
Example 1:
10+
Input: nums = [1,3,5,4,7]
11+
Output: 2
12+
Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
13+
14+
Example 2:
15+
Input: nums = [2,2,2,2,2]
16+
Output: 5
17+
Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
18+
19+
Constraints:
20+
1 <= nums.length <= 2000
21+
-106 <= nums[i] <= 106
22+
*/
23+
24+
class Solution {
25+
public int findNumberOfLIS(int[] nums) {
26+
// LIS Pure::
27+
//edge case ::
28+
if(nums.length==1){
29+
return 1;
30+
}
31+
return solveLIS(nums);
32+
33+
}
34+
public static int solveLIS(int[] arr){
35+
int n=arr.length;
36+
HashMap<Integer,Integer> map =new HashMap<>();
37+
int len[] = new int[n];
38+
int cnt[] = new int[n];
39+
Arrays.fill(len,1);
40+
Arrays.fill(cnt,1);
41+
42+
int maxi =0;
43+
for(int i=1;i<n;i++){
44+
for(int j=0;j<i;j++){
45+
if(arr[i]>arr[j]){
46+
if(1+len[j]>len[i]){
47+
len[i] =1+len[j];
48+
cnt[i]=cnt[j];
49+
}else if(1+len[j]==len[i]){
50+
cnt[i]+=cnt[j];
51+
}
52+
}
53+
}
54+
maxi =Math.max(maxi,len[i]);
55+
}
56+
int num=0;
57+
for(int i=0;i<n;i++){
58+
if(maxi==len[i]){
59+
num +=cnt[i];
60+
}
61+
}
62+
return num;
63+
}
64+
}

0 commit comments

Comments
 (0)