This is one of those problems where the clean idea matters more than the amount of code. For 1329. Sort the Matrix Diagonally, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The solution is organized around the main LeetCode entry point and a few local helpers.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 28 ms, faster than 86.54% of C++ online submissions for Sort the Matrix Diagonally.
02//Memory Usage: 10.4 MB, less than 100.00% of C++ online submissions for Sort the Matrix Diagonally.
03
04class Solution {
05public:
06 vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
07 int m = mat.size(), n = mat[0].size();
08
09 int diag_num = (m-1)+1+(n-1);
10
11 // for(int i = 0; i < m; i++){
12 // for(int j = 0; j < n; j++){
13 // cout << mat[i][j] << " ";
14 // }
15 // cout << endl;
16 // }
17 // cout << endl;
18
19 for(int k = 0; k < diag_num; k++){
20 vector<int> tmp;
21
22 for(int i = max(m-1-k, 0), j = max(k-m+1, 0); i < m && j < n; i++, j++){
23 // cout << "i: " << i << ", j: " << j << endl;
24 tmp.push_back(mat[i][j]);
25 }
26 // cout << "tmp: " << tmp.size() << endl;
27 sort(tmp.begin(), tmp.end());
28 for(int i = max(m-1-k, 0), j = max(k-m+1, 0), t = 0; i < m && j < n; i++, j++, t++){
29 mat[i][j] = tmp[t];
30 }
31 }
32
33 // for(int i = 0; i < m; i++){
34 // for(int j = 0; j < n; j++){
35 // cout << mat[i][j] << " ";
36 // }
37 // cout << endl;
38 // }
39 // cout << endl;
40
41 return mat;
42 }
43};
Cost