Find Valid Matrix Given Row and Column Sums LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<int>> restoreMatrix(vector<int> &rowSum, vector<int> &colSum) {
4
int nRow = rowSum.size();
5
int nCol = colSum.size();
6

7
vector<vector<int>> res(nRow, vector<int>(nCol, 0));
8

9
int i = 0, j = 0;
10
while (i < nRow and j < nCol) {
11
if (rowSum[i] < colSum[j]) {
12
res[i][j] = rowSum[i];
13
colSum[j] -= rowSum[i];
14
rowSum[i++] = 0;
15
} else {
16
res[i][j] = colSum[j];
17
rowSum[i] -= colSum[j];
18
colSum[j++] = 0;
19
}
20
}
21
return res;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0