Skip to content

Commit 3523c84

Browse files
committed
Add problem 852: Peak Index in a Mountain Array
1 parent 9df9a91 commit 3523c84

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ pub mod problem_0845_longest_mountain_in_array;
694694
pub mod problem_0846_hand_of_straights;
695695
pub mod problem_0848_shifting_letters;
696696
pub mod problem_0849_maximize_distance_to_closest_person;
697+
pub mod problem_0852_peak_index_in_a_mountain_array;
697698
pub mod problem_0867_transpose_matrix;
698699
pub mod problem_1143_longest_common_subsequence;
699700
pub mod problem_1192_critical_connections_in_a_network;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn peak_index_in_mountain_array(arr: Vec<i32>) -> i32 {
7+
let mut left = 1;
8+
let mut right = arr.len() - 2;
9+
10+
while left < right {
11+
let middle = (left + right) / 2;
12+
13+
if arr[middle] < arr[middle + 1] {
14+
left = middle + 1;
15+
} else {
16+
right = middle;
17+
}
18+
}
19+
20+
left as _
21+
}
22+
}
23+
24+
// ------------------------------------------------------ snip ------------------------------------------------------ //
25+
26+
impl super::Solution for Solution {
27+
fn peak_index_in_mountain_array(arr: Vec<i32>) -> i32 {
28+
Self::peak_index_in_mountain_array(arr)
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
#[test]
35+
fn test_solution() {
36+
super::super::tests::run::<super::Solution>();
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn peak_index_in_mountain_array(arr: Vec<i32>) -> i32 {
7+
arr.iter().zip(&arr[1..]).position(|(lhs, rhs)| lhs > rhs).unwrap() as _
8+
}
9+
}
10+
11+
// ------------------------------------------------------ snip ------------------------------------------------------ //
12+
13+
impl super::Solution for Solution {
14+
fn peak_index_in_mountain_array(arr: Vec<i32>) -> i32 {
15+
Self::peak_index_in_mountain_array(arr)
16+
}
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
#[test]
22+
fn test_solution() {
23+
super::super::tests::run::<super::Solution>();
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn peak_index_in_mountain_array(arr: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(&[0, 1, 0] as &[_], 1),
14+
(&[0, 2, 1, 0], 1),
15+
(&[0, 10, 5, 2], 1),
16+
(&[3, 4, 5, 1], 2),
17+
(&[24, 69, 100, 99, 79, 78, 67, 36, 26, 19], 2),
18+
];
19+
20+
for (arr, expected) in test_cases {
21+
assert_eq!(S::peak_index_in_mountain_array(arr.to_vec()), expected);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)