Skip to content

Commit 13521ea

Browse files
committed
feat: add solutions to lc problem: No.2058
No.2058.Find the Minimum and Maximum Number of Nodes Between Critical Points
1 parent 1a074ae commit 13521ea

File tree

6 files changed

+447
-2
lines changed

6 files changed

+447
-2
lines changed

solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README.md

+155-1
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,83 @@
8181

8282
<!-- 这里可写通用的实现逻辑 -->
8383

84+
遍历链表,维护第一个临界点 first、最后一个临界点 last,以及相邻临界点的最小距离。
85+
8486
<!-- tabs:start -->
8587

8688
### **Python3**
8789

8890
<!-- 这里可写当前语言的特殊实现逻辑 -->
8991

9092
```python
91-
93+
# Definition for singly-linked list.
94+
# class ListNode:
95+
# def __init__(self, val=0, next=None):
96+
# self.val = val
97+
# self.next = next
98+
class Solution:
99+
def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
100+
prev, curr = head, head.next
101+
first = last = None
102+
i = 1
103+
ans = [float('inf'), float('-inf')]
104+
while curr.next:
105+
if curr.val < min(prev.val, curr.next.val) or curr.val > max(prev.val, curr.next.val):
106+
if last is None:
107+
first = last = i
108+
else:
109+
ans[0] = min(ans[0], i - last)
110+
ans[1] = i - first
111+
last = i
112+
i += 1
113+
prev, curr = curr, curr.next
114+
return ans if first != last else [-1, -1]
92115
```
93116

94117
### **Java**
95118

96119
<!-- 这里可写当前语言的特殊实现逻辑 -->
97120

98121
```java
122+
/**
123+
* Definition for singly-linked list.
124+
* public class ListNode {
125+
* int val;
126+
* ListNode next;
127+
* ListNode() {}
128+
* ListNode(int val) { this.val = val; }
129+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
130+
* }
131+
*/
132+
class Solution {
133+
134+
public int[] nodesBetweenCriticalPoints(ListNode head) {
135+
ListNode prev = head;
136+
ListNode curr = head.next;
137+
int first = 0, last = 0;
138+
int i = 1;
139+
int[] ans = new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE };
140+
while (curr.next != null) {
141+
if (
142+
curr.val < Math.min(prev.val, curr.next.val) ||
143+
curr.val > Math.max(prev.val, curr.next.val)
144+
) {
145+
if (last == 0) {
146+
first = i;
147+
last = i;
148+
} else {
149+
ans[0] = Math.min(ans[0], i - last);
150+
ans[1] = i - first;
151+
last = i;
152+
}
153+
}
154+
++i;
155+
prev = curr;
156+
curr = curr.next;
157+
}
158+
return first == last ? new int[] { -1, -1 } : ans;
159+
}
160+
}
99161

100162
```
101163

@@ -142,6 +204,98 @@ function nodesBetweenCriticalPoints(head: ListNode | null): number[] {
142204
}
143205
```
144206

207+
### **C++**
208+
209+
```cpp
210+
/**
211+
* Definition for singly-linked list.
212+
* struct ListNode {
213+
* int val;
214+
* ListNode *next;
215+
* ListNode() : val(0), next(nullptr) {}
216+
* ListNode(int x) : val(x), next(nullptr) {}
217+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
218+
* };
219+
*/
220+
class Solution {
221+
public:
222+
vector<int> nodesBetweenCriticalPoints(ListNode* head) {
223+
ListNode* prev = head;
224+
ListNode* curr = head->next;
225+
int first = 0, last = 0;
226+
int i = 1;
227+
vector<int> ans(2, INT_MAX);
228+
while (curr->next)
229+
{
230+
if (curr->val < min(prev->val, curr->next->val) || curr->val > max(prev->val, curr->next->val))
231+
{
232+
if (last == 0) first = i;
233+
else
234+
{
235+
ans[0] = min(ans[0], i - last);
236+
ans[1] = i - first;
237+
}
238+
last = i;
239+
}
240+
++i;
241+
prev = curr;
242+
curr = curr->next;
243+
}
244+
if (first == last) return {-1, -1};
245+
return ans;
246+
}
247+
};
248+
```
249+
250+
### **Go**
251+
252+
```go
253+
/**
254+
* Definition for singly-linked list.
255+
* type ListNode struct {
256+
* Val int
257+
* Next *ListNode
258+
* }
259+
*/
260+
func nodesBetweenCriticalPoints(head *ListNode) []int {
261+
prev, curr := head, head.Next
262+
first, last := 0, 0
263+
i := 1
264+
ans := []int{math.MaxInt32, 0}
265+
for curr.Next != nil {
266+
if curr.Val < min(prev.Val, curr.Next.Val) || curr.Val > max(prev.Val, curr.Next.Val) {
267+
if last == 0 {
268+
first, last = i, i
269+
} else {
270+
ans[0] = min(ans[0], i-last)
271+
ans[1] = i - first
272+
last = i
273+
}
274+
}
275+
i++
276+
prev, curr = curr, curr.Next
277+
}
278+
if first == last {
279+
return []int{-1, -1}
280+
}
281+
return ans
282+
}
283+
284+
func min(a, b int) int {
285+
if a < b {
286+
return a
287+
}
288+
return b
289+
}
290+
291+
func max(a, b int) int {
292+
if a > b {
293+
return a
294+
}
295+
return b
296+
}
297+
```
298+
145299
### **...**
146300

147301
```

solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README_EN.md

