LeetCode solutions

1094. Matrix Cells in Distance Order

My accepted C++ solution to LeetCode problem 1094, Matrix Cells in Distance Order, running in 23ms.

  • Difficulty: Easy
  • C++
  • Runtime 23ms
  • Memory 19.9MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 23ms, memory 19.9MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
        vector<vector<int>> res;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                res.push_back({i, j});
            }
        }
        sort(res.begin(), res.end(), [&](auto& a, auto& b) {
            return abs(a[0] - rCenter) + abs(a[1] - cCenter) < abs(b[0] - rCenter) + abs(b[1] - cCenter);
        });
        return res;
    }
};

Source