<p>Given an <code>m x n</code> matrix <code>mat</code>, return <em>an array of all the elements of the array in a diagonal order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg" style="width: 334px; height: 334px;" />
<pre>
<strong>Input:</strong> mat = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [1,2,4,7,5,3,6,8,9]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,2],[3,4]]
<strong>Output:</strong> [1,2,3,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= mat[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:

        n_rows=len(mat)
        n_cols=len(mat[0])
        n=n_rows*n_cols
        i=0
        integer_list=[0]*n

        pos_r=0
        pos_c=0

        dir_r=-1
        dir_c=1

        while i<n:
            #print(pos_c,pos_r,dir_c,dir_r, i, n, integer_list)
            integer_list[i]=mat[pos_r][pos_c]
            i+=1
        
            pos_r+=dir_r
            pos_c+=dir_c

            if pos_r<0 or pos_r>=n_rows or pos_c<0 or pos_c>=n_cols:
                #reset direction
                dir_r,dir_c=-dir_r,-dir_c
                #go in row direction 1
                if pos_c>=n_cols or pos_r<0:
                    pos_r+=1
                elif pos_c<0 or pos_r>=n_rows:
                    pos_c+=1
                #if still out of bounds, take a step
                if pos_r<0 or pos_r>=n_rows or pos_c<0 or pos_c>=n_cols:
                    pos_r+=dir_r
                    pos_c+=dir_c

        
        return integer_list

                


        
19200