+153-1
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,72 @@ Note that the last node is not considered a local maxima because it does not hav
7272
### **Python3**
7373

7474
```python
75-
75+
# Definition for singly-linked list.
76+
# class ListNode:
77+
# def __init__(self, val=0, next=None):
78+
# self.val = val
79+
# self.next = next
80+
class Solution:
81+
def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
82+
prev, curr = head, head.next
83+
first = last = None
84+
i = 1
85+
ans = [float('inf'), float('-inf')]
86+
while curr.next:
87+
if curr.val < min(prev.val, curr.next.val) or curr.val > max(prev.val, curr.next.val):
88+
if last is None:
89+
first = last = i
90+
else:
91+
ans[0] = min(ans[0], i - last)
92+
ans[1] = i - first
93+
last = i
94+
i += 1
95+
prev, curr = curr, curr.next
96+
return ans if first != last else [-1, -1]
7697
```
7798

7899
### **Java**
79100

80101
```java
102+
/**
103+
* Definition for singly-linked list.
104+
* public class ListNode {
105+
* int val;
106+
* ListNode next;
107+
* ListNode() {}
108+
* ListNode(int val) { this.val = val; }
109+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
110+
* }
111+
*/
112+
class Solution {
113+
114+
public int[] nodesBetweenCriticalPoints(ListNode head) {
115+
ListNode prev = head;
116+
ListNode curr = head.next;
117+
int first = 0, last = 0;
118+
int i = 1;
119+
int[] ans = new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE };
120+
while (curr.next != null) {
121+
if (
122+
curr.val < Math.min(prev.val, curr.next.val) ||
123+
curr.val > Math.max(prev.val, curr.next.val)
124+
) {
125+
if (last == 0) {
126+
first = i;
127+
last = i;
128+
} else {
129+
ans[0] = Math.min(ans[0], i - last);
130+
ans[1] = i - first;
131+
last = i;
132+
}
133+
}
134+
++i;
135+
prev = curr;
136+
curr = curr.next;
137+
}
138+
return first == last ? new int[] { -1, -1 } : ans;
139+
}
140+
}
81141

82142
```
83143

@@ -124,6 +184,98 @@ function nodesBetweenCriticalPoints(head: ListNode | null): number[] {
124184
}
125185
```
126186

187+
### **C++**
188+
189+
```cpp
190+
/**
191+
* Definition for singly-linked list.
192+
* struct ListNode {
193+
* int val;
194+
* ListNode *next;
195+
* ListNode() : val(0), next(nullptr) {}
196+
* ListNode(int x) : val(x), next(nullptr) {}
197+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
198+
* };
199+
*/
200+
class Solution {
201+
public:
202+
vector<int> nodesBetweenCriticalPoints(ListNode* head) {
203+
ListNode* prev = head;
204+
ListNode* curr = head->next;
205+
int first = 0, last = 0;
206+
int i = 1;
207+
vector<int> ans(2, INT_MAX);
208+
while (curr->next)
209+
{
210+
if (curr->val < min(prev->val, curr->next->val) || curr->val > max(prev->val, curr->next->val))
211+
{
212+
if (last == 0) first = i;
213+
else
214+
{
215+
ans[0] = min(ans[0], i - last);
216+
ans[1] = i - first;
217+
}
218+
last = i;
219+
}
220+
++i;
221+
prev = curr;
222+
curr = curr->next;
223+
}
224+
if (first == last) return {-1, -1};
225+
return ans;
226+
}
227+
};
228+
```
229+
230+
### **Go**
231+
232+
```go
233+
/**
234+
* Definition for singly-linked list.
235+
* type ListNode struct {
236+
* Val int
237+
* Next *ListNode
238+
* }
239+
*/
240+
func nodesBetweenCriticalPoints(head *ListNode) []int {
241+
prev, curr := head, head.Next
242+
first, last := 0, 0
243+
i := 1
244+
ans := []int{math.MaxInt32, 0}
245+
for curr.Next != nil {
246+
if curr.Val < min(prev.Val, curr.Next.Val) || curr.Val > max(prev.Val, curr.Next.Val) {
247+
if last == 0 {
248+
first, last = i, i
249+
} else {
250+
ans[0] = min(ans[0], i-last)
251+
ans[1] = i - first
252+
last = i
253+
}
254+
}
255+
i++
256+
prev, curr = curr, curr.Next
257+
}
258+
if first == last {
259+
return []int{-1, -1}
260+
}
261+
return ans
262+
}
263+
264+
func min(a, b int) int {
265+
if a < b {
266+
return a
267+
}
268+
return b
269+
}
270+
271+
func max(a, b int) int {
272+
if a > b {
273+
return a
274+
}
275+
return b
276+
}
277+
```
278+
127279
### **...**
128280

129281
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
vector<int> nodesBetweenCriticalPoints(ListNode* head) {
14+
ListNode* prev = head;
15+
ListNode* curr = head->next;
16+
int first = 0, last = 0;
17+
int i = 1;
18+
vector<int> ans(2, INT_MAX);
19+
while (curr->next)
20+
{
21+
if (curr->val < min(prev->val, curr->next->val) || curr->val > max(prev->val, curr->next->val))
22+
{
23+
if (last == 0) first = i;
24+
else
25+
{
26+
ans[0] = min(ans[0], i - last);
27+
ans[1] = i - first;
28+
}
29+
last = i;
30+
}
31+
++i;
32+
prev = curr;
33+
curr = curr->next;
34+
}
35+
if (first == last) return {-1, -1};
36+
return ans;
37+
}
38+
};

0 commit comments

Comments
 (0)