Skip to content

Commit 64c9108

Browse files
authored
Create 1901-find-a-peak-element-ii.js
1 parent 3cff0f2 commit 64c9108

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1901-find-a-peak-element-ii.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number[]}
4+
*/
5+
const findPeakGrid = function(mat) {
6+
let lowCol = 0;
7+
let highCol = mat[0].length - 1;
8+
9+
while(lowCol <= highCol) {
10+
let midCol = lowCol + ~~((highCol - lowCol) / 2);
11+
let maxRow = 0;
12+
for(let i = 0; i < mat.length; i++) {
13+
maxRow = mat[i][midCol] > mat[maxRow][midCol] ? i : maxRow;
14+
}
15+
16+
let isLeftElementBig = midCol - 1 >= lowCol && mat[maxRow][midCol - 1] > mat[maxRow][midCol];
17+
let isRightElementBig = midCol + 1 <= highCol && mat[maxRow][midCol + 1] > mat[maxRow][midCol];
18+
19+
if(!isLeftElementBig && !isRightElementBig) {
20+
return [maxRow, midCol];
21+
} else if(isRightElementBig) {
22+
lowCol = midCol + 1;
23+
} else {
24+
highCol = midCol - 1;
25+
}
26+
}
27+
return null;
28+
};

0 commit comments

Comments
 (0)