public int[] nodesBetweenCriticalPoints(ListNode head) {
ListNode pre = head;
head = head.next;
Deque<Integer> q = new LinkedList<>();
int maxDistance = Integer.MIN_VALUE;
int minDistance = Integer.MAX_VALUE;
int pos = 0;
while (head != null && head.next != null) {
int curVal = head.val;
int preVal = pre.val;
int nextVal = head.next.val;
if ((preVal < curVal && nextVal < curVal) || (preVal > curVal && nextVal > curVal)) {
if (!q.isEmpty()) {
maxDistance = Math.max(pos - q.peekFirst(), maxDistance);
minDistance = Math.min(pos - q.peekLast(), minDistance);
}
q.add(pos);
}
pre = head;
head = head.next;
pos++;
}
return new int[]{minDistance == Integer.MAX_VALUE ? -1 : minDistance,maxDistance == Integer.MIN_VALUE ? -1 : maxDistance }
}
lc-2058
原创wx5be5864e766ab 博主文章分类:java ©著作权
©著作权归作者所有:来自51CTO博客作者wx5be5864e766ab的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章