LintCode "Find Peak Element II"

时间:2023-03-09 02:26:42
LintCode "Find Peak Element II"

Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind!

class Solution {
public:
/**
* @param A: An integer matrix
* @return: The index of the peak
*/
vector<int> findPeakII(vector<vector<int> > A)
{
int n = A.size();
int m = A[].size(); int i = , j = ;
while(i < m - && j < n - )
{
bool up = A[j - ][i] < A[j][i];
bool down = A[j + ][i] < A[j][i];
bool left = A[j][i - ] < A[j][i];
bool right= A[j][i + ] < A[j][i]; if(up && down && left && right)
{
return {j, i};
}
if(!down && j < n - )
{
j ++;
}
else if(!right && i < m - )
{
i ++;
}
} return {-, -};
}
};