File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments