<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width: 158px; height: 143px;" />
<pre>
<strong>Input:</strong> nums = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [1,4,2,7,5,3,8,6,9]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png" style="width: 230px; height: 177px;" />
<pre>
<strong>Input:</strong> nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
<strong>Output:</strong> [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i].length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= sum(nums[i].length) &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

/*

idk y my code is not able to transpose [[1,2,3],[4],[5,6,7],[8],[9,10,11]] this matrix
coz this code is able to do transpose when I checked it manually 

#include <algorithm>
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& nums) {

        int maxi=0;
        for(int i=0; i<nums.size(); i++){ maxi = max( maxi,static_cast<int>( nums[i].size() ) ); }
        
        vector<int> v;

        for(int i=0; i<nums.size(); i++){

            if( nums[i].size() < maxi ){

                int size = maxi - nums[i].size();
                for( int j=0; j<size; j++ ){ nums[i].push_back(0); }
            }

        }

        cout<<"hello"<<endl;

        for (int i = 0; i < nums.size(); i++) {
            for (int j = 0; j < maxi; j++) {
                cout<<nums[i][j]<<" ";
            }
            cout<<endl;
        }

        vector<vector<int>> result(maxi, vector<int>(nums.size()) );

        //here is the issue its not doing transpose in test case 3 i.e :-  [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = 0; j < nums[i].size(); ++j) {  // Check actual size of nums[i]
                result[j][i] = nums[i][j];  // Swapped indices for transposing
            }
        }

        for (int i = 0; i < nums.size(); i++) {
            for (int j = 0; j < maxi; j++) {
                cout<<result[i][j]<<" ";
            }
            cout<<endl;
        }

        cout<<"hii"<<endl;

        int col = result[0].size();
        for( int i=0; i<col; i++ ){

            int first = 0;
            int sec = i;

            cout<<i<<endl;

            for( int j=0; j<i+1; j++ ){

                if( first <= col-1 && sec >=0 && result[first][sec] != 0 ){ v.push_back( result[first][sec] ); }

                first++;
                sec--;

            }

        }

        int row = result.size();

        for( int i=1; i<row; i++ ){

            int first = i;
            int sec = maxi-1;;

            for( int j=maxi-i; j>0; j-- ){

                if( result[first][sec] != 0 ){ v.push_back( result[first][sec] ); }

                first++;
                sec--;

            }

        }

        return v;

    }
};
*/

// so that's y I'm uploading this code from chatgpt
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
        vector<int> v;  // This will store the diagonal order
        vector<vector<int>> result;  // We will use this to temporarily store the transposed-like structure

        // Find maximum number of diagonals, i.e., maximum index sum i+j
        int max_diagonals = nums.size();
        for (const auto& row : nums) {
            if (!row.empty()) {
                max_diagonals += row.size() - 1;
            }
        }

        // Create a temporary storage for diagonals
        vector<vector<int>> diagonals(max_diagonals);

        // Fill diagonals with elements
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = 0; j < nums[i].size(); ++j) {
                diagonals[i + j].push_back(nums[i][j]);
            }
        }

        // Collect the results from diagonals in the correct order
        for (const auto& diagonal : diagonals) {
            for (auto it = diagonal.rbegin(); it != diagonal.rend(); ++it) {
                v.push_back(*it);
            }
        }

        return v;
    }
};
88975