File tree Expand file tree Collapse file tree 4 files changed +65
-37
lines changed
solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray Expand file tree Collapse file tree 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 {
199199
200200``` ts
201201function longestMonotonicSubarray(nums : number []): number {
202+ const n = nums .length ;
202203 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 );
209209 }
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);
216226 }
227+
217228 return ans;
218229}
219230```
Original file line number Diff line number Diff line change @@ -195,21 +195,32 @@ func longestMonotonicSubarray(nums []int) int {
195195
196196``` ts
197197function longestMonotonicSubarray(nums : number []): number {
198+ const n = nums .length ;
198199 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 );
205205 }
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);
212222 }
223+
213224 return ans;
214225}
215226```
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 11function longestMonotonicSubarray ( nums : number [ ] ) : number {
2+ const n = nums . length ;
23 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 ) ;
169 }
10+
1711 return ans ;
1812}
You can’t perform that action at this time.
0 commit comments