✅C++ || Logic Explained in detail || EASY
\n\nLogic->You cannot do anything about colsum[i] = 2 case or colsum[i] = 0 case. Then you put colsum[i] = 1 case to the upper row until upper has reached. Then
![image](https://assets.leetcode.com/users/images/bbceabad-4e44-48cf-a14d-59facbcd846d_1659953169.3950763.png)\n\n***Logic->You cannot do anything about colsum[i] = 2 case or colsum[i] = 0 case. Then you put colsum[i] = 1 case to the upper row until upper has reached. Then put the rest into lower row.***\n\n**T->O(n) && S->O(n * 2)**\n\n\t\tclass Solution {\n\t\tpublic:\n\t\t\tvector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {\n\t\t\t\tint n=colsum.size();\n\t\t\t\tvector<vector<int>>mat(2,vector<int>(n,0));\n\t\t\t\tif(lower+upper!=accumulate(colsum.begin(),colsum.end(),0))return {};\n\t\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\t\tif(colsum[j]==2){\n\t\t\t\t\t\tmat[1][j]=mat[0][j]=1;\n\t\t\t\t\t\tcolsum[j]=0;\n\t\t\t\t\t\tupper--;\n\t\t\t\t\t\tlower--;\n\t\t\t\t\t}        \n\t\t\t\t}\n\t\t//      Upper\n\t\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\t\tif(upper && colsum[j]){\n\t\t\t\t\t\tmat[0][j]=1;\n\t\t\t\t\t\tupper--;\n\t\t\t\t\t\tcolsum[j]--;\n\t\t\t\t\t}    \n\t\t\t\t}\n\t\t//      Lower\n\t\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\t\tif(lower && colsum[j]){\n\t\t\t\t\t\tmat[1][j]=1;\n\t\t\t\t\t\tlower--;\n\t\t\t\t\t\tcolsum[j]--;\n\t\t\t\t\t}    \n\t\t\t\t}\n\t\t\t\tif(lower || upper)return {};\n\t\t\t\treturn mat;\n\t\t\t}\n\t\t};
['C', 'C++']