From 2db8bfda73886fa98ec1e3875f58ec3767b6672f Mon Sep 17 00:00:00 2001 From: Krish Modi Date: Thu, 30 Oct 2025 15:38:14 +0530 Subject: [PATCH 1/2] Create number_of_longest_increasing_subsequence.py Implement function to count number of longest increasing subsequence --- ...umber_of_longest_increasing_subsequence.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 dynamic_programming/number_of_longest_increasing_subsequence.py diff --git a/dynamic_programming/number_of_longest_increasing_subsequence.py b/dynamic_programming/number_of_longest_increasing_subsequence.py new file mode 100644 index 000000000000..6ddd1827da39 --- /dev/null +++ b/dynamic_programming/number_of_longest_increasing_subsequence.py @@ -0,0 +1,52 @@ +''' +Leetcode Problem:673. Number of Longest Increasing Subsequence +Link: https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ + +Given an integer array nums, return the number of longest increasing subsequences. +Notice that the sequence has to be strictly increasing. + +Example 1: + +Input: nums = [1,3,5,4,7] +Output: 2 +Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7]. +Example 2: + +Input: nums = [2,2,2,2,2] +Output: 5 +Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5. + + +Constraints: + +1 <= nums.length <= 2000 +-10**6 <= nums[i] <= 10**6 +The answer is guaranteed to fit inside a 32-bit integer. +''' + + +def findNumberOfLIS(nums): + n = len(nums) + if n == 0: + return 0 + + length = [1] * n + count = [1] * n + + for i in range(n): + for j in range(i): + if nums[j] < nums[i]: + if length[j] + 1 > length[i]: + length[i] = length[j] + 1 + count[i] = count[j] + elif length[j] + 1 == length[i]: + count[i] += count[j] + + max_len = max(length) + return sum(c for l, c in zip(length, count) if l == max_len) + + +# For testing... +n=int(input()) +nums=list(map(int,input().split())) +print(findNumberOfLIS(nums)) From 7cbae762c9330d1697455eeb7de4172456e4063e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Oct 2025 10:11:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../number_of_longest_increasing_subsequence.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/number_of_longest_increasing_subsequence.py b/dynamic_programming/number_of_longest_increasing_subsequence.py index 6ddd1827da39..c9f018dbc3f1 100644 --- a/dynamic_programming/number_of_longest_increasing_subsequence.py +++ b/dynamic_programming/number_of_longest_increasing_subsequence.py @@ -1,4 +1,4 @@ -''' +""" Leetcode Problem:673. Number of Longest Increasing Subsequence Link: https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ @@ -15,14 +15,14 @@ Input: nums = [2,2,2,2,2] Output: 5 Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5. - + Constraints: 1 <= nums.length <= 2000 -10**6 <= nums[i] <= 10**6 The answer is guaranteed to fit inside a 32-bit integer. -''' +""" def findNumberOfLIS(nums): @@ -47,6 +47,6 @@ def findNumberOfLIS(nums): # For testing... -n=int(input()) -nums=list(map(int,input().split())) +n = int(input()) +nums = list(map(int, input().split())) print(findNumberOfLIS(nums))