Opening your workspace…
Workspace/Coding room
Opening your workspace…
Deep in the Candlefish Caves, the tide is rising around a lost moonstone. Each cave has a candlefish whose glow shows how many caves ahead you can safely leap from there. You may leap fewer caves than that, but never backward, and you must land in a cave with a candlefish to leap again. Find the fewest leaps needed to reach the final cave from the entrance. Return -1 if it cannot be reached. With zero or one cave, you are already at the destination, so return 0.
[2,3,1,1,4]Output2Leap from cave 0 to cave 1, then from cave 1 to cave 4. One leap cannot cover the whole passage.
[3,1,2,0,4,1]Output3The shortest route is cave 0 to cave 2 to cave 4 to cave 5. The fish at cave 0 cannot reach cave 4 in one leap.
Track the farthest cave reachable with the current leap count.
function solve(reach) { // Write your solution here. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.