673. Number of Longest Increasing Subsequence
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the number of longest increasing subsequences (LIS). This is a variation of the classic LIS problem, which only asks for the length. A brute-force approach, generating all subsequences, would have a time complexity of O(2^n), which is too slow for the given constraints (n <= 2000).
Solution
Number of Longest Increasing Subsequence in Python
class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
n = len(nums)
if n <= 1:
return n
# lengths[i] = length of LIS ending at index i
lengths = [1] * n
# counts[i] = number of LIS ending at index i
counts = [1] * n
for i in range(n):
for j in range(i):
# If nums[i] can extend the subsequence ending at nums[j]
if nums[j] < nums[i]:
# If we found a longer LIS
if lengths[j] + 1 > lengths[i]:
lengths[i] = lengths[j] + 1
counts[i] = counts[j] # Reset count
# If we found another LIS of the same length
elif lengths[j] + 1 == lengths[i]:
counts[i] += counts[j] # Add to count
# Find the maximum length of any LIS
max_len = max(lengths)
# Sum up the counts of all LIS with the maximum length
result = 0
for i in range(n):
if lengths[i] == max_len:
result += counts[i]
return result
Number of Longest Increasing Subsequence in Java
class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
if (n <= 1) {
return n;
}
// lengths[i] = length of LIS ending at index i
int[] lengths = new int[n];
// counts[i] = number of LIS ending at index i
int[] counts = new int[n];
Arrays.fill(lengths, 1);
Arrays.fill(counts, 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
// If nums[i] can extend the subsequence ending at nums[j]
if (nums[j] < nums[i]) {
// If we found a longer LIS
if (lengths[j] + 1 > lengths[i]) {
lengths[i] = lengths[j] + 1;
counts[i] = counts[j]; // Reset count
}
// If we found another LIS of the same length
else if (lengths[j] + 1 == lengths[i]) {
counts[i] += counts[j]; // Add to count
}
}
}
}
// Find the maximum length of any LIS
int max_len = 0;
for (int len : lengths) {
max_len = Math.max(max_len, len);
}
// Sum up the counts of all LIS with the maximum length
int result = 0;
for (int i = 0; i < n; ++i) {
if (lengths[i] == max_len) {
result += counts[i];
}
}
return result;
}
}
Number of Longest Increasing Subsequence in C++
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
if (n <= 1) {
return n;
}
// lengths[i] = length of LIS ending at index i
vector<int> lengths(n, 1);
// counts[i] = number of LIS ending at index i
vector<int> counts(n, 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
// If nums[i] can extend the subsequence ending at nums[j]
if (nums[j] < nums[i]) {
// If we found a longer LIS
if (lengths[j] + 1 > lengths[i]) {
lengths[i] = lengths[j] + 1;
counts[i] = counts[j]; // Reset count
}
// If we found another LIS of the same length
else if (lengths[j] + 1 == lengths[i]) {
counts[i] += counts[j]; // Add to count
}
}
}
}
// Find the maximum length of any LIS
int max_len = 0;
for (int len : lengths) {
max_len = max(max_len, len);
}
// Sum up the counts of all LIS with the maximum length
int result = 0;
for (int i = 0; i < n; ++i) {
if (lengths[i] == max_len) {
result += counts[i];
}
}
return result;
}
};
Number of Longest Increasing Subsequence in TypeScript
function findNumberOfLIS(nums: number[]): number {
const n = nums.length;
if (n <= 1) {
return n;
}
// lengths[i] = length of LIS ending at index i
const lengths: number[] = new Array(n).fill(1);
// counts[i] = number of LIS ending at index i
const counts: number[] = new Array(n).fill(1);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
// If nums[i] can extend the subsequence ending at nums[j]
if (nums[j] < nums[i]) {
// If we found a longer LIS
if (lengths[j] + 1 > lengths[i]) {
lengths[i] = lengths[j] + 1;
counts[i] = counts[j]; // Reset count
}
// If we found another LIS of the same length
else if (lengths[j] + 1 === lengths[i]) {
counts[i] += counts[j]; // Add to count
}
}
}
}
// Find the maximum length of any LIS
let max_len = 0;
for (const len of lengths) {
max_len = Math.max(max_len, len);
}
// Sum up the counts of all LIS with the maximum length
let result = 0;
for (let i = 0; i < n; ++i) {
if (lengths[i] === max_len) {
result += counts[i];
}
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.