Data Structures & AlgorithmsHardProduct Company SDE · DP warm-up
Problem: Minimum cost to climb stairs where cost[i] is paid when standing on step i and you may take one or two steps. Starting positions are index 0 or 1 and the top is past the last step. Cost = [10, 15, 20]. Run the code.
Problem: Minimum cost to climb stairs where cost[i] is paid when standing on step i and you may take one or two steps. Starting positions are index 0 or 1 and the top is past the last step. Cost = [10, 15, 20]. Run the code.
cost = [10, 15, 20]
a, b = 0, 0
for i in range(len(cost) - 1, -1, -1):
a, b = cost[i] + min(a, b), a
print(min(a, b))What is printed?- A. 25
- B. 30
- C. 20
- D. 15
Correct answer: D. 15
Starting on step 1 (cost 15) and jumping two steps to the top costs 15. The DP keeps the minimum cost from the two next positions at each step.
More Coding Interview Problems questions
- Problem: Houses in a circle hold the amounts [2, 3, 2]. You may not rob two adjacent houses, and the first and last are adjacent. Run the…
- Problem: What is the length of the longest common subsequence of 'ABCBDAB' and 'BDCABA'? python a, b = 'ABCBDAB', 'BDCABA' dp = [[0]…
- Problem: The number of structurally distinct binary search trees on 5 keys is the Catalan number. What does this code print? python dp =…
- Problem: What is the sum over all pairs (i < j) of the Hamming distances (number of differing bits) between the values in [1, 3, 5]? Run…
- Problem: People with weights [3, 2, 2, 1] must cross a river. A boat carries at most 2 people with total weight at most limit = 3, and…
- Problem: Koko eats bananas from piles [3, 6, 7, 11] and has h = 8 hours. She picks one pile per hour and eats up to k bananas from it…