Data Structures & AlgorithmsHardProduct Company SDE · DP with binary search
Problem: Find the length of the longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18] with the O(n log n) tails method.
Problem: Find the length of the longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18] with the O(n log n) tails method.
import bisect
a = [10, 9, 2, 5, 3, 7, 101, 18]
tails = []
for x in a:
i = bisect.bisect_left(tails, x)
if i == len(tails):
tails.append(x)
else:
tails[i] = x
print(len(tails))What is printed?- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
The tails array evolves as [10], [9], [2], [2,5], [2,3], [2,3,7], [2,3,7,101], then [2,3,7,18], so the length is 4. The subsequence 2, 3, 7, 18 realises it.
More Coding Interview Problems questions
- Problem: Coins of values 2, 5, 10 and 1 are available with unlimited copies. What is the minimum number of coins summing to 27? Run the…
- Problem: How many ways are there to make change for 100 using coins of 1, 5, 10, 25 and 50 (order does not matter)? Run the code. python…
- Problem: Find the longest palindromic substring length of 'forgeeksskeegfor' by expanding around each centre. python s =…
- Problem: What does the following print? It counts distinct values in each window of size 3. python from collections import Counter a = [1…
- Problem: Rotate the array [1, 2, 3, 4, 5, 6, 7] to the right by k = 3 using the three-reversal method. What is printed? python a = [1, 2…
- Problem: How many unordered pairs of distinct elements of [1, 5, 3, 9, 7, 2] have absolute difference exactly 2? Run the code. python a =…