generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
31 lines (27 loc) · 798 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vector>
class Solution {
public:
std::vector<std::vector<int>> onesMinusZeros(std::vector<std::vector<int>>& grid) {
const int m = grid.size();
const int n = grid[0].size();
// Calculate the total count of elements with a value of one in each row and column.
std::vector<int> oneRow(m);
std::vector<int> oneCol(n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
++oneRow[i];
++oneCol[j];
}
}
}
// Modify the given grid.
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
// Given that zeroRow[i] + zeroCol[j] = m + n - oneRow[i] - oneCol[j].
grid[i][j] = 2 * (oneRow[i] + oneCol[j]) - m - n;
}
}
return grid;
}
};