feat: added alternative python solutions.
This commit is contained in:
34
longest-increasing-subsequence-II/src/alternative-ac.py
Normal file
34
longest-increasing-subsequence-II/src/alternative-ac.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import bisect
|
||||
|
||||
n = int(input())
|
||||
a = list(map(int, input().split()))
|
||||
|
||||
tails = []
|
||||
tail_indices = []
|
||||
parent = [-1] * n
|
||||
|
||||
for i in range(n):
|
||||
pos = bisect.bisect_left(tails, a[i])
|
||||
|
||||
if pos > 0:
|
||||
parent[i] = tail_indices[pos - 1]
|
||||
|
||||
if pos == len(tails):
|
||||
tails.append(a[i])
|
||||
tail_indices.append(i)
|
||||
else:
|
||||
tails[pos] = a[i]
|
||||
tail_indices[pos] = i
|
||||
|
||||
max_length = len(tails)
|
||||
|
||||
lis = []
|
||||
if max_length > 0:
|
||||
idx = tail_indices[-1]
|
||||
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