1253. Reconstruct a 2-Row Binary Matrix.cpp
Code\n\nclass Solution {\npublic:\n    vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& col) {\n        int n = col.size();\n        vec
**Code**\n```\nclass Solution {\npublic:\n    vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& col) {\n        int n = col.size();\n        vector<vector<int>>res(2,vector<int>(n,0));\n        vector<int>colsum(col.begin(),col.end());        \n        int rowsum [2] = {upper,lower};\n        for(int j=0;j<n;j++)\n        {\n           if(colsum[j]==2)\n           {\n               res[0][j] = 1;\n               res[1][j] = 1;\n               colsum[j] = 0;\n               rowsum[0] -= 1;\n               rowsum[1] -= 1;\n           }\n        }\n        for(int i=0;i<2;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(res[i][j]!=1)\n                {\n                    res[i][j] = (rowsum[i]>0 && colsum[j]>0) ? 1 : 0;\n                    rowsum[i] -= res[i][j];\n                    colsum[j] -= res[i][j];\n                }\n            }   \n        }\n        int t_upper = 0;\n        int t_lower = 0;\n        for(int i=0;i<n;i++)\n        {\n            t_upper+=res[0][i];\n            t_lower+=res[1][i];         \n            if(col[i]!=(res[0][i]+res[1][i]))return {};\n        }        \n        if(t_upper != upper || t_lower!=lower)return {};         \n        return res;\n    }\n};\n```
['C']