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&nbsp;two distinct critical points and maxDistance is the maximum distance between any&nbsp;two distinct critical points. If there are fewer than two critical points, return [-1, -1].

&nbsp;
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:
- [5,3,1,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.
- [5,3,1,2,5,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.
- [5,3,1,2,5,1,2]: The sixth node is a local minima because 1 is less than 5 and 2.
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:
- [1,3,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.
- [1,3,2,2,3,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.
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.


&nbsp;
Constraints:


	The number of nodes in the list is in the range [2, 105].
	1 &lt;= Node.val &lt;= 105


class Solution:
    def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
        idx, i = [], 1
        prev, cur = head, head.next
        while cur and cur.next:
            if prev.val < cur.val > cur.next.val or prev.val > cur.val < cur.next.val:
                idx.append(i)
            prev = cur
            cur = cur.next
            i += 1

        if len(idx) < 2:
            return [-1, -1]
        
        minDist = min(j - i for i, j in pairwise(idx))
        maxDist = idx[-1] - idx[0]

        return [minDist, maxDist]
class Solution {
    public int[] nodesBetweenCriticalPoints(ListNode head) {
        int res[]=new int[]{-1,-1};
        if(head==null||head.next==null||head.next.next==null) return res;
        int minidx=Integer.MAX_VALUE,curridx=-1,lastidx=-1;
        ListNode prev=head,ptr=head.next;
        int idx=1,minD=Integer.MAX_VALUE;
        while(ptr!=null&&ptr.next!=null){
            if((ptr.val>prev.val&&ptr.val>ptr.next.val)||(ptr.val<prev.val&&ptr.val<ptr.next.val)){
                if(idx<minidx) minidx=idx;
                lastidx=curridx;
                curridx=idx;
                if(lastidx!=-1&&curridx-lastidx<minD) minD=curridx-lastidx;
            }
            prev=ptr;
            ptr=ptr.next;
            idx++;
        }
        if(lastidx==-1) return res;
        else{
            res[0]=minD;
            res[1]=curridx-minidx;
        }
        return res;
    }
}
class Solution {
public:
    vector<int> nodesBetweenCriticalPoints(ListNode* head) {
        if(!head or !head->next or !head->next->next) return {-1,-1};
        int mini = 1e9;
        int prevInd = -1e9;
        int currInd = 0;
        int start = -1e9;
        int prev = head->val;
        head = head->next;

        while(head->next){
            if(prev<head->val and head->next->val<head->val){
                mini = min(mini,currInd-prevInd);
                prevInd = currInd;
                if(start==-1e9) start = currInd;
            }
            if(prev>head->val and head->next->val>head->val){
                mini = min(mini,currInd-prevInd);
                prevInd = currInd;
                if(start==-1e9) start = currInd;
            }
            prev = head->val;
            head = head->next;
            currInd++;
        }

        if(start!=prevInd) return {mini,prevInd-start};
        return {-1,-1};
    }
};
var nodesBetweenCriticalPoints = function(head) {
    const MAX = Number.MAX_SAFE_INTEGER;
    const MIN = Number.MIN_SAFE_INTEGER;
    
    let currNode = head.next;
    let prevVal = head.val;
    
    let minIdx = MAX;
    let maxIdx = MIN;
    
    let minDist = MAX;
    let maxDist = MIN;
    
    for (let i = 1; currNode.next != null; ++i) {
        const currVal = currNode.val;
        const nextNode = currNode.next;
        const nextVal = nextNode.val;
        
        // Triggers when we have either a maxima or a minima
        if ((prevVal < currVal && currVal > nextVal) || (prevVal > currVal && currVal < nextVal)) {
            if (maxIdx != MIN) minDist = Math.min(minDist, i - maxIdx);
            if (minIdx != MAX) maxDist = Math.max(maxDist, i - minIdx);
            
            if (minIdx == MAX) minIdx = i;
            maxIdx = i;
        }
        
        prevVal = currVal;
        currNode = nextNode;
    }

    const minRes = minDist === MAX ? -1 : minDist;
    const maxRes = maxDist === MIN ? -1 : maxDist;
    
    return [minRes, maxRes];
};
Find the Minimum and Maximum Number of Nodes Between Critical Points