Skip to content

Commit

Permalink
Number of Longest Increasing Subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sukhoverkhov committed Aug 23, 2024
1 parent a2cef70 commit df4dc1d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions python/leetcode/Number of Longest Increasing Subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Number of Longest Increasing Subsequence
"""
from typing import List


class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
lis = [1] * len(nums)
ct = [0] * len(nums)

res = 0
max_len = 1
for i in range(len(nums) - 1, -1, -1):

tmp = 0
tmp_ct = 1
for j in range(i + 1, len(nums)):
if nums[i] < nums[j]:
if lis[j] > tmp:
tmp = lis[j]
tmp_ct = ct[j]
elif lis[j] == tmp:
tmp_ct += ct[j]

lis[i] += tmp
ct[i] = tmp_ct

if max_len < lis[i]:
max_len = lis[i]
res = ct[i]
elif max_len == lis[i]:
res += ct[i]

return res


if __name__ == "__main__":
obj = Solution()

nums = [1, 3, 5, 4, 7]
assert obj.findNumberOfLIS(nums) == 2

nums = [1, 2, 4, 3, 5, 4, 7, 2]
assert obj.findNumberOfLIS(nums) == 3

0 comments on commit df4dc1d

Please sign in to comment.