-
-
Notifications
You must be signed in to change notification settings - Fork 100
3446. Sort Matrix by Diagonals.cpp #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Reviewer's GuideThis PR introduces a new C++ solution that sorts each diagonal of an n×n matrix by extracting diagonals from the bottom-left and top-right regions into temporary arrays, sorting them in the required order, and then writing the sorted values back into the original grid. Class diagram for the new Solution class implementing diagonal sortingclassDiagram
class Solution {
+vector<vector<int>> sortMatrix(vector<vector<int>>& grid)
}
class vector
class vector_vector_int
class vector_int
Solution --> vector_vector_int
Solution --> vector_int
vector_vector_int <|-- vector
vector_int <|-- vector
Flow diagram for diagonal extraction, sorting, and placement in the matrixflowchart TD
A[Start] --> B[Extract bottom-left diagonals]
B --> C[Sort each bottom-left diagonal in descending order]
C --> D[Extract top-right diagonals]
D --> E[Sort each top-right diagonal in ascending order]
E --> F[Place sorted diagonals back into matrix]
F --> G[Return updated matrix]
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Refactor the diagonal extraction and writing logic into a helper function to avoid duplicating the bottom-left and top-right processing loops.
- Replace single-letter variables like p, q, r, and j with descriptive names such as row and col to make the iteration logic clearer.
- Instead of collecting all diagonals into ans/a1 then writing back later, consider sorting and writing each diagonal in place to simplify the control flow and reduce indexing errors.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Refactor the diagonal extraction and writing logic into a helper function to avoid duplicating the bottom-left and top-right processing loops.
- Replace single-letter variables like p, q, r, and j with descriptive names such as row and col to make the iteration logic clearer.
- Instead of collecting all diagonals into ans/a1 then writing back later, consider sorting and writing each diagonal in place to simplify the control flow and reduce indexing errors.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
🎉 Congrats on getting your PR merged in, @Subhosjx! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
PR Title Format: 3446. Sort Matrix by Diagonals.cpp
Intuition
The problem requires sorting matrix diagonals differently based on their position:
Bottom-left triangle (including the main diagonal) → sort in non-increasing order
Top-right triangle → sort in non-decreasing order
Each diagonal can be uniquely identified by its starting position: either from the first column (bottom-left) or from the first row (top-right). Once we know the starting point of a diagonal, we can extract all its elements, sort them according to the required order, and place them back.
By processing one diagonal at a time, we ensure that sorting is localized and does not interfere with other diagonals. This approach leverages the small constraint on 𝑛 ≤ 10 to keep sorting simple and efficient.
Approach
Identify diagonals:
Bottom-left diagonals start from the first column (rows from n-1 to 0).
Top-right diagonals start from the first row (columns from 1 to n-1).
Extract elements of each diagonal into a temporary array.
Sort the diagonal:
Bottom-left → descending order (non-increasing)
Top-right → ascending order (non-decreasing)
Place elements back into the matrix along the same diagonal path.
Repeat for all diagonals until the entire matrix is updated.
Return the updated matrix.
Code Solution (C++)
Related Issues
#144
By submitting this PR, I confirm that:
Summary by Sourcery
Sort matrix diagonals by extracting each diagonal, sorting bottom-left diagonals in descending order and top-right diagonals in ascending order, and writing values back to the grid.
New Features: