-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJul-5-24.py
39 lines (39 loc) · 1.2 KB
/
Jul-5-24.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
31
32
33
34
35
36
37
38
39
# 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]:
res = [float('inf'),float('-inf')]
n = 0
curr = head
while curr:
curr = curr.next
n += 1
arr = []
if n>=4:
back,curr,front = head,head.next,head.next.next
i = 1
while front:
if back.val<curr.val>front.val or back.val>curr.val<front.val:
arr.append(i)
back = curr
curr = front
front = front.next
i += 1
if len(arr)<2:
return [-1,-1]
l,r = 0,1
maX,miN = arr[0],arr[0]
minLocal = arr[r]-arr[l]
print(arr)
while r<len(arr):
minLocal = min(minLocal,arr[r]-arr[l])
maX,miN = max(maX,arr[r]),min(miN,arr[r])
l += 1
r += 1
res[0] = minLocal
res[1] = maX-miN
return res
return [-1,-1]