Skip to content

Commit 0556dc4

Browse files
committed
一刷845
1 parent c805636 commit 0556dc4

6 files changed

Lines changed: 117 additions & 41 deletions

File tree

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5940,13 +5940,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
59405940
|Easy
59415941
|
59425942

5943-
//|{counter:codes}
5944-
//|{leetcode_base_url}/longest-mountain-in-array/[845. Longest Mountain in Array^]
5945-
//|{source_base_url}/_0845_LongestMountainInArray.java[Java]
5946-
//|{doc_base_url}/0845-longest-mountain-in-array.adoc[题解]
5947-
//|Medium
5948-
//|
5949-
//
5943+
|{counter:codes}
5944+
|{leetcode_base_url}/longest-mountain-in-array/[845. Longest Mountain in Array^]
5945+
|{source_base_url}/_0845_LongestMountainInArray.java[Java]
5946+
|{doc_base_url}/0845-longest-mountain-in-array.adoc[题解]
5947+
|Medium
5948+
|
5949+
59505950
//|{counter:codes}
59515951
//|{leetcode_base_url}/hand-of-straights/[846. Hand of Straights^]
59525952
//|{source_base_url}/_0846_HandOfStraights.java[Java]
Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,76 @@
11
[#0845-longest-mountain-in-array]
2-
= 845. Longest Mountain in Array
2+
= 845. 数组中的最长山脉
33

4-
{leetcode}/problems/longest-mountain-in-array/[LeetCode - Longest Mountain in Array^]
4+
https://leetcode.cn/problems/longest-mountain-in-array/[LeetCode - 845. 数组中的最长山脉^]
55

6-
Let's call any (contiguous) subarray B (of A) a _mountain_ if the following properties hold:
6+
把符合下列属性的数组 `arr` 称为 *山脉数组*
77

8+
* `arr.length >= 3`
9+
* 存在下标 `i``0 < i < arr.length - 1`),满足
10+
** `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
11+
** `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
812
9-
* `B.length >= 3`
10-
* There exists some `0 < i < B.length - 1` such that `B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]`
13+
给出一个整数数组 `arr`,返回最长山脉子数组的长度。如果不存在山脉子数组,返回 `0`
1114

15+
*示例 1:*
1216

13-
(Note that B could be any subarray of A, including the entire array A.)
17+
....
18+
输入:arr = [2,1,4,7,3,2,5]
19+
输出:5
20+
解释:最长的山脉子数组是 [1,4,7,3,2],长度为 5。
21+
....
1422

15-
Given an array `A` of integers, return the length of the longest _mountain_.
23+
*示例 2:*
1624

17-
Return `0` if there is no mountain.
25+
....
26+
输入:arr = [2,2,2]
27+
输出:0
28+
解释:不存在山脉子数组。
29+
....
1830

19-
*Example 1:*
31+
*提示:*
2032

21-
[subs="verbatim,quotes,macros"]
22-
----
23-
*Input:* [2,1,4,7,3,2,5]
24-
*Output:* 5
25-
*Explanation:* The largest mountain is [1,4,7,3,2] which has length 5.
26-
----
27-
28-
*Example 2:*
29-
30-
[subs="verbatim,quotes,macros"]
31-
----
32-
*Input:* [2,2,2]
33-
*Output:* 0
34-
*Explanation:* There is no mountain.
35-
----
33+
* `1 \<= arr.length \<= 10^4^`
34+
* `0 \<= arr[i] \<= 10^4^`
3635
37-
*Note:*
36+
*进阶:*
3837

38+
* 你可以仅用一趟扫描解决此问题吗?
39+
* 你可以用 stem:[O(1)] 空间解决此问题吗?
3940
40-
* `0 <= A.length <= 10000`
41-
* `0 <= A[i] <= 10000`
4241
42+
== 思路分析
4343

44-
*Follow up:*
45-
46-
47-
* Can you solve it using only one pass?
48-
* Can you solve it in `O(1)` space?
49-
44+
image::images/0845-10.png[{image_attr}]
5045

46+
从前向后遍历,统计递增长度,拐点后,统计递降长度。如果遇到相等或者递降拐点成递增,则从头开始。
5147

48+
TIP: 官方题解中,分别从前和从后统计递增,最后再遍历求和的思路也很好。
5249

