Skip to content

Commit e0d56be

Browse files
committed
2058. Find the Minimum and Maximum Number of Nodes Between Critical Points: RETRY
1 parent a96dc5f commit e0d56be

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,3 +1552,4 @@ mod s2054_two_best_non_overlapping_events;
15521552
mod s2055_plates_between_candles;
15531553
mod s2056_number_of_valid_move_combinations_on_chessboard;
15541554
mod s2057_smallest_index_with_equal_value;
1555+
mod s2058_find_the_minimum_and_maximum_number_of_nodes_between_critical_points;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
*/
45+
pub struct Solution {}
46+
use crate::util::linked_list::{to_list, ListNode};
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+
// }
69+
impl 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)]
78+
mod tests {
79+
use super::*;
80+
81+
#[test]
82+
#[ignore]
83+
fn test_2058_example_1() {
84+
let head = linked![3, 1];
85+
86+
let result = vec![-1, -1];
87+
88+
assert_eq!(Solution::nodes_between_critical_points(head), result);
89+
}
90+
91+
#[test]
92+
#[ignore]
93+
fn test_2058_example_2() {
94+
let head = linked![5, 3, 1, 2, 5, 1, 2];
95+
96+
let result = vec![1, 3];
97+
98+
assert_eq!(Solution::nodes_between_critical_points(head), result);
99+
}
100+
101+
#[test]
102+
#[ignore]
103+
fn test_2058_example_3() {
104+
let head = linked![1, 3, 2, 2, 3, 2, 2, 2, 7];
105+
106+
let result = vec![3, 3];
107+
108+
assert_eq!(Solution::nodes_between_critical_points(head), result);
109+
}
110+
}

0 commit comments

Comments
 (0)