|
1 | 1 | // https://leetcode.com/problems/count-hills-and-valleys-in-an-array
|
2 |
| -// |
| 2 | +// |
3 | 3 | // You are given a **0-indexed** integer array `nums`. An index `i` is part of a **hill** in `nums` if the closest non-equal neighbors of `i` are smaller than `nums[i]`. Similarly, an index `i` is part of a **valley** in `nums` if the closest non-equal neighbors of `i` are larger than `nums[i]`. Adjacent indices `i` and `j` are part of the **same** hill or valley if `nums[i] == nums[j]`.
|
4 |
| -// |
| 4 | +// |
5 | 5 | // Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on **both** the left and right of the index.
|
6 |
| -// |
| 6 | +// |
7 | 7 | // Return _the number of hills and valleys in_ `nums`.
|
8 |
| -// |
| 8 | +// |
9 | 9 | // **Example 1:**
|
10 |
| -// |
| 10 | +// |
11 | 11 | // ```
|
12 | 12 | // **Input:** nums = [2,4,1,1,6,5]
|
13 | 13 | // **Output:** 3
|
14 | 14 | // **Explanation:**
|
15 | 15 | // At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
|
16 |
| -// At index 1: The closest non-equal neighbors of 4 are 2 and 1\. Since 4 > 2 and 4 > 1, index 1 is a hill. |
| 16 | +// At index 1: The closest non-equal neighbors of 4 are 2 and 1\. Since 4 > 2 and 4 > 1, index 1 is a hill. |
17 | 17 | // At index 2: The closest non-equal neighbors of 1 are 4 and 6\. Since 1 < 4 and 1 < 6, index 2 is a valley.
|
18 | 18 | // At index 3: The closest non-equal neighbors of 1 are 4 and 6\. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
|
19 | 19 | // At index 4: The closest non-equal neighbors of 6 are 1 and 5\. Since 6 > 1 and 6 > 5, index 4 is a hill.
|
20 |
| -// At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. |
| 20 | +// At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. |
21 | 21 | // There are 3 hills and valleys so we return 3.
|
22 | 22 | // ```
|
23 |
| -// |
| 23 | +// |
24 | 24 | // **Example 2:**
|
25 |
| -// |
| 25 | +// |
26 | 26 | // ```
|
27 | 27 | // **Input:** nums = [6,6,5,5,4,1]
|
28 | 28 | // **Output:** 0
|
|
35 | 35 | // At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
|
36 | 36 | // There are 0 hills and valleys so we return 0.
|
37 | 37 | // ```
|
38 |
| -// |
| 38 | +// |
39 | 39 | // **Constraints:**
|
40 |
| -// |
| 40 | +// |
41 | 41 | // * `3 <= nums.length <= 100`
|
42 | 42 | // * `1 <= nums[i] <= 100`
|
43 | 43 |
|
44 | 44 | pub fn count_hill_valley(nums: Vec<i32>) -> i32 {
|
| 45 | + let mut res = 0; |
| 46 | + let mut new_num = Vec::new(); |
| 47 | + new_num.push(nums[0]); |
| 48 | + for i in 1..nums.len() { |
| 49 | + if nums[i] != nums[i - 1] { |
| 50 | + new_num.push(nums[i]); |
| 51 | + } |
| 52 | + } |
45 | 53 |
|
| 54 | + for i in 1..new_num.len() - 1 { |
| 55 | + if new_num[i] > new_num[i - 1] && new_num[i] > new_num[i + 1] { |
| 56 | + res += 1; |
| 57 | + } |
| 58 | + if new_num[i] < new_num[i - 1] && new_num[i] < new_num[i + 1] { |
| 59 | + res += 1; |
| 60 | + } |
46 | 61 | }
|
| 62 | + return res; |
| 63 | +} |
47 | 64 |
|
48 | 65 | #[test]
|
49 | 66 | pub fn t1() {
|
| 67 | + assert_eq!(count_hill_valley(vec![2, 4, 1, 1, 6, 5]), 3); |
50 | 68 | }
|
0 commit comments