<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 {
public:
    vector<int> findDiagonalOrder(const vector<vector<int>>& matrix) {
        // return findDiagonalOrderSpace(matrix);
        // return findDiagonalOrderNoSpace(matrix);
        return findDiagonalOrderGraph(matrix);
    }

private:
    vector<int> findDiagonalOrderSpace(const vector<vector<int>>& matrix) {
        if (matrix.empty())
            return {};

        const int m = matrix.size();
        const int n = matrix[0].size();

        vector<int> res;
        res.reserve(m * n);

        unordered_map<int, vector<int>> mp;

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                mp[i + j].push_back(matrix[i][j]);
            }
        }

        for (int i = 0; i < n + m - 1; ++i) {
            auto& m = mp[i];
            if (i % 2 == 0) {
                reverse(m.begin(), m.end());
            }

            res.insert(res.end(), m.begin(), m.end());
        }

        return res;
    }

    vector<int> findDiagonalOrderGraph(const vector<vector<int>>& matrix) {
        if (matrix.empty())
            return {};

        queue<vector<int>> qu;
        qu.push({0, 0, 0});

        vector<int> res;
        while (!qu.empty()) {
          auto vec = qu.front(); qu.pop();
          int row = vec[0];
          int col = vec[1];
          int dir = vec[2];

          res.push_back(matrix[row][col]);

          int nextRow = row - 1;
          int nextCol = col + 1;

          if (dir != 0) {
            nextRow = row + 1;
            nextCol = col - 1;
          }

          bool isValid = nextRow < matrix.size() && nextRow >= 0 && nextCol < matrix[nextRow].size() && nextRow >= 0;
          if (isValid) {
            qu.push({nextRow, nextCol, dir});
          } else {
            if (dir == 0) {
              if (col+1 < matrix[row].size()) {
                qu.push({row, col+1, 1});
              } else if (row+1 < matrix.size()) {
                qu.push({row+1, col, 1});  
              }
            } else if (dir != 0) {
              if (row+1 < matrix.size()) {
                qu.push({row+1, col, 0});
              } else if (col+1 < matrix[row].size()) {
                qu.push({row, col+1, 0});
              }
            } 
          }
        }
        
        return res;
    }

    vector<int> findDiagonalOrderNoSpace(const vector<vector<int>>& matrix) {
         if (matrix.empty()) return {};
        
        const int m = matrix.size();
        const int n = matrix[0].size();
        
        vector<int> res(m*n);
        int counter = 0;
        for (int sumIdx = 0; sumIdx < m+n-1; ++sumIdx) {
            for (int j = 0; j <= sumIdx; ++j) {
                int ii = sumIdx % 2 != 0? j : sumIdx - j;
                int jj = sumIdx % 2 != 0? sumIdx - j : j;
                
                if (ii >= m || jj >= n) continue;
                
                res[counter] = matrix[ii][jj];
                ++counter;
            }
        }
        
        return res;
    }
};
31103