Skip to content

Commit 5282635

Browse files
committed
feat: add more
1 parent a61ce5a commit 5282635

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
// https://leetcode.com/problems/count-hills-and-valleys-in-an-array
2-
//
2+
//
33
// 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+
//
55
// 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+
//
77
// Return _the number of hills and valleys in_ `nums`.
8-
//
8+
//
99
// **Example 1:**
10-
//
10+
//
1111
// ```
1212
// **Input:** nums = [2,4,1,1,6,5]
1313
// **Output:** 3
1414
// **Explanation:**
1515
// 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.
1717
// 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.
1818
// 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.
1919
// 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.
2121
// There are 3 hills and valleys so we return 3.
2222
// ```
23-
//
23+
//
2424
// **Example 2:**
25-
//
25+
//
2626
// ```
2727
// **Input:** nums = [6,6,5,5,4,1]
2828
// **Output:** 0
@@ -35,16 +35,34 @@
3535
// At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
3636
// There are 0 hills and valleys so we return 0.
3737
// ```
38-
//
38+
//
3939
// **Constraints:**
40-
//
40+
//
4141
// * `3 <= nums.length <= 100`
4242
// * `1 <= nums[i] <= 100`
4343

4444
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+
}
4553

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+
}
4661
}
62+
return res;
63+
}
4764

4865
#[test]
4966
pub fn t1() {
67+
assert_eq!(count_hill_valley(vec![2, 4, 1, 1, 6, 5]), 3);
5068
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ mod _2164_sort_even_and_odd_indices_independently;
157157
mod _2176_count_equal_and_divisible_pairs_in_an_array;
158158
mod _2185_counting_words_with_a_given_prefix;
159159
mod _2206_divide_array_into_equal_pairs;
160+
mod _2210_count_hills_and_valleys_in_an_array;
160161
mod _2215_find_the_difference_of_two_arrays;
161162
mod _2229_check_if_an_array_is_consecutive;
162163
mod _2235_add_two_integers;

0 commit comments

Comments
 (0)