Loading problem...
You are given a 2D integer array nums representing a jagged matrix (rows may have different lengths). Your task is to traverse and collect all elements following an anti-diagonal pattern, starting from the top-left corner.
An anti-diagonal consists of elements where the sum of their row and column indices is constant. The traversal proceeds as follows:
This pattern effectively traverses the matrix in wave-like anti-diagonal sweeps, where each wave moves from lower-left to upper-right within that diagonal group.
Key Insight: The defining property of anti-diagonals is that all elements on the same anti-diagonal share the same value of (row + column). Elements are ordered within each anti-diagonal by their row index in descending order (bottom to top).
nums = [[1,2,3],[4,5,6],[7,8,9]][1,4,2,7,5,3,8,6,9]The anti-diagonals are: [1], [4,2], [7,5,3], [8,6], [9]. Reading each from bottom to top gives the result. For example, the anti-diagonal with sum=2 contains elements at (2,0)=7, (1,1)=5, (0,2)=3, collected in that order.
nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]][1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]This is a jagged matrix where rows have varying lengths. The anti-diagonals are: [1], [6,2], [8,7,3], [9,4], [12,10,5], [13,11], [14], [15], [16]. Notice that missing elements (like row 2 column 1) are simply skipped, demonstrating how the algorithm handles irregular row lengths gracefully.
nums = [[1,2],[3,4]][1,3,2,4]For this 2x2 matrix, the anti-diagonals are: [1], [3,2], [4]. The middle anti-diagonal contains (1,0)=3 before (0,1)=2 since we process bottom to top.
Constraints