1105. Hole Jumps
A hopscotch lane has n tiles in a row, numbered 0 to n - 1. Tile i carries a spring of strength power[i] (always at least 1). A marble resting on tile i is launched to tile i + power[i], then launched again by the spring there, and so on, until it lands beyond the last tile and leaves the lane. Each launch is one jump, including the final one.
Implement the class HoleJumps:
HoleJumps(int[] power)sets up the lane.void setPower(int index, int power)replaces the spring on tileindex.int[] jumpsOut(int index)places a marble on tileindexand returns[jumps, lastTile]: the number of jumps it makes before leaving the lane, and the number of the last tile it was launched from.
Example 1
- Input:
- operations = ["HoleJumps","jumpsOut","jumpsOut","setPower","jumpsOut"]arguments = [[[2,1,3,1,1]],[0],[1],[2,1],[0]]
- Output:
- [null,[2,2],[2,2],null,[4,4]]
- Explanation:
From tile 0 the marble goes 0, 2, then leaves (2 jumps, last tile 2); from tile 1 it goes 1, 2, out (2 jumps, last tile 2); after tile 2 weakens to 1 the path from tile 0 is 0, 2, 3, 4, out: 4 jumps, last tile 4.
Example 2
- Input:
- operations = ["HoleJumps","jumpsOut","jumpsOut","jumpsOut"]arguments = [[[1,1,1,1,1,1]],[2],[5],[0]]
- Output:
- [null,[4,5],[1,5],[6,5]]
- Explanation:
With every spring equal to 1 the marble visits each tile in turn, so from tile 2 it makes 4 jumps ending at tile 5, from tile 5 one jump, and from tile 0 six jumps.
Example 3
- Input:
- operations = ["HoleJumps","jumpsOut","setPower","jumpsOut","jumpsOut"]arguments = [[[100,100]],[0],[1,1],[1],[0]]
- Output:
- [null,[1,0],null,[1,1],[1,0]]
- Explanation:
A spring of strength 100 flings the marble out of a two-tile lane at once, so tile 0 gives 1 jump ending at tile 0; after tile 1 weakens to 1 it still leaves in one jump from tile 1, and tile 0 is unchanged.
Constraints
1 ≤ power.length ≤ 100000
1 ≤ power[i], power ≤ 109
At most 100000 calls in total; 0 ≤ index < power.length
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Input
- Each case is an operation log.
operationsnames the class first and then each method call;argumentsholds the arguments for each, in the same order. Your answer is one list with a result per operation -nullfor the constructor and for methods that return nothing.
Expected complexity
- Time
- O(sqrt(n)) per operation
- Space
- O(n)