LeetCode in Kotlin

2058. Find the Minimum and Maximum Number of Nodes Between Critical Points

Medium

A critical point in a linked list is defined as either a local maxima or a local minima.

A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.

A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.

Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.

Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].

Example 1:

Input: head = [3,1]

Output: [-1,-1]

Explanation: There are no critical points in [3,1].

Example 2:

Input: head = [5,3,1,2,5,1,2]

Output: [1,3]

Explanation: There are three critical points:

The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.

The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.

Example 3:

Input: head = [1,3,2,2,3,2,2,2,7]

Output: [3,3]

Explanation: There are two critical points:

Both the minimum and maximum distances are between the second and the fifth node.

Thus, minDistance and maxDistance is 5 - 2 = 3.

Note that the last node is not considered a local maxima because it does not have a next node.

Constraints:

Solution

import com_github_leetcode.ListNode

class Solution {
    fun nodesBetweenCriticalPoints(head: ListNode?): IntArray {
        val arr = IntArray(2)
        if (head?.next == null || head.next!!.next == null) {
            arr[0] = -1
            arr[1] = -1
            return arr
        }
        var c1 = 0
        var c2 = 0
        var c3 = 0
        var prev = head
        var curr = prev.next
        var sec = curr!!.next
        var count = 1
        var min = Int.MAX_VALUE
        while (sec != null) {
            count++
            if (curr!!.`val` > prev!!.`val` && curr.`val` > sec.`val` ||
                curr.`val` < prev.`val` && curr.`val` < sec.`val`
            ) {
                if (c1 == 0) {
                    c1 = count
                    c2 = count
                } else {
                    c3 = count
                    min = (c3 - c2).coerceAtMost(min)
                    c2 = c3
                }
            }
            prev = prev.next
            curr = curr.next
            sec = sec.next
        }
        if (c3 == 0) {
            arr[0] = -1
            arr[1] = -1
        } else {
            arr[1] = c3 - c1
            arr[0] = min
        }
        return arr
    }
}