2058. Find the Minimum and Maximum Number of Nodes Between Critical Points Medium

@problem@discussion
#Linked List



1/**
2 * [2058] Find the Minimum and Maximum Number of Nodes Between Critical Points
3 *
4 * A critical point in a linked list is defined as either a local maxima or a local minima.
5 * A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.
6 * A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.
7 * Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.
8 * Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].
9 *  
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a1.png" style="width: 148px; height: 55px;" />
12 * Input: head = [3,1]
13 * Output: [-1,-1]
14 * Explanation: There are no critical points in [3,1].
15 * 
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/13/a2.png" style="width: 624px; height: 46px;" />
18 * Input: head = [5,3,1,2,5,1,2]
19 * Output: [1,3]
20 * Explanation: There are three critical points:
21 * - [5,3,<u>1</u>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.
22 * - [5,3,1,2,<u>5</u>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.
23 * - [5,3,1,2,5,<u>1</u>,2]: The sixth node is a local minima because 1 is less than 5 and 2.
24 * The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.
25 * The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.
26 * 
27 * Example 3:
28 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/14/a5.png" style="width: 624px; height: 39px;" />
29 * Input: head = [1,3,2,2,3,2,2,2,7]
30 * Output: [3,3]
31 * Explanation: There are two critical points:
32 * - [1,<u>3</u>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.
33 * - [1,3,2,2,<u>3</u>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.
34 * Both the minimum and maximum distances are between the second and the fifth node.
35 * Thus, minDistance and maxDistance is 5 - 2 = 3.
36 * Note that the last node is not considered a local maxima because it does not have a next node.
37 * 
38 *  
39 * Constraints:
40 * 
41 * 	The number of nodes in the list is in the range [2, 10^5].
42 * 	1 <= Node.val <= 10^5
43 * 
44 */
45pub struct Solution {}
46use crate::util::linked_list::{ListNode, to_list};
47
48// problem: https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
49// discuss: https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53// Definition for singly-linked list.
54// #[derive(PartialEq, Eq, Clone, Debug)]
55// pub struct ListNode {
56//   pub val: i32,
57//   pub next: Option<Box<ListNode>>
58// }
59// 
60// impl ListNode {
61//   #[inline]
62//   fn new(val: i32) -> Self {
63//     ListNode {
64//       next: None,
65//       val
66//     }
67//   }
68// }
69impl Solution {
70    pub fn nodes_between_critical_points(head: Option<Box<ListNode>>) -> Vec<i32> {
71        vec![]
72    }
73}
74
75// submission codes end
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_2058() {
83    }
84}
85


Back
© 2025 bowen.ge All Rights Reserved.