1901.cpp (750B)
1 class Solution { 2 public: 3 vector<int> findPeakGrid(const vector<vector<int>> &mat) const { 4 int beg = 0, end = mat[0].size() - 1; 5 6 while (beg <= end) { 7 int maxi = 0, mid = beg + (end - beg) / 2; 8 9 for (int i = 0; i < mat.size(); i++) { 10 maxi = mat[i][mid] >= mat[maxi][mid] ? i : maxi; 11 } 12 13 bool isLeft = mid - 1 >= beg && mat[maxi][mid - 1] > mat[maxi][mid]; 14 bool isRight = mid + 1 <= end && mat[maxi][mid + 1] > mat[maxi][mid]; 15 16 if (!isLeft && !isRight) 17 return {maxi, mid}; 18 else if (isRight) 19 beg = mid + 1; 20 else 21 end = mid - 1; 22 } 23 24 return {-1, -1}; 25 } 26 };