-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2058 v2 looped.py
30 lines (26 loc) · 932 Bytes
/
m2058 v2 looped.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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]:
if not head or not head.next or not head.next.next :
return [-1, -1]
critPts = []
indx = 0
prev = head.next.val
prevprev = head.val
head = head.next.next
while head :
if (prev > head.val and prev > prevprev) \
or (prev < head.val and prev < prevprev) :
critPts.append(indx - 1)
prevprev = prev
prev = head.val
head = head.next
indx += 1
if len(critPts) < 2 :
return [-1] * 2
return [min([critPts[i] - critPts[i - 1] for i in range(1, len(critPts))]),
critPts[-1] - critPts[0]]