diff --git a/solution/0800-0899/0845.Longest Mountain in Array/README.md b/solution/0800-0899/0845.Longest Mountain in Array/README.md index e24ced29e2ab4..33e17ea139e1f 100644 --- a/solution/0800-0899/0845.Longest Mountain in Array/README.md +++ b/solution/0800-0899/0845.Longest Mountain in Array/README.md @@ -190,6 +190,31 @@ func longestMountain(arr []int) (ans int) { } ``` +#### TypeScript + +```ts +function longestMountain(arr: number[]): number { + const n = arr.length; + const f: number[] = Array(n).fill(1); + const g: number[] = Array(n).fill(1); + for (let i = 1; i < n; ++i) { + if (arr[i] > arr[i - 1]) { + f[i] = f[i - 1] + 1; + } + } + let ans = 0; + for (let i = n - 2; i >= 0; --i) { + if (arr[i] > arr[i + 1]) { + g[i] = g[i + 1] + 1; + if (f[i] > 1) { + ans = Math.max(ans, f[i] + g[i] - 1); + } + } + } + return ans; +} +``` + @@ -200,7 +225,7 @@ func longestMountain(arr []int) (ans int) { 我们可以枚举山脉的左侧山脚,然后向右寻找山脉的右侧山脚。我们可以使用两个指针 $l$ 和 $r$,其中 $l$ 表示左侧山脚的下标,$r$ 表示右侧山脚的下标,初始时 $l=0$,$r=0$,然后我们向右移动 $r$,找到山顶的位置,此时判断 $r$ 是否满足 $r + 1 \lt n$ 并且 $arr[r] \gt arr[r + 1]$,如果满足,我们向右继续移动 $r$,直到找到右侧山脚的位置,此时山脉的长度为 $r - l + 1$,我们更新答案,然后将 $l$ 的值更新为 $r$,继续寻找下一个山脉。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $arr$ 的长度。空间复杂度 $O(1)$。 @@ -308,6 +333,32 @@ func longestMountain(arr []int) (ans int) { } ``` +#### TypeScript + +```ts +function longestMountain(arr: number[]): number { + const n = arr.length; + let ans = 0; + for (let l = 0, r = 0; l + 2 < n; l = r) { + r = l + 1; + if (arr[l] < arr[r]) { + while (r + 1 < n && arr[r] < arr[r + 1]) { + ++r; + } + if (r + 1 < n && arr[r] > arr[r + 1]) { + while (r + 1 < n && arr[r] > arr[r + 1]) { + ++r; + } + ans = Math.max(ans, r - l + 1); + } else { + ++r; + } + } + } + return ans; +} +``` + diff --git a/solution/0800-0899/0845.Longest Mountain in Array/README_EN.md b/solution/0800-0899/0845.Longest Mountain in Array/README_EN.md index ad2003fc8e076..8846498e5364c 100644 --- a/solution/0800-0899/0845.Longest Mountain in Array/README_EN.md +++ b/solution/0800-0899/0845.Longest Mountain in Array/README_EN.md @@ -72,7 +72,11 @@ tags: -### Solution 1 +## Solution 1: Preprocessing + Enumeration + +We define two arrays $f$ and $g$, where $f[i]$ represents the length of the longest increasing subsequence ending at $arr[i]$, and $g[i]$ represents the length of the longest decreasing subsequence starting at $arr[i]$. Then for each index $i$, if $f[i] \gt 1$ and $g[i] \gt 1$, the length of the mountain with $arr[i]$ as the peak is $f[i] + g[i] - 1$. We only need to enumerate all $i$ and find the maximum value. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $arr$. @@ -183,13 +187,42 @@ func longestMountain(arr []int) (ans int) { } ``` +#### TypeScript + +```ts +function longestMountain(arr: number[]): number { + const n = arr.length; + const f: number[] = Array(n).fill(1); + const g: number[] = Array(n).fill(1); + for (let i = 1; i < n; ++i) { + if (arr[i] > arr[i - 1]) { + f[i] = f[i - 1] + 1; + } + } + let ans = 0; + for (let i = n - 2; i >= 0; --i) { + if (arr[i] > arr[i + 1]) { + g[i] = g[i + 1] + 1; + if (f[i] > 1) { + ans = Math.max(ans, f[i] + g[i] - 1); + } + } + } + return ans; +} +``` + -### Solution 2 +## Solution 2: One Pass (Enumerate Left Base of Mountain) + +We can enumerate the left base of the mountain and then search to the right for the right base of the mountain. We can use two pointers $l$ and $r$, where $l$ represents the index of the left base and $r$ represents the index of the right base. Initially, $l=0$ and $r=0$. Then we move $r$ to the right to find the position of the peak. At this point, we check if $r$ satisfies $r + 1 \lt n$ and $arr[r] \gt arr[r + 1]$. If so, we continue moving $r$ to the right until we find the position of the right base. At this point, the length of the mountain is $r - l + 1$. We update the answer and then update the value of $l$ to $r$, continuing to search for the next mountain. + +The time complexity is $O(n)$, where $n$ is the length of the array $arr$. The space complexity is $O(1)$. @@ -297,6 +330,32 @@ func longestMountain(arr []int) (ans int) { } ``` +#### TypeScript + +```ts +function longestMountain(arr: number[]): number { + const n = arr.length; + let ans = 0; + for (let l = 0, r = 0; l + 2 < n; l = r) { + r = l + 1; + if (arr[l] < arr[r]) { + while (r + 1 < n && arr[r] < arr[r + 1]) { + ++r; + } + if (r + 1 < n && arr[r] > arr[r + 1]) { + while (r + 1 < n && arr[r] > arr[r + 1]) { + ++r; + } + ans = Math.max(ans, r - l + 1); + } else { + ++r; + } + } + } + return ans; +} +``` + diff --git a/solution/0800-0899/0845.Longest Mountain in Array/Solution.ts b/solution/0800-0899/0845.Longest Mountain in Array/Solution.ts new file mode 100644 index 0000000000000..d64385011621a --- /dev/null +++ b/solution/0800-0899/0845.Longest Mountain in Array/Solution.ts @@ -0,0 +1,20 @@ +function longestMountain(arr: number[]): number { + const n = arr.length; + const f: number[] = Array(n).fill(1); + const g: number[] = Array(n).fill(1); + for (let i = 1; i < n; ++i) { + if (arr[i] > arr[i - 1]) { + f[i] = f[i - 1] + 1; + } + } + let ans = 0; + for (let i = n - 2; i >= 0; --i) { + if (arr[i] > arr[i + 1]) { + g[i] = g[i + 1] + 1; + if (f[i] > 1) { + ans = Math.max(ans, f[i] + g[i] - 1); + } + } + } + return ans; +} diff --git a/solution/0800-0899/0845.Longest Mountain in Array/Solution2.ts b/solution/0800-0899/0845.Longest Mountain in Array/Solution2.ts new file mode 100644 index 0000000000000..789a17e728aa4 --- /dev/null +++ b/solution/0800-0899/0845.Longest Mountain in Array/Solution2.ts @@ -0,0 +1,21 @@ +function longestMountain(arr: number[]): number { + const n = arr.length; + let ans = 0; + for (let l = 0, r = 0; l + 2 < n; l = r) { + r = l + 1; + if (arr[l] < arr[r]) { + while (r + 1 < n && arr[r] < arr[r + 1]) { + ++r; + } + if (r + 1 < n && arr[r] > arr[r + 1]) { + while (r + 1 < n && arr[r] > arr[r + 1]) { + ++r; + } + ans = Math.max(ans, r - l + 1); + } else { + ++r; + } + } + } + return ans; +}