Question 51
MediumHow does the O(n log n) longest increasing subsequence method work?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
How does the O(n log n) longest increasing subsequence method work?
It uses a stack
It sorts the array
It uses a hash map
It keeps the smallest tail for each length and uses binary search
Sign up free to see the answer and a step-by-step explanation.
Which problem is solved with interval DP, such as dp[i][j] over substrings?
Longest palindromic subsequence
BFS
Binary search
Two-sum
Sign up free to see the answer and a step-by-step explanation.
In the rod-cutting problem with prices p[1..n], what does dp[n] = max(p[i] + dp[n − i]) express?
Cutting into equal pieces
Sorting the prices
Selling the rod whole
Making the first cut of length i and solving the rest optimally
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the Floyd–Warshall DP in terms of its subproblems?
O(V³): V choices of intermediate vertex for each of V² pairs
O(E log V)
O(V²)
O(2^V)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a, b = "horse", "ros"
dp = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
for i in range(len(a) + 1):
dp[i][0] = i
for j in range(len(b) + 1):
dp[0][j] = j
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
dp[i][j] = dp[i-1][j-1] if a[i-1] == b[j-1] else 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
print(dp[-1][-1])
4
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
items, W = [(1, 1), (3, 4), (4, 5), (5, 7)], 7
dp = [0] * (W + 1)
for w, v in items:
for c in range(W, w - 1, -1):
dp[c] = max(dp[c], dp[c - w] + v)
print(dp[W])
10
7
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a, t = [3, 34, 4, 12, 5, 2], 9
dp = [True] + [False] * t
for x in a:
for s in range(t, x - 1, -1):
dp[s] = dp[s] or dp[s - x]
print(dp[t])
Error
None
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
p = [10, 30, 5, 60]
n = len(p) - 1
dp = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
dp[i][j] = min(dp[i][k] + dp[k+1][j] + p[i] * p[k+1] * p[j+1] for k in range(i, j))
print(dp[0][n-1])
6000
4500
Sign up free to see the answer and a step-by-step explanation.
When you iterate capacity downwards in a 1D 0/1 knapsack array, what does that ensure?
The array is sorted
The answer is computed faster
Each item is used at most once
Each item can be used many times
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
s = "aab"
n = len(s)
is_pal = [[False] * n for _ in range(n)]
cuts = [0] * n
for j in range(n):
best = j
for i in range(j + 1):
if s[i] == s[j] and (j - i < 2 or is_pal[i+1][j-1]):
is_pal[i][j] = True
best = 0 if i == 0 else min(best, cuts[i-1] + 1)
cuts[j] = best
print(cuts[-1])
2
3
Sign up free to see the answer and a step-by-step explanation.
2
5
9
8
True
False
3000
7500
0
1