Matrix Cells in Distance Order LeetCode solution in C++
1class Solution {2public:3vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {4vector<vector<int>> ans;5multimap<int, vector<int>> mp;6for (int i = 0; i < rows; i++) {7for (int j = 0; j < cols; j++) {8mp.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}12for (auto itr = mp.begin(); itr != mp.end(); itr++) {13ans.push_back(itr->second);14}15return ans;16}17};