diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md index 6cfc08fd14814..df669b2e72d6f 100644 --- a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md @@ -199,21 +199,32 @@ func longestMonotonicSubarray(nums []int) int { ```ts function longestMonotonicSubarray(nums: number[]): number { + const n = nums.length; let ans = 1; - for (let i = 1, t = 1; i < nums.length; ++i) { - if (nums[i - 1] < nums[i]) { - ans = Math.max(ans, ++t); - } else { - t = 1; - } + + for (let i = 1, t1 = 1, t2 = 1; i < n; i++) { + t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1; + t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1; + ans = Math.max(ans, t1, t2); } - for (let i = 1, t = 1; i < nums.length; ++i) { - if (nums[i - 1] > nums[i]) { - ans = Math.max(ans, ++t); - } else { - t = 1; - } + + return ans; +} +``` + +#### JavaScript + +```js +function longestMonotonicSubarray(nums) { + const n = nums.length; + let ans = 1; + + for (let i = 1, t1 = 1, t2 = 1; i < n; i++) { + t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1; + t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1; + ans = Math.max(ans, t1, t2); } + return ans; } ``` diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md index a8a0aba72d547..154ee4f0687e2 100644 --- a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md @@ -195,21 +195,32 @@ func longestMonotonicSubarray(nums []int) int { ```ts function longestMonotonicSubarray(nums: number[]): number { + const n = nums.length; let ans = 1; - for (let i = 1, t = 1; i < nums.length; ++i) { - if (nums[i - 1] < nums[i]) { - ans = Math.max(ans, ++t); - } else { - t = 1; - } + + for (let i = 1, t1 = 1, t2 = 1; i < n; i++) { + t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1; + t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1; + ans = Math.max(ans, t1, t2); } - for (let i = 1, t = 1; i < nums.length; ++i) { - if (nums[i - 1] > nums[i]) { - ans = Math.max(ans, ++t); - } else { - t = 1; - } + + return ans; +} +``` + +#### JavaScript + +```js +function longestMonotonicSubarray(nums) { + const n = nums.length; + let ans = 1; + + for (let i = 1, t1 = 1, t2 = 1; i < n; i++) { + t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1; + t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1; + ans = Math.max(ans, t1, t2); } + return ans; } ``` diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.js b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.js new file mode 100644 index 0000000000000..e4b3cf59d0bf7 --- /dev/null +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.js @@ -0,0 +1,12 @@ +function longestMonotonicSubarray(nums) { + const n = nums.length; + let ans = 1; + + for (let i = 1, t1 = 1, t2 = 1; i < n; i++) { + t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1; + t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1; + ans = Math.max(ans, t1, t2); + } + + return ans; +} diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts index d65a189f524ab..e0faacf56a19e 100644 --- a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts @@ -1,18 +1,12 @@ function longestMonotonicSubarray(nums: number[]): number { + const n = nums.length; let ans = 1; - for (let i = 1, t = 1; i < nums.length; ++i) { - if (nums[i - 1] < nums[i]) { - ans = Math.max(ans, ++t); - } else { - t = 1; - } - } - for (let i = 1, t = 1; i < nums.length; ++i) { - if (nums[i - 1] > nums[i]) { - ans = Math.max(ans, ++t); - } else { - t = 1; - } + + for (let i = 1, t1 = 1, t2 = 1; i < n; i++) { + t1 = nums[i] > nums[i - 1] ? t1 + 1 : 1; + t2 = nums[i] < nums[i - 1] ? t2 + 1 : 1; + ans = Math.max(ans, t1, t2); } + return ans; }