NeetCode #317LC-2058MediumLinked List
← Back to All Problems#317 · #2058 · Find the Minimum and Maximum Number of Nodes Between Critical Points(找出临界点之间的最小和最大距离)
📌 Problem Statement & Constraints
A critical point in a linked list is either a local minimum or a local maximum. Given the head of a linked list, return
[minDistance, maxDistance] where the distances are between pairs of critical points; if there are fewer than two, return [-1, -1]. Constraints: the number of nodes is in [2, 10^5], 1 <= Node.val <= 10^5.💡 Core Algorithmic Approaches
- Walk three consecutive nodes at a time and test whether the middle one is a local extremum.
- Collect the indices of all critical points.
- The maximum distance is from the first to the last; the minimum is the smallest gap between consecutive critical points.
- Returning
[-1, -1]when fewer than two critical points exist.
💻 Benchmark Python3 Implementation
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]
idx = []
i = 1 # index of the middle node (head is 0)
prev, cur, nxt = head, head.next, head.next.next
while nxt:
if (cur.val > prev.val and cur.val > nxt.val) or \
(cur.val < prev.val and cur.val < nxt.val):
idx.append(i) # local extremum
prev, cur, nxt = cur, nxt, nxt.next
i += 1
if len(idx) < 2:
return [-1, -1]
min_dist = min(idx[k] - idx[k - 1] for k in range(1, len(idx)))
max_dist = idx[-1] - idx[0]
return [min_dist, max_dist]⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one pass over the list.
💾 Space Complexity
O(k) for the critical-point indices, where k is their count (O(1) if the minimum and maximum are tracked on the fly).
⚠️ Interview Pitfalls & Follow-ups
- Testing only the middle against one neighbour: a critical point must be strictly greater than both neighbours or strictly less than both.
- Using
>=in the comparisons: equality disqualifies the node from being a critical point. - Computing the maximum distance as the largest consecutive gap: the maximum is first-to-last, which the sorted index list gives directly.
- Forgetting the fewer-than-two case: return
[-1, -1].
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Linked List Problems
中文速览与面试要点
找出临界点之间的最小和最大距离
链表中的 临界点 定义为一个 局部极大值点 或 局部极小值点 。如果当前节点的值 严格大于 前一个节点的值和后一个节点的值,它就是局部极大值点;如果当前节点的值 严格小于 前一个节点的值和后一个节点的值,它就是局部极小值点。给你链表的头节点 head,返回一个长度为 2 的数组 [minDistance, maxDistance] ,其中 minDistance 是任意两个不同临界点之间的最小距离, maxDistance 是任意两个不同临界点之间的最大距离。如果临界点少于两个,则返回 [−1, −1] 。约束:链表中节点的数量在范围 [2, 10^5] 内;1 ≤ Node.val ≤ 10^5。
核心要点
- 一次遍历收集临界点下标(标准解) :用三个指针 prev 、 cur 、 next 滑动,判断 cur 是否为局部极值点,记录其下标。
- 最小距离 :相邻两个临界点之间的距离,取最小值(遍历收集的下标数组即可)。
- 最大距离 :首尾两个临界点之间的距离,即 idx[-1] − idx[0] 。
- 为什么最小距离只需看相邻 :下标数组是升序的,任意两点的距离至少为相邻两点距离之和,因此最小距离必然出现在相邻对中。
常见陷阱
- 临界点判断用 >= 而非 > :题目要求 严格 大于/小于。
- 遍历范围写成 while cur :需要访问 cur.next ,因此必须是 while cur and cur.next 。
- 下标 i 的初始值写成 0 : cur 从 head.next 开始,其下标是 1。
- 最大距离用相邻距离的最大值 :错误。最大距离是「首尾两个临界点之间的距离」。
- 临界点少于 2 个时返回 [0, 0] :应返回 [-1, -1] 。
参考实现
class Solution:
def nodesBetweenCriticalPoints(self, head: 'ListNode') -> list[int]:
idx = [] # 收集所有临界点的下标
prev = head
cur = head.next
i = 1
while cur and cur.next:
if (cur.val > prev.val and cur.val > cur.next.val) or \
(cur.val < prev.val and cur.val < cur.next.val):
idx.append(i) # cur 是临界点
prev = cur
cur = cur.next
i += 1
if len(idx) < 2:
return [-1, -1]
min_d = min(idx[k + 1] - idx[k] for k in range(len(idx) - 1))
max_d = idx[-1] - idx[0]
return [min_d, max_d]