Skip to content

Commit eb1beaf

Browse files
committed
Added solution of problem 162
1 parent a939e8b commit eb1beaf

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub mod s0151_reverse_words_in_a_string;
9090
pub mod s0152_maximum_product_subarray;
9191
pub mod s0153_find_minimum_in_rotated_sorted_array;
9292
pub mod s0155_min_stack;
93+
pub mod s0162_find_peak_element;
9394
pub mod s0231_power_of_two;
9495
pub mod s0232_implement_queue_using_stacks;
9596
pub mod s0234_palindrome_linked_list;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub fn find_peak_element(nums: Vec<i32>) -> i32 {
2+
if nums.len() == 1 || nums[0] > nums[1] {
3+
return 0;
4+
}
5+
6+
if nums[nums.len() - 1] > nums[nums.len() - 2] {
7+
return nums.len() as i32 - 1;
8+
}
9+
10+
for i in 1..nums.len() - 1 {
11+
if nums[i - 1] < nums[i] && nums[i] > nums[i + 1] {
12+
return i as i32;
13+
}
14+
}
15+
16+
0
17+
}
18+
19+
#[cfg(test)]
20+
mod test {
21+
use super::*;
22+
23+
#[test]
24+
fn test_case_1() {
25+
assert_eq!(find_peak_element(vec![1, 2, 3, 1]), 2);
26+
}
27+
28+
#[test]
29+
fn test_case_2() {
30+
assert_eq!(find_peak_element(vec![1, 2, 1, 3, 5, 6, 4]), 1);
31+
}
32+
}

0 commit comments

Comments
 (0)