on
LeetCode 55. Jump Game
Description for this problem can be found here.
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
Here is the solution in Scala.
First we need to use zipWithIndex
to create a list of tuples where first element is the actual element and second element is index of that element in the list.
After we have the list of tuples then we use foldLeft
to traverse on the list with initial value(0, 0)
. As we traverse we update the maximum index we can reach.
After the foldLeft
operation we check whether the maximum index we can reach is greater or equal to last index.
We return the boolean result of above line of code.