Matrix Cells in Distance Order LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
4
vector<vector<int>> ans;
5
multimap<int, vector<int>> mp;
6
for (int i = 0; i < rows; i++) {
7
for (int j = 0; j < cols; j++) {
8
mp.insert(pair<int, vector<int>>(abs(i - rCenter) + abs(j - cCenter),
9
{i, j})); // map will sort the value based on key(distance)
10
}
11
}
12
for (auto itr = mp.begin(); itr != mp.end(); itr++) {
13
ans.push_back(itr->second);
14
}
15
return ans;
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0