The problem is: count all increasing subsequence of s.
def main():
print(count([3,2,4,5,4]))
def count(s: list) -> int:
n = len(s)
counter = 0
result = {}
if n == 0:
return 0
for i in range(n):
counter += recurse(s[i+1:], s[i], result)
return counter
def recurse(s: list, x: int, result: dict) -> int:
n = len(s)
counter = 0
if n in result:
return result[n]
if n == 0:
return 1
for i in range(n):
if s[i] > x:
counter += recurse(s[i+1:], s[i], result)
result[n] = counter + 1
return counter + 1
Can anyone help me what is the time complexity of this algorithm?
What I know is T(count()) is O(n) * T(recurse(n)). Correct me if I'm wrong, T(recurse(s1, ...)) will be O(1) if I already calculated recurse(s1, ...) and that will make T(count(s)) = O(n).