498. Diagonal Traverse

class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
        rows, cols = len(mat), len(mat[0])

        res = []

        curRow, curCol = 0, 0 
        isUp = True

        while len(res) != rows * cols:
            if isUp:
                while curRow >= 0 and curCol < cols:
                    res.append(mat[curRow][curCol])
                    curRow -= 1
                    curCol += 1

                if curCol == cols:
                    curCol -= 1
                    curRow += 2
                else:
                    curRow += 1

                isUp = False
            else:
                while curRow < rows and curCol >= 0:
                    res.append(mat[curRow][curCol])
                    curRow += 1
                    curCol -= 1

                if curRow == rows:
                    curCol += 2
                    curRow -= 1
                else:
                    curCol += 1

                isUp = True
                
        return res