Ruaconlonton123
Senior Member
Python:
# 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]:
first_i = -1
max_dis = 0
min_dis = float('inf')
last_i = -1
i = 1
while head.next and head.next.next:
if (head.next.val > head.val and head.next.val > head.next.next.val) or (head.next.val < head.val and head.next.val < head.next.next.val):
if first_i == -1:
first_i = i
else:
min_dis = min(i - last_i, min_dis)
last_i = i
i += 1
head = head.next
max_dis = last_i - first_i
if first_i == last_i:
return [-1, -1]
return [min_dis, max_dis
, tôi đợt lâu nhất tới 1 năm đây





copilot tab tab tab