-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0046.py
67 lines (59 loc) · 2.08 KB
/
LC0046.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# class Solution:
# def __init__(self):
# self.max_len = -inf
# self.result = 0
# def dfs(self, index, last_index, nums, dp):
# if index == 0:
# if nums[index] < nums[last_index]:
# dp[(index, last_index)] = 1
# return 1
# else:
# dp[(index, last_index)] = 0
# return 0
# if dp[(index, last_index)] != -1:
# return dp[(index, last_index)]
# l1 = 0 + self.dfs(index - 1, last_index, nums, dp)
# l2 = -inf
# if nums[index] < nums[last_index]:
# l2 = 1 + self.dfs(index - 1, index, nums, dp)
# dp[(index, last_index)] = max(l1, l2)
# if dp[(index, last_index)] > self.max_len:
# self.max_len = dp[(index, last_index)]
# self.result = 1
# elif dp[(index, last_index)] == self.max_len:
# self.result += 1
# return dp[(index, last_index)]
# def findNumberOfLIS(self, nums: List[int]) -> int:
# dp = {}
# for i in range(len(nums)):
# for j in range(-1, len(nums)):
# dp[(i,j)] = -1
# _ = self.dfs(len(nums) - 1, -1, nums, dp)
# return self.result
class Solution:
def findNumberOfLIS(self, nums):
n = len(nums)
length = [0] * n
count = [0] * n
def calculate_dp(i):
if length[i] != 0:
return
length[i] = 1
count[i] = 1
for j in range(i):
if nums[j] < nums[i]:
calculate_dp(j)
if length[j] + 1 > length[i]:
length[i] = length[j] + 1
count[i] = 0
if length[j] + 1 == length[i]:
count[i] += count[j]
max_length = 0
result = 0
for i in range(n):
calculate_dp(i)
max_length = max(max_length, length[i])
for i in range(n):
if length[i] == max_length:
result += count[i]
return result