845. Longest Mountain in Array Medium
1/**
2 * [845] Longest Mountain in Array
3 *
4 * You may recall that an array arr is a mountain array if and only if:
5 *
6 * arr.length >= 3
7 * There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
8 *
9 * arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
10 * arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
11 *
12 *
13 *
14 * Given an integer array arr, return the length of the longest subarray, which is a mountain. Return 0 if there is no mountain subarray.
15 *
16 * Example 1:
17 *
18 * Input: arr = [2,1,4,7,3,2,5]
19 * Output: 5
20 * Explanation: The largest mountain is [1,4,7,3,2] which has length 5.
21 *
22 * Example 2:
23 *
24 * Input: arr = [2,2,2]
25 * Output: 0
26 * Explanation: There is no mountain.
27 *
28 *
29 * Constraints:
30 *
31 * 1 <= arr.length <= 10^4
32 * 0 <= arr[i] <= 10^4
33 *
34 *
35 * Follow up:
36 *
37 * Can you solve it using only one pass?
38 * Can you solve it in O(1) space?
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/longest-mountain-in-array/
44// discuss: https://leetcode.com/problems/longest-mountain-in-array/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn longest_mountain(arr: Vec<i32>) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_845() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.