Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
A frog starts on stone 0 with heights [10, 30, 40, 20, 25, 10]. It can jump to stone i+1 or i+2, and the cost of a jump is the absolute height difference. What is the minimum total cost to reach the last stone?
- A. 60
- B. 40
- C. 50
- D. 35
Correct answer: B. 40
dp[i] = min(dp[i−1] + |h[i] − h[i−1]|, dp[i−2] + |h[i] − h[i−2]|). Greedily taking the cheaper next jump can end up on a stone that makes the later jumps dear.
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
- Grid [[3, 1, 4, 1], [5, 9, 2, 6], [5, 3, 5, 8], [9, 7, 9, 3]]. Starting from any cell in the top row, each step goes to the cell directly…
- In how many ways can a 3 × 8 rectangle be tiled exactly with 2 × 1 dominoes (horizontal or vertical)?
- How many DIFFERENT values can the sum of three distinct integers chosen from 1 to 9 take?
- What is the length of the longest substring of "aebcdiouxyab" in which the number of vowels (a, e, i, o, u) equals the number of non-vowels?
- How many index pairs (i < j) in [4, 9, 1, 7, 12, 5, 8, 3] satisfy a[i] − a[j] = 3 (exactly 3, in this order)?
- Starting from the number 1, each operation either doubles the current number or adds 1. What is the minimum number of operations needed to…