1329. Sort the Matrix Diagonally LeetCode Solution

In this guide, you will get 1329. Sort the Matrix Diagonally LeetCode Solution with the best time and space complexity. The solution to Sort the Matrix Diagonally problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sort the Matrix Diagonally solution in C++
  4. Sort the Matrix Diagonally solution in Java
  5. Sort the Matrix Diagonally solution in Python
  6. Additional Resources
1329. Sort the Matrix Diagonally LeetCode Solution image

Problem Statement of Sort the Matrix Diagonally

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]]

Example 2:

Input: mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
Output: [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]

Constraints:

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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1329. Sort the Matrix Diagonally LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
    const int m = mat.size();
    const int n = mat[0].size();

    unordered_map<int, priority_queue<int>> count;

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        count[i - j].push(mat[i][j]);

    for (int i = m - 1; i >= 0; --i)
      for (int j = n - 1; j >= 0; --j)
        mat[i][j] = count[i - j].top(), count[i - j].pop();

    return mat;
  }
};
/* code provided by PROGIEZ */

1329. Sort the Matrix Diagonally LeetCode Solution in Java

class Solution {
  public int[][] diagonalSort(int[][] mat) {
    final int m = mat.length;
    final int n = mat[0].length;

    Map<Integer, Queue<Integer>> count = new HashMap<>();

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        count.computeIfAbsent(i - j, k -> new PriorityQueue<>()).add(mat[i][j]);

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        mat[i][j] = count.get(i - j).poll();

    return mat;
  }
}
// code provided by PROGIEZ

1329. Sort the Matrix Diagonally LeetCode Solution in Python

class Solution:
  def diagonalSort(self, mat: list[list[int]]) -> list[list[int]]:
    m = len(mat)
    n = len(mat[0])

    count = collections.defaultdict(list)

    for i in range(m):
      for j in range(n):
        count[i - j].append(mat[i][j])

    for value in count.values():
      value.sort(reverse=1)

    for i in range(m):
      for j in range(n):
        mat[i][j] = count[i - j].pop()

    return mat
# code by PROGIEZ

Additional Resources

See also  827. Making A Large Island LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.