Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ed11d9

Browse files
authored
feat: add solutions to lc problem: No.3105 (doocs#4018)
1 parent b13b1d1 commit 7ed11d9

File tree

4 files changed

+65
-37
lines changed

4 files changed

+65
-37
lines changed

‎solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,32 @@ func longestMonotonicSubarray(nums []int) int {
199199

200200
```ts
201201
function 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
```

‎solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,32 @@ func longestMonotonicSubarray(nums []int) int {
195195

196196
```ts
197197
function 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
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
function 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
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /