Skip to content

Commit

Permalink
Merge pull request #75 from AlgoLeadMe/21-InSange
Browse files Browse the repository at this point in the history
21-Insange
  • Loading branch information
InSange authored Aug 5, 2024
2 parents 88f6b71 + 8c54d04 commit bced723
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
| 18차시 | 2024.07.10 | 문자열 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [#18](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)]
| 19차시 | 2024.07.13 | 스택 | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/72)]
| 20차시 | 2024.07.17 | 스택 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/74)]
| 21차시 | 2024.07.21 | 그리디 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/75)]
---

https://leetcode.com/problems/robot-collisions/
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <vector>

using namespace std;

class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
vector<vector<int>> ans;
int rsize = rowSum.size();
int csize = colSum.size();
int rindex = 0;
int cindex = 0;

ans.assign(rsize, vector<int>(csize, 0));

while (rindex < rsize || cindex < csize)
{
if (rindex >= rsize)
{
ans[rindex - 1][cindex] = colSum[cindex];
cindex++;
continue;
}
else if (cindex >= csize)
{
ans[rindex][cindex - 1] = rowSum[rindex];
rindex++;
continue;
}

int val = min(colSum[cindex], rowSum[rindex]);

ans[rindex][cindex] = val;
rowSum[rindex] -= val;
colSum[cindex] -= val;

if (!rowSum[rindex])
{
rindex++;
}
if (!colSum[cindex])
{
cindex++;
}
}

return ans;
}
};

0 comments on commit bced723

Please sign in to comment.