LeetCode in Kotlin

3250. Find the Count of Monotonic Pairs I

Hard

You are given an array of positive integers nums of length n.

We call a pair of non-negative integer arrays (arr1, arr2) monotonic if:

Return the count of monotonic pairs.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: nums = [2,3,2]

Output: 4

Explanation:

The good pairs are:

  1. ([0, 1, 1], [2, 2, 1])
  2. ([0, 1, 2], [2, 2, 0])
  3. ([0, 2, 2], [2, 1, 0])
  4. ([1, 2, 2], [1, 1, 0])

Example 2:

Input: nums = [5,5,5,5]

Output: 126

Constraints:

Solution

import kotlin.math.max
import kotlin.math.min

class Solution {
    fun countOfPairs(nums: IntArray): Int {
        val maxShift = IntArray(nums.size)
        maxShift[0] = nums[0]
        var currShift = 0
        for (i in 1 until nums.size) {
            currShift = max(currShift, (nums[i] - maxShift[i - 1]))
            maxShift[i] = min(maxShift[i - 1], (nums[i] - currShift))
            if (maxShift[i] < 0) {
                return 0
            }
        }
        val cases = getAllCases(nums, maxShift)
        return cases[nums.size - 1]!![maxShift[nums.size - 1]]
    }

    private fun getAllCases(nums: IntArray, maxShift: IntArray): Array<IntArray?> {
        var currCases: IntArray
        val cases = arrayOfNulls<IntArray>(nums.size)
        cases[0] = IntArray(maxShift[0] + 1)
        for (i in cases[0]!!.indices) {
            cases[0]!![i] = i + 1
        }
        for (i in 1 until nums.size) {
            currCases = IntArray(maxShift[i] + 1)
            currCases[0] = 1
            for (j in 1 until currCases.size) {
                val prevCases =
                    if (j < cases[i - 1]!!.size
                    ) {
                        cases[i - 1]!![j]
                    } else {
                        cases[i - 1]!![cases[i - 1]!!.size - 1]
                    }
                currCases[j] = (currCases[j - 1] + prevCases) % (1000000000 + 7)
            }
            cases[i] = currCases
        }
        return cases
    }
}