Skip to content

Commit 5e41888

Browse files
committed
1901. Find a Peak Element II: AC
1 parent a1a74d4 commit 5e41888

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1433,3 +1433,4 @@ mod s1897_redistribute_characters_to_make_all_strings_equal;
14331433
mod s1898_maximum_number_of_removable_characters;
14341434
mod s1899_merge_triplets_to_form_target_triplet;
14351435
mod s1900_the_earliest_and_latest_rounds_where_players_compete;
1436+
mod s1901_find_a_peak_element_ii;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* [1901] Find a Peak Element II
3+
*
4+
* A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom.
5+
* Given a 0-indexed m x n matrix mat where no two adjacent cells are equal, find any peak element mat[i][j] and return the length 2 array [i,j].
6+
* You may assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell.
7+
* You must write an algorithm that runs in O(m log(n)) or O(n log(m)) time.
8+
*
9+
* Example 1:
10+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/1.png" style="width: 206px; height: 209px;" />
11+
*
12+
* Input: mat = [[1,4],[3,2]]
13+
* Output: [0,1]
14+
* Explanation: Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.
15+
*
16+
* Example 2:
17+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/07/3.png" style="width: 254px; height: 257px;" />
18+
*
19+
* Input: mat = [[10,20,15],[21,30,14],[7,16,32]]
20+
* Output: [1,1]
21+
* Explanation: Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.
22+
*
23+
*
24+
* Constraints:
25+
*
26+
* m == mat.length
27+
* n == mat[i].length
28+
* 1 <= m, n <= 500
29+
* 1 <= mat[i][j] <= 10^5
30+
* No two adjacent cells are equal.
31+
*
32+
*/
33+
pub struct Solution {}
34+
35+
// problem: https://leetcode.com/problems/find-a-peak-element-ii/
36+
// discuss: https://leetcode.com/problems/find-a-peak-element-ii/discuss/?currentPage=1&orderBy=most_votes&query=
37+
38+
// submission codes start here
39+
40+
impl Solution {
41+
pub fn find_peak_grid(mat: Vec<Vec<i32>>) -> Vec<i32> {
42+
let mut start = 0_usize;
43+
let mut end = mat.len();
44+
45+
let (m, n) = (mat.len(), mat[0].len());
46+
47+
while start < end {
48+
let mid: usize = (start + end) / 2;
49+
let (mut max_idx, mut max_val) = (0_usize, mat[mid][0]);
50+
51+
for i in 0..n {
52+
if mat[mid][i] > max_val {
53+
max_idx = i;
54+
max_val = mat[mid][i];
55+
}
56+
}
57+
58+
if mid > 0 && mat[mid - 1][max_idx] > max_val {
59+
end = mid;
60+
} else if mid < m - 1 && mat[mid + 1][max_idx] > max_val {
61+
start = mid + 1;
62+
} else {
63+
return vec![mid as i32, max_idx as i32];
64+
}
65+
}
66+
67+
vec![0, 0]
68+
}
69+
}
70+
71+
// submission codes end
72+
73+
#[cfg(test)]
74+
mod tests {
75+
use super::*;
76+
77+
#[test]
78+
#[ignore]
79+
fn test_1901_example_1() {
80+
let mat = vec![vec![1, 4], vec![3, 2]];
81+
82+
let result = vec![0, 1];
83+
84+
assert_eq!(Solution::find_peak_grid(mat), result);
85+
}
86+
87+
#[test]
88+
#[ignore]
89+
fn test_1901_example_2() {
90+
let mat = vec![vec![10, 20, 15], vec![21, 30, 14], vec![7, 16, 32]];
91+
92+
let result = vec![1, 1];
93+
94+
assert_eq!(Solution::find_peak_grid(mat), result);
95+
}
96+
}

0 commit comments

Comments
 (0)