Frog Jump
Last updated
Last updated
Given an increasing sequence of stone positions, determine the doability jumping from the first stone to the last one. The first jump is always 1. The next jump can be k-1, k, or k+1.
What are the numbers not in the sequence? They are water positions (not jumpable). How large will the stone sequence be? The number of stones is between 2 to 1100. We will at least have the starting stone and the ending stone right? Yes. The first stone is always at pos 0.
This is a doability problem ({0,1} guarantees optimal substructure). And subproblems are overlapping (draw out an example). The subproblem needs to be more specific than just the landing position. The jump steps that are required to get there need to be in the state as well.
This block of code will get Memory Limit Exceeded. The online judge offers the following hint
Your code cost too much memory than we expected. Check your space complexity. Memory limit exceeded usually caused by you create a 2D-array which is unnecessary.
From the state graph, the rows that are water rows can certainly be reduced. We can take advantage of the resizing ability of HashMap to store the stone rows as keys. We can save space from the columns as well. We can use a HashSet to store all the possible jumps to get to that row.