feat: added alternative python solutions.
This commit is contained in:
25
longest-increasing-subsequence/src/alternative-ac.py
Normal file
25
longest-increasing-subsequence/src/alternative-ac.py
Normal file
@@ -0,0 +1,25 @@
|
||||
n = int(input())
|
||||
a = list(map(int, input().split()))
|
||||
|
||||
dp = [1] * n
|
||||
parent = [-1] * n
|
||||
|
||||
for i in range(n):
|
||||
for j in range(i):
|
||||
if a[j] < a[i] and dp[j] + 1 > dp[i]:
|
||||
dp[i] = dp[j] + 1
|
||||
parent[i] = j
|
||||
|
||||
max_length = max(dp)
|
||||
max_index = dp.index(max_length)
|
||||
|
||||
lis = []
|
||||
idx = max_index
|
||||
while idx != -1:
|
||||
lis.append(a[idx])
|
||||
idx = parent[idx]
|
||||
|
||||
lis.reverse()
|
||||
|
||||
print(max_length)
|
||||
print(' '.join(map(str, lis)))
|
||||
Reference in New Issue
Block a user