def lis_length_v1(seq) -> int:
arr = []
for val in seq:
pos = bisect.bisect_left(arr, val)
if pos == len(arr):
arr.append(val)
else:
arr[pos] = val
return len(arr)
def lis_length_v2(seq) -> int:
arr = [seq[0]]
for val in seq:
if val > arr[-1]:
arr.append(val)
else:
arr[bisect.bisect_left(arr, val)] = val
return len(arr)
def lis_v1(seq: list[int]) -> list[int]:
prev = []
arr = [seq[0]]
ind_arr = [0]
for i, val in enumerate(seq):
if val > arr[-1]:
prev.append(ind_arr[-1])
arr.append(val)
ind_arr.append(i)
else:
lis_len = bisect.bisect_left(arr, val)
arr[lis_len] = val
ind_arr[lis_len] = i
prev.append(ind_arr[lis_len - 1] if lis_len > 0 else None)
ret = []
ind = ind_arr[-1]
while ind is not None:
ret.append(seq[ind])
ind = prev[ind]
return ret[::-1]
def lis_v2(seq: list[int]) -> list[int]:
arr = [seq[0]]
lis_lengths = []
for val in seq:
if val > arr[-1]:
lis_lengths.append(len(arr))
arr.append(val)
else:
lis_len = bisect.bisect_left(arr, val)
lis_lengths.append(lis_len)
arr[lis_len] = val
p = len(arr) - 1
ret = []
for val, lis_len in zip(reversed(seq), reversed(lis_lengths)):
if lis_len == p:
ret.append(val)
p -= 1
return ret[::-1]