<p>A <strong>matrix diagonal</strong> 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&#39;s end. For example, the <strong>matrix diagonal</strong> starting from <code>mat[2][0]</code>, where <code>mat</code> is a <code>6 x 3</code> matrix, includes cells <code>mat[2][0]</code>, <code>mat[3][1]</code>, and <code>mat[4][2]</code>.</p>

<p>Given an <code>m x n</code> matrix <code>mat</code> of integers, sort each <strong>matrix diagonal</strong> in ascending order and return <em>the resulting matrix</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png" style="width: 500px; height: 198px;" />
<pre>
<strong>Input:</strong> mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
<strong>Output:</strong> [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> 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]]
<strong>Output:</strong> [[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]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
	<li><code>1 &lt;= mat[i][j] &lt;= 100</code></li>
</ul>

class Solution {
public:
    vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
        // Store the matrix dimensions.
        size_t m = mat.size();
        size_t n = mat[0].size();

        // Sort each diagonal that starts on a row.
        for (size_t row = 0; row < m; row++) {
            sortDiagonal(row, 0, mat);
        }

        // Sort each diagonal that starts on a col.
        // Note that we've already sorted the one starting
        // at col = 0; this is the same as the one starting
        // at row = 0.
        for (size_t col = 0; col < n; col++) {
            sortDiagonal(0, col, mat);
        }

        return mat;
    }

    // Helper function to sort the current diagonal at row, col
    void sortDiagonal(size_t row, size_t col, vector<vector<int>>& mat) {
        size_t m = mat.size();
        size_t n = mat[0].size();

        // create a min-heap to store the diagonal
        priority_queue<int, vector<int>, greater<int>> diagonal;

        size_t diagonal_length = min(m - row, n - col);
        // Put values on this diagonal into the heap.
        for (size_t i = 0; i < diagonal_length; i++) {
            diagonal.push(mat[row + i][col + i]);
        }

        // Put values on heap back into this diagonal
        for (size_t i = 0; i < diagonal_length; i++) {
            mat[row + i][col + i] = diagonal.top();
            diagonal.pop();
        }
    }
};
12900