2
* @param {number[][]} mat5
var findPeakGrid = function (mat) {7
* Helper function to find max value in a given list11
function maxValueIdx(array) {13
for (let i = 0; i < array.length; i++) {14
if (array[i] > array[maxIdx]) {21
// Helper pointers for Binary Search23
let right = mat.length - 1;25
while (left < right) {26
const midRowIdx = parseInt((left + right) / 2, 10);28
// Find index with highest value in the mid row29
let colIdx = maxValueIdx(mat[midRowIdx]);31
// If value in the next row is higher - current value is not a pick - check right subgroup32
if (mat[midRowIdx][colIdx] < mat[midRowIdx + 1][colIdx]) {34
// If value in the next row is not higher - current value might be a pick - check left subgroup40
// After completing binary search last value will be pointing to a peak item.42
// Time: Binary Search to find row * Linear search to find max value in a row43
// Time: O(log(N) * M) where N - number of rows, M - number of cells45
return [left, maxValueIdx(mat[left])];