LeetCode in Kotlin

2420. Find All Good Indices

Medium

You are given a 0-indexed integer array nums of size n and a positive integer k.

We call an index i in the range k <= i < n - k good if the following conditions are satisfied:

Return an array of all good indices sorted in increasing order.

Example 1:

Input: nums = [2,1,1,1,3,4,1], k = 2

Output: [2,3]

Explanation: There are two good indices in the array:

Note that the index 4 is not good because [4,1] is not non-decreasing.

Example 2:

Input: nums = [2,1,1,2], k = 2

Output: []

Explanation: There are no good indices in this array.

Constraints:

Solution

class Solution {
    fun goodIndices(nums: IntArray, k: Int): List<Int> {
        var amount = 1
        val result: MutableList<Int> = ArrayList()
        if (k == 1) {
            for (i in 1 until nums.size - 1) {
                result.add(i)
            }
            return result
        }
        var left = 1
        var right = k + 2
        while (right < nums.size) {
            if (nums[left - 1] >= nums[left] && nums[right - 1] <= nums[right]) {
                amount++
                if (amount >= k) {
                    result.add(left + 1)
                }
            } else {
                amount = 1
            }
            left++
            right++
        }
        return result
    }
}