LeetCode in Kotlin

2770. Maximum Number of Jumps to Reach the Last Index

Medium

You are given a 0-indexed array nums of n integers and an integer target.

You are initially positioned at index 0. In one step, you can jump from index i to any index j such that:

Return the maximum number of jumps you can make to reach index n - 1.

If there is no way to reach index n - 1, return -1.

Example 1:

Input: nums = [1,3,6,4,1,2], target = 2

Output: 3

Explanation:

To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:

It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps.

Hence, the answer is 3.

Example 2:

Input: nums = [1,3,6,4,1,2], target = 3

Output: 5

Explanation:

To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:

It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps.

Hence, the answer is 5.

Example 3:

Input: nums = [1,3,6,4,1,2], target = 0

Output: -1

Explanation: It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.

Constraints:

Solution

class Solution {
    private class Pair(var prev: Int, var len: Int)

    fun maximumJumps(nums: IntArray, target: Int): Int {
        val arr = arrayOfNulls<Pair>(nums.size)
        arr[0] = Pair(0, 0)
        for (i in 1 until nums.size) {
            arr[i] = Pair(-1, 0)
            for (j in i - 1 downTo 0) {
                if (Math.abs(nums[i] - nums[j]) <= target &&
                    arr[j]!!.prev != -1 && arr[j]!!.len + 1 > arr[i]!!.len
                ) {
                    arr[i]!!.prev = j
                    arr[i]!!.len = arr[j]!!.len + 1
                }
            }
        }
        return if (arr[nums.size - 1]!!.len > 0) arr[nums.size - 1]!!.len else -1
    }
}