5350
[[src-0845]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
5456
[{java_src_attr}]
5557
----
5658
include::{sourcedir}/_0845_LongestMountainInArray.java[tag=answer]
5759
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_0845_LongestMountainInArray_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
5874

75+
. https://leetcode.cn/problems/longest-mountain-in-array/solutions/459406/shu-zu-zhong-de-zui-chang-shan-mai-by-leetcode-sol/[845. 数组中的最长山脉 - 官方题解^]
76+
. https://leetcode.cn/problems/longest-mountain-in-array/solutions/3804958/ling-shen-fen-zu-xun-huan-mo-ban-ti-by-e-phzz/[845. 数组中的最长山脉 - 灵神分组循环模板题^]

docs/images/0845-10.png

5.47 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ include::0842-split-array-into-fibonacci-sequence.adoc[leveloffset=+1]
17821782

17831783
include::0844-backspace-string-compare.adoc[leveloffset=+1]
17841784

1785-
// include::0845-longest-mountain-in-array.adoc[leveloffset=+1]
1785+
include::0845-longest-mountain-in-array.adoc[leveloffset=+1]
17861786

17871787
// include::0846-hand-of-straights.adoc[leveloffset=+1]
17881788

logbook/202601.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,12 @@ endif::[]
11061106
|{doc_base_url}/0844-backspace-string-compare.adoc[题解]
11071107
|✅ 双指针。从后向前比较:如果双方都是字母,则直接比较;否则将不是字母的下标向前推进到字母或者结束为止。
11081108

1109+
|{counter:codes}
1110+
|{leetcode_base_url}/longest-mountain-in-array/[845. 数组中的最长山脉^]
1111+
|{doc_base_url}/0845-longest-mountain-in-array.adoc[题解]
1112+
|✅ 双指针。从前向后遍历,统计递增长度,拐点后,统计递降长度。如果遇到相等或者递降拐点成递增,则从头开始。这个过程,即可统计长度,也可以记录两端山脚的索引。
1113+
1114+
11091115
|===
11101116

11111117

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0845_LongestMountainInArray {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2026-08-02 20:51:40
9+
*/
10+
public int longestMountain(int[] arr) {
11+
int result = 0;
12+
boolean isUp = true;
13+
int len = 1;
14+
for (int i = 1; i < arr.length; i++) {
15+
int pre = arr[i - 1];
16+
int cur = arr[i];
17+
// 如果持平,就从头开始
18+
if (pre == cur) {
19+
len = 1;
20+
isUp = true;
21+
continue;
22+
}
23+
// 递增,则长度增长
24+
if (isUp && pre < cur) {
25+
len++;
26+
} else if (len > 1 && pre > cur) {
27+
// 拐点,则长度也要增产
28+
len++;
29+
isUp = false;
30+
}
31+
// 只在下降过程中,统计最长长度
32+
if (!isUp && len >= 3 && len > result) {
33+
result = len;
34+
}
35+
// 递降过程中,出现了拐点,则从头开始
36+
if (!isUp && pre < cur) {
37+
len = 2;
38+
isUp = true;
39+
}
40+
}
41+
return result;
42+
}
43+
// end::answer[]
44+
45+
static void main() {
46+
new _0845_LongestMountainInArray()
47+
.longestMountain(new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0});
48+
// .longestMountain(new int[]{2, 1, 3, 1, 0, 1, 3, 1, 0, 1, 2, 0, 0, 0, 2, 1, 0, 3, 0, 3, 1, 0, 3, 2, 1, 1, 1, 0, 2, 0, 0, 1, 0, 2, 2, 0, 3, 1, 2, 2, 0, 3, 2, 2, 3, 3, 3, 1, 3, 0, 0, 2, 3, 0, 0, 1, 0, 1, 2, 3, 1, 0, 1, 0, 0, 3, 0, 3, 2, 0, 2, 1, 1, 2, 0, 0, 2, 3, 1, 1, 1, 3, 1, 0, 1, 3, 2, 0, 0, 3, 0, 2, 0, 3, 2, 2, 0, 0, 3, 0, 3, 1, 0, 0, 2, 2, 1, 2, 1, 1, 3, 0, 1, 1, 3, 1, 2, 0, 3, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0, 1, 0, 1, 2, 2, 1, 3, 3, 0,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