Approach
Heap or priority queue
For Sort the Matrix Diagonally, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.
- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 20 lines of C++ from the credited upstream file 1329.cpp.
- The implementation visibly relies on sequence storage, hash lookup, work queue.
- 4 loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.