Skip to content

Commit 6339309

Browse files
authored
Create 1605. Find Valid Matrix Given Row and Column Sums
1 parent 42ce8dc commit 6339309

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
4+
int n=rowSum.size(),i=0;
5+
int m=colSum.size(),j=0;
6+
vector<vector<int>>ans(n,vector<int>(m,0));
7+
while(i<n && j<m){
8+
if(rowSum[i]<=colSum[j]){
9+
ans[i][j]=rowSum[i];
10+
colSum[j]-=rowSum[i];
11+
rowSum[i]=0;
12+
}else{
13+
ans[i][j]=colSum[j];
14+
rowSum[i]-=colSum[j];
15+
colSum[j]=0;
16+
17+
}
18+
if(rowSum[i]==0) i++;
19+
if(colSum[j]==0) j++;
20+
}
21+
return ans;
22+
}
23+
};

0 commit comments

Comments
 (0)