Data Structures & AlgorithmsVery hard
In the jump game (minimum number of jumps), why does a single left-to-right scan with a 'current boundary' count the jumps correctly?
- A. Each time the scan reaches the boundary of the current jump level, one more jump is needed and the boundary extends to the furthest index reachable so far.
- B. Every element is a jump of length one, so the count is the array length.
- C. The scan must restart from index 0 after each jump.
- D. The jumps are the same as the number of zeros in the array.
Correct answer: A. Each time the scan reaches the boundary of the current jump level, one more jump is needed and the boundary extends to the furthest index reachable so far.
The set of indices reachable in k jumps is a prefix of the array, so the furthest reach at each level determines the next level. This gives the minimum number of levels in a single pass.
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 the 'first non-repeating character in a stream' problem, why is a queue with lazy removal used?
- Why is a hash map (not a sorted array) used to find the longest run of consecutive integers in an unsorted array in linear expected time?
- When tracking the first occurrence of every value for an order-preserving de-duplication, which structure gives expected O(1) membership…
- A subarray's average is at least K exactly when which quantity is non-negative for that subarray?
- Convert the infix expression a+b(c-d)/e to postfix with the shunting-yard algorithm (operators '+', '-', '', '/' with '' and '/' binding…
- A 3-litre jug and a 5-litre jug start empty and you can fill either, empty either, or pour one into the other until one is full or empty…