We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3cff0f2 commit 64c9108Copy full SHA for 64c9108
1901-find-a-peak-element-ii.js
@@ -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