Formatted question description: https://leetcode.ca/all/1329.html

1329. Sort the Matrix Diagonally (Medium)

A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2].

Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.

 

Example 1:

Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]

 

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • 1 <= mat[i][j] <= 100

Related Topics:
Array, Sort

Solution 1.

// OJ: https://leetcode.com/problems/sort-the-matrix-diagonally/

// Time: O((M + N) * XlogX) where X is min(M, N) 
// Space: O(X)
class Solution {
public:
    vector<vector<int>> diagonalSort(vector<vector<int>>& A) {
        int M = A.size(), N = A[0].size();
        for (int i = 0; i < M; ++i) {
            vector<int> v;
            for (int x = i, y = 0; x < M && y < N; ++x, ++y) v.push_back(A[x][y]);
            sort(begin(v), end(v));
            for (int x = i, y = 0; x < M && y < N; ++x, ++y) A[x][y] = v[y];
        }
        for (int j = 1; j < N; ++j) {
            vector<int> v;
            for (int x = 0, y = j; x < M && y < N; ++x, ++y) v.push_back(A[x][y]);
            sort(begin(v), end(v));
            for (int x = 0, y = j; x < M && y < N; ++x, ++y) A[x][y] = v[x];
        }
        return A;
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/sort-the-matrix-diagonally/

// Time: O(min(M, N)^2 * (M + N))
// Space: O(1)
class Solution {
    int M, N, minMN;
    void bubbleSort(vector<vector<int>> &v, int sx, int sy) {
        for (int i = 0; i < minMN; ++i) {
            int ix = sx + i, iy = sy + i;
            if (ix >= M || iy >= N) return;
            for (int j = minMN - 1; j > i; --j) {
                int jx = sx + j, jy = sy + j;
                if (jx >= M || jy  >= N) continue;
                if (jx - 1 < 0 || jy - 1 < 0) break;
                if (v[jx][jy] < v[jx - 1][ jy - 1]) swap(v[jx][jy] ,v[jx - 1][ jy - 1]);
            }
        }
    }
public:
    vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
        M = mat.size(), N = mat[0].size(), minMN = min(M, N);
        for (int i = 0; i < N; ++i) bubbleSort(mat, 0, i);
        for (int i = 1; i < M; ++i) bubbleSort(mat, i, 0);            
        return mat;
    }
};

Java

class Solution {
    public int[][] diagonalSort(int[][] mat) {
        int rows = mat.length, columns = mat[0].length;
        int startRow = rows - 2, startColumn = 0;
        while (startRow > 0) {
            List<Integer> diagonalList = new ArrayList<Integer>();
            for (int i = startRow, j = startColumn; i < rows && j < columns; i++, j++)
                diagonalList.add(mat[i][j]);
            Collections.sort(diagonalList);
            for (int i = startRow, j = startColumn, index = 0; i < rows && j < columns; i++, j++, index++)
                mat[i][j] = diagonalList.get(index);
            startRow--;
        }
        if (startRow < 0)
            return mat;
        while (startColumn < columns) {
            List<Integer> diagonalList = new ArrayList<Integer>();
            for (int i = startRow, j = startColumn; i < rows && j < columns; i++, j++)
                diagonalList.add(mat[i][j]);
            Collections.sort(diagonalList);
            for (int i = startRow, j = startColumn, index = 0; i < rows && j < columns; i++, j++, index++)
                mat[i][j] = diagonalList.get(index);
            startColumn++;
        }
        return mat;
    }
}

All Problems

All Solutions