Data Structures & AlgorithmsHardGATE CS · TCS NQT
A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps. Paying cost[i] is needed each time you leave step i. What is the minimum total cost to reach the top (just past the last step)?
- A. 62
- B. 51
- C. 115
- D. 133
Correct answer: B. 51
Let dp[i] be the cheapest cost of standing on step i after paying for it: dp[i] = cost[i] + min(dp[i-1], dp[i-2]). The top is reached from one of the last two steps. Greedily hopping to the cheaper next step misses a cheap cell that sits behind a slightly dearer one.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts…
- Each digit string maps to letters with 1 to A ... 26 to Z. The string "2262012" is split into groups of one or two digits, and each group…
- For the array [3, -7, 4, -2, 5, -9, 6, 1], you may delete at most ONE element (the rest keep their order). Then you take a non-empty…
- Array [2, 3, 1, 1, 4, 2, 1, 3, 1, 2]: from index i you may jump to any index in the range i+1 ... i+arr[i]. You start at index 0 and want…
- You split the array [1, 15, 7, 9, 2, 5, 10] into contiguous non-empty segments, each of length at most 3. A segment contributes (its…
- What is the maximum product of a non-empty contiguous subarray of [-2, 3, -4, 0, -1, 5, -3, -2]?