Data Structures & AlgorithmsHardGATE CS · TCS NQT
A path of length 9 is walked from 0 to 9: a step of 1 costs 3 and a step of 2 costs 5. What is the minimum total cost?
- A. 23
- B. 25
- C. 27
- D. 24
Correct answer: A. 23
Let d[i] be the cheapest cost to reach i: d[i] = min(d[i−1] + c1, d[i−2] + c2). Taking the double step whenever it looks cheaper per unit covers the distance, but the choice that is locally cheap can cost more overall.
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
- A 1 × 11 strip must be covered exactly by tiles of lengths 2, 3 and 5 (tiles cannot overlap or stick out). Tile lengths 2, 3, 5 cost 4, 5…
- What is the length of the longest CONTIGUOUS block that appears in both arrays [1, 5, 3, 7, 2, 3, 7, 8] and [4, 7, 2, 3, 7, 9, 1] (equal…
- What is the minimum number of letters you must insert (into either string) so that "leetcode" and "codeleet" become anagrams of each other?
- A string of 8 brackets is "]][[][][" where '[' and ']' appear equally often. Each operation swaps any two characters. What is the minimum…
- For each index i of [4, 2, 5, 3, 1, 6], let dist(i) be the distance to the next index j > i with a[j] < a[i] (dist = n − i if no such…
- Let A = [1, 4, 6, 9], B = [2, 3, 7] and S = [5, 9, 10, 13]. Count the ordered pairs (x from A, y from B) whose sum x + y lies in S.