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

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2058

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
        prev, curr = head, head.next
        first = last = None
        i = 1
        res = [inf, -inf]
        while curr.next:
            if curr.val < min(prev.val, curr.next.val) or curr.val > max(
                prev.val, curr.next.val
            ):
                if last is None:
                    first = last = i
                else:
                    res[0] = min(res[0], i - last)
                    res[1] = i - first
                    last = i
            i += 1
            prev, curr = curr, curr.next
        return res if first != last else [-1, -1]