File tree 4 files changed +65
-37
lines changed
solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray
4 files changed +65
-37
lines changed Original file line number Diff line number Diff line change @@ -199,21 +199,32 @@ func longestMonotonicSubarray(nums []int) int {
199
199
200
200
``` ts
201
201
function longestMonotonicSubarray(nums : number []): number {
202
+ const n = nums .length ;
202
203
let ans = 1 ;
203
- for (let i = 1 , t = 1 ; i < nums .length ; ++ i ) {
204
- if (nums [i - 1 ] < nums [i ]) {
205
- ans = Math .max (ans , ++ t );
206
- } else {
207
- t = 1 ;
208
- }
204
+
205
+ for (let i = 1 , t1 = 1 , t2 = 1 ; i < n ; i ++ ) {
206
+ t1 = nums [i ] > nums [i - 1 ] ? t1 + 1 : 1 ;
207
+ t2 = nums [i ] < nums [i - 1 ] ? t2 + 1 : 1 ;
208
+ ans = Math .max (ans , t1 , t2 );
209
209
}
210
- for (let i = 1 , t = 1 ; i < nums .length ; ++ i ) {
211
- if (nums [i - 1 ] > nums [i ]) {
212
- ans = Math .max (ans , ++ t );
213
- } else {
214
- t = 1 ;
215
- }
210
+
211
+ return ans ;
212
+ }
213
+ ```
214
+
215
+ #### JavaScript
216
+
217
+ ``` js
218
+ function longestMonotonicSubarray (nums ) {
219
+ const n = nums .length ;
220
+ let ans = 1 ;
221
+
222
+ for (let i = 1 , t1 = 1 , t2 = 1 ; i < n; i++ ) {
223
+ t1 = nums[i] > nums[i - 1 ] ? t1 + 1 : 1 ;
224
+ t2 = nums[i] < nums[i - 1 ] ? t2 + 1 : 1 ;
225
+ ans = Math .max (ans, t1, t2);
216
226
}
227
+
217
228
return ans;
218
229
}
219
230
```
Original file line number Diff line number Diff line change @@ -195,21 +195,32 @@ func longestMonotonicSubarray(nums []int) int {
195
195
196
196
``` ts
197
197
function longestMonotonicSubarray(nums : number []): number {
198
+ const n = nums .length ;
198
199
let ans = 1 ;
199
- for (let i = 1 , t = 1 ; i < nums .length ; ++ i ) {
200
- if (nums [i - 1 ] < nums [i ]) {
201
- ans = Math .max (ans , ++ t );
202
- } else {
203
- t = 1 ;
204
- }
200
+
201
+ for (let i = 1 , t1 = 1 , t2 = 1 ; i < n ; i ++ ) {
202
+ t1 = nums [i ] > nums [i - 1 ] ? t1 + 1 : 1 ;
203
+ t2 = nums [i ] < nums [i - 1 ] ? t2 + 1 : 1 ;
204
+ ans = Math .max (ans , t1 , t2 );
205
205
}
206
- for (let i = 1 , t = 1 ; i < nums .length ; ++ i ) {
207
- if (nums [i - 1 ] > nums [i ]) {
208
- ans = Math .max (ans , ++ t );
209
- } else {
210
- t = 1 ;
211
- }
206
+
207
+ return ans ;
208
+ }
209
+ ```
210
+
211
+ #### JavaScript
212
+
213
+ ``` js
214
+ function longestMonotonicSubarray (nums ) {
215
+ const n = nums .length ;
216
+ let ans = 1 ;
217
+
218
+ for (let i = 1 , t1 = 1 , t2 = 1 ; i < n; i++ ) {
219
+ t1 = nums[i] > nums[i - 1 ] ? t1 + 1 : 1 ;
220
+ t2 = nums[i] < nums[i - 1 ] ? t2 + 1 : 1 ;
221
+ ans = Math .max (ans, t1, t2);
212
222
}
223
+
213
224
return ans;
214
225
}
215
226
```
Original file line number Diff line number Diff line change
1
+ function longestMonotonicSubarray ( nums ) {
2
+ const n = nums . length ;
3
+ let ans = 1 ;
4
+
5
+ for ( let i = 1 , t1 = 1 , t2 = 1 ; i < n ; i ++ ) {
6
+ t1 = nums [ i ] > nums [ i - 1 ] ? t1 + 1 : 1 ;
7
+ t2 = nums [ i ] < nums [ i - 1 ] ? t2 + 1 : 1 ;
8
+ ans = Math . max ( ans , t1 , t2 ) ;
9
+ }
10
+
11
+ return ans ;
12
+ }
Original file line number Diff line number Diff line change 1
1
function longestMonotonicSubarray ( nums : number [ ] ) : number {
2
+ const n = nums . length ;
2
3
let ans = 1 ;
3
- for ( let i = 1 , t = 1 ; i < nums . length ; ++ i ) {
4
- if ( nums [ i - 1 ] < nums [ i ] ) {
5
- ans = Math . max ( ans , ++ t ) ;
6
- } else {
7
- t = 1 ;
8
- }
9
- }
10
- for ( let i = 1 , t = 1 ; i < nums . length ; ++ i ) {
11
- if ( nums [ i - 1 ] > nums [ i ] ) {
12
- ans = Math . max ( ans , ++ t ) ;
13
- } else {
14
- t = 1 ;
15
- }
4
+
5
+ for ( let i = 1 , t1 = 1 , t2 = 1 ; i < n ; i ++ ) {
6
+ t1 = nums [ i ] > nums [ i - 1 ] ? t1 + 1 : 1 ;
7
+ t2 = nums [ i ] < nums [ i - 1 ] ? t2 + 1 : 1 ;
8
+ ans = Math . max ( ans , t1 , t2 ) ;
16
9
}
10
+
17
11
return ans ;
18
12
}
You can’t perform that action at this time.
0 commit comments