@@ -77,7 +77,11 @@ Note that the last node is not considered a local maxima because it does not hav
77
77
78
78
<!-- solution:start -->
79
79
80
- ### Solution 1
80
+ ### Solution 1: Direct Traversal
81
+
82
+ Based on the problem description, we need to find the positions of the first and last critical points in the linked list, $\text{first}$ and $\text{last}$, respectively. This allows us to calculate the maximum distance $\text{maxDistance} = \text{last} - \text{first}$. For the minimum distance $\text{minDistance}$, we need to traverse the linked list, calculate the distance between two adjacent critical points, and take the minimum value.
83
+
84
+ The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
81
85
82
86
<!-- tabs:start -->
83
87
@@ -91,23 +95,21 @@ Note that the last node is not considered a local maxima because it does not hav
91
95
# self.next = next
92
96
class Solution :
93
97
def nodesBetweenCriticalPoints (self , head : Optional[ListNode]) -> List[int ]:
94
- prev, curr = head, head.next
95
- first = last = None
96
- i = 1
97
98
ans = [inf, - inf]
98
- while curr.next:
99
- if curr.val < min (prev.val, curr.next.val) or curr.val > max (
100
- prev.val, curr.next.val
101
- ):
102
- if last is None :
99
+ first = last = - 1
100
+ i = 0
101
+ while head.next.next:
102
+ a, b, c = head.val, head.next.val, head.next.next.val
103
+ if a > b < c or a < b > c:
104
+ if last == - 1 :
103
105
first = last = i
104
106
else :
105
107
ans[0 ] = min (ans[0 ], i - last)
106
- ans[1 ] = i - first
107
108
last = i
109
+ ans[1 ] = max (ans[1 ], last - first)
108
110
i += 1
109
- prev, curr = curr, curr .next
110
- return ans if first != last else [ - 1 , - 1 ]
111
+ head = head .next
112
+ return [ - 1 , - 1 ] if first == last else ans
111
113
```
112
114
113
115
#### Java
@@ -124,28 +126,21 @@ class Solution:
124
126
* }
125
127
*/
126
128
class Solution {
127
-
128
129
public int [] nodesBetweenCriticalPoints (ListNode head ) {
129
- ListNode prev = head;
130
- ListNode curr = head. next;
131
- int first = 0 , last = 0 ;
132
- int i = 1 ;
133
- int [] ans = new int [] {Integer . MAX_VALUE , Integer . MIN_VALUE };
134
- while (curr. next != null ) {
135
- if (curr. val < Math . min(prev. val, curr. next. val)
136
- || curr. val > Math . max(prev. val, curr. next. val)) {
137
- if (last == 0 ) {
130
+ int [] ans = {1 << 30 , 0 };
131
+ int first = - 1 , last = - 1 ;
132
+ for (int i = 0 ; head. next. next != null ; head = head. next, ++ i) {
133
+ int a = head. val, b = head. next. val, c = head. next. next. val;
134
+ if (b < Math . min(a, c) || b > Math . max(a, c)) {
135
+ if (last == - 1 ) {
138
136
first = i;
139
137
last = i;
140
138
} else {
141
139
ans[0 ] = Math . min(ans[0 ], i - last);
142
- ans[1 ] = i - first;
143
140
last = i;
141
+ ans[1 ] = Math . max(ans[1 ], last - first);
144
142
}
145
143
}
146
- ++ i;
147
- prev = curr;
148
- curr = curr. next;
149
144
}
150
145
return first == last ? new int [] {- 1 , - 1 } : ans;
151
146
}
@@ -168,27 +163,22 @@ class Solution {
168
163
class Solution {
169
164
public:
170
165
vector<int > nodesBetweenCriticalPoints(ListNode* head) {
171
- ListNode* prev = head;
172
- ListNode* curr = head->next;
173
- int first = 0, last = 0;
174
- int i = 1;
175
- vector<int > ans(2, INT_MAX);
176
- while (curr->next) {
177
- if (curr->val < min(prev->val, curr->next->val) || curr->val > max(prev->val, curr->next->val)) {
178
- if (last == 0)
166
+ vector<int > ans = {1 << 30, 0};
167
+ int first = -1, last = -1;
168
+ for (int i = 0; head->next->next; head = head->next, ++i) {
169
+ int a = head->val, b = head->next->val, c = head->next->next->val;
170
+ if (b < min(a, c) || b > max(a, c)) {
171
+ if (last == -1) {
179
172
first = i;
180
- else {
173
+ last = i;
174
+ } else {
181
175
ans[ 0] = min(ans[ 0] , i - last);
182
- ans[ 1] = i - first;
176
+ last = i;
177
+ ans[ 1] = max(ans[ 1] , last - first);
183
178
}
184
- last = i;
185
179
}
186
- ++i;
187
- prev = curr;
188
- curr = curr->next;
189
180
}
190
- if (first == last) return {-1, -1};
191
- return ans;
181
+ return first == last ? vector<int >{-1, -1} : ans;
192
182
}
193
183
};
194
184
```
@@ -204,22 +194,19 @@ public:
204
194
* }
205
195
*/
206
196
func nodesBetweenCriticalPoints(head *ListNode) []int {
207
- prev, curr := head, head.Next
208
- first, last := 0, 0
209
- i := 1
210
- ans := []int{math.MaxInt32, 0}
211
- for curr.Next != nil {
212
- if curr.Val < min(prev.Val, curr.Next.Val) || curr.Val > max(prev.Val, curr.Next.Val) {
213
- if last == 0 {
197
+ ans := []int{1 << 30, 0}
198
+ first, last := -1, -1
199
+ for i := 0; head.Next.Next != nil; head, i = head.Next, i+1 {
200
+ a, b, c := head.Val, head.Next.Val, head.Next.Next.Val
201
+ if b < min(a, c) || b > max(a, c) {
202
+ if last == -1 {
214
203
first, last = i, i
215
204
} else {
216
205
ans[0] = min(ans[0], i-last)
217
- ans[1] = i - first
218
206
last = i
207
+ ans[1] = max(ans[1], last-first)
219
208
}
220
209
}
221
- i++
222
- prev, curr = curr, curr.Next
223
210
}
224
211
if first == last {
225
212
return []int{-1, -1}
@@ -244,30 +231,22 @@ func nodesBetweenCriticalPoints(head *ListNode) []int {
244
231
*/
245
232
246
233
function nodesBetweenCriticalPoints(head : ListNode | null ): number [] {
247
- let idx = 1 ;
248
- let pre = head .val ;
249
- head = head .next ;
250
- let nums = [];
251
- while (head .next != null ) {
252
- let val = head .val ,
253
- post = head .next .val ;
254
- if (pre < val && val > post ) {
255
- nums .push (idx );
256
- }
257
- if (pre > val && val < post ) {
258
- nums .push (idx );
234
+ const ans: number [] = [Infinity , 0 ];
235
+ let [first, last] = [- 1 , - 1 ];
236
+ for (let i = 0 ; head .next .next ; head = head .next , ++ i ) {
237
+ const [a, b, c] = [head .val , head .next .val , head .next .next .val ];
238
+ if (b < Math .min (a , c ) || b > Math .max (a , c )) {
239
+ if (last < 0 ) {
240
+ first = i ;
241
+ last = i ;
242
+ } else {
243
+ ans [0 ] = Math .min (ans [0 ], i - last );
244
+ last = i ;
245
+ ans [1 ] = Math .max (ans [1 ], last - first );
246
+ }
259
247
}
260
- pre = val ;
261
- idx ++ ;
262
- head = head .next ;
263
- }
264
- let n = nums .length ;
265
- if (n < 2 ) return [- 1 , - 1 ];
266
- let min = Infinity ;
267
- for (let i = 1 ; i < n ; i ++ ) {
268
- min = Math .min (nums [i ] - nums [i - 1 ], min );
269
248
}
270
- return [ min , nums [ n - 1 ] - nums [ 0 ]] ;
249
+ return first === last ? [ - 1 , - 1 ] : ans ;
271
250
}
272
251
```
273
252
0 commit comments