Number of Longest Increasing Subsequence

Given an integer array nums, return the number of longest strictly increasing subsequences.

30:00

Number of Longest Increasing Subsequence
medium
Free written solution
Topics
Companies

Given an integer array nums, return the number of longest strictly increasing subsequences.

Example 1:
Input: {"nums":[1,3,5,4,7]}
Output: 2
Constraints:
  • 1≤nums.length≤20001 \leq \text{nums.length} \leq 2000

  • −106≤nums[i]≤106-10^6 \leq \text{nums}[i] \leq 10^6

  • The answer is guaranteed to fit inside a 32-bit integer.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,3,5,4,7]}

Every element alone is an increasing subsequence of length 1, and there is exactly 1 way to make it. So length[i] = 1 and count[i] = 1 to start.

.
.
.
.
.

nums

1

3

5

4

7

len

1
1
1
1
1

cnt

1
1
1
1
1

len = longest increasing run ending here

cnt = how many such runs

Variables
VariableValue
n5
DepthFunction Call
Stack empty
0/17