Rows have unequal lengths and there are up to \(10^5\) entries, so walking diagonals with raw indices is awkward. Entries on one diagonal share \(i+j\); diagonals increase by \(i+j\), and within a diagonal \(j\) increases.
Store triples \((i+j,j,v)\), sort, and emit the values.
We observe that:
The value of \(i + j\) is the same for each diagonal;
The value of \(i + j\) for the next diagonal is greater than that of the previous diagonal;
Within the same diagonal, the value of \(i + j\) is the same, and the value of \(j\) increases from small to large.
Therefore, we store all numbers in the form of \((i, j, \textit{nums}[i][j])\) into \(\textit{arr}\), and then sort according to the first two items. Finally, return the array composed of the values at index 2 of all elements in \(\textit{arr}\).
The time complexity is \(O(n \times \log n)\), where \(n\) is the number of elements in the array \(\textit{nums}\). The space complexity is \(O(n)\).