Matrix Diagonal Stacks to Max Heap
Given a matrix of integers, process each of its diagonals forming a stack (using a top-down approach) starting from the top-right corner to the bottom-left corner of the matrix. Convert each diagonal stack into a max heap structure and return these heaps as an array of integers, each representing the maximum element from each transformed max heap.
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Explanation. We process the diagonals starting from the top-right corner. Diagonals are: [3], [2, 6], [1, 5, 9], [4, 8], [7]. Each diagonal transformed into a max-heap would then have the maximum elements as: 3, 6, 9, 8, 7.
[ [ 10, 20 ], [ 15, 25 ] ]
Explanation. Diagonals are: [20], [10, 25], [15]. Converting these to max-heaps and extracting the max gives us: [20, 25, 15, 10].
Follow-up: Can you optimize your solution to achieve better than $O(n^2 \log n)$ complexity, where $n$ is the length of the matrix diagonal?
1. The matrix will have at least one row and one column.\n2. Matrix elements and the number of rows and columns are all integers.\n3. The result should be ordered from the diagonal starting at the top-right corner to the one starting at the bottom-left corner.
- Accepted
- 1/2
- Acceptance Rate
- 50.0%
- Views
- 1