class Solution {
public:
int binaryFind(vector<int> mat, int n, int x) {
int line = mat.size();
int start = 0, end = line;
while(start <= end) {
int mid = (start + end) / 2;
if (mat[mid] == x) {
return mid;
} else if (mat[mid] < x) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
vector<int> ret;
if (n == 0 && m == 0) {
return ret;
}
if (n == 1) {
vector<int> vec;
for (int i = 0; i < mat[0].size(); i++) {
vec.push_back(mat[0][i]);
}
int index = binaryFind(vec, m, x);
ret.push_back(0);
ret.push_back(index);
return ret;
}
if (m == 1) {
vector<int> vec;
for (int i = 0; i < mat.size(); i++) {
vec.push_back(mat[i][0]);
}
int index = binaryFind(vec, n, x);
ret.push_back(index);
ret.push_back(0);
return ret;
}
int columnIndex = 0, lineIndex = m - 1;
while(columnIndex < n && lineIndex >= 0) {
if (x == mat[columnIndex][lineIndex]) {
ret.push_back(columnIndex);
ret.push_back(lineIndex);
return ret;
} else if (x < mat[columnIndex][lineIndex]){
lineIndex--;
} else {
columnIndex++;
}
}
return ret;
// write code here
}
};