Skip to content

Commit 82980df

Browse files
authored
Merge pull request larissalages#163 from ojasvik/master
added cpp code for 1605 leetcode
2 parents 13b3d3a + f1dc219 commit 82980df

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

leetcode/cpp/string/1605.cpp

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

0 commit comments

Comments
 (0)