πΉ Problem: 3446 Sort Matrix by Diagonals
Difficulty: #Medium
Tags: #Matrix, #HashMap, #Sorting
π Problem Summary
You are given a 2D grid. Each diagonal of the matrix (identified by the difference
row - col
) must be sorted independently.
- Diagonals starting from the top-right should be sorted in descending order.
- Diagonals starting from the top-left should be sorted in ascending order. Return the modified matrix after sorting each diagonal.
π§ My Thought Process
Brute Force Idea:
Extract each diagonal separately into a list, sort it, and then put the sorted values back. But this requires a lot of manual tracking of diagonals.-
Optimized Strategy:
Instead of treating diagonals manually, use the property that each diagonal is uniquely identified byrow - col
.- Store elements of each diagonal in a
hashmap
wherekey = row - col
. - Sort diagonals:
- If
key < 0
(upper diagonals), sort descending. - If
key >= 0
(lower diagonals), sort ascending. - Refill the matrix by popping elements back in the correct order.
- Store elements of each diagonal in a
Algorithm Used:
HashMap for grouping + Sorting each diagonal independently
.
βοΈ Code Implementation (Python)
from collections import defaultdict
from typing import List
class Solution:
def sortMatrix(self, grid: List[List[int]]) -> List[List[int]]:
hashmap = defaultdict(list)
rows = len(grid)
cols = len(grid[0])
# Group elements by diagonal (key = row - col)
for row in range(rows):
for col in range(cols):
key = row - col
hashmap[key].append(grid[row][col])
# Sort each diagonal
for key in hashmap:
if key < 0:
hashmap[key].sort(reverse=True)
else:
hashmap[key].sort()
# Refill the matrix with sorted diagonals
for row in range(rows):
for col in range(cols):
key = row - col
grid[row][col] = hashmap[key].pop()
return grid
β±οΈ Time & Space Complexity
- Time: O(m Γ n Γ log(min(m, n))) β each diagonal is sorted individually.
- Space: O(m Γ n) β hashmap storage for all diagonals.
π§© Key Takeaways
- β
Learned the trick that diagonals in a matrix can be grouped using
row - col
. - π‘ The twist was handling different sort orders (ascending vs descending) depending on diagonal position.
- π For future diagonal problems, always check if
row - col
orrow + col
can uniquely represent them.
π Reflection (Self-Check)
- [x] Could I solve this without help?
- [x] Did I write code from scratch?
- [x] Did I understand why it works?
* [x] Will I be able to recall this in a week?
π Related Problems
- [[1329 Sort the Matrix Diagonally]]
π Progress Tracker
Metric | Value |
---|---|
Day | 69 |
Total Problems Solved | 430 |
Confidence Today | π |
Leetcode Rating | 1530 |
Top comments (0)