Welcome to Subscribe On Youtube

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.

  • 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;
        }
    }
    
    ############
    
    class Solution {
        public int[][] diagonalSort(int[][] mat) {
            int m = mat.length, n = mat[0].length;
            for (int k = 0; k < Math.min(m, n) - 1; ++k) {
                for (int i = 0; i < m - 1; ++i) {
                    for (int j = 0; j < n - 1; ++j) {
                        if (mat[i][j] > mat[i + 1][j + 1]) {
                            int t = mat[i][j];
                            mat[i][j] = mat[i + 1][j + 1];
                            mat[i + 1][j + 1] = t;
                        }
                    }
                }
            }
            return mat;
        }
    }
    
  • // 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;
        }
    };
    
  • class Solution:
        def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
            m, n = len(mat), len(mat[0])
            for k in range(min(m, n) - 1):
                for i in range(m - 1):
                    for j in range(n - 1):
                        if mat[i][j] > mat[i + 1][j + 1]:
                            mat[i][j], mat[i + 1][j + 1] = mat[i + 1][j + 1], mat[i][j]
            return mat
    
    
    
  • func diagonalSort(mat [][]int) [][]int {
    	m, n := len(mat), len(mat[0])
    	for k := 0; k < m-1 && k < n-1; k++ {
    		for i := 0; i < m-1; i++ {
    			for j := 0; j < n-1; j++ {
    				if mat[i][j] > mat[i+1][j+1] {
    					mat[i][j], mat[i+1][j+1] = mat[i+1][j+1], mat[i][j]
    				}
    			}
    		}
    	}
    	return mat
    }
    

All Problems

All Solutions