Skip to content

Commit ea8dcba

Browse files
committed
Add solution #2482
1 parent 88b8ce7 commit ea8dcba

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@
306306
2244|[Minimum Rounds to Complete All Tasks](./2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
307307
2427|[Number of Common Factors](./2427-number-of-common-factors.js)|Easy|
308308
2469|[Convert the Temperature](./2469-convert-the-temperature.js)|Easy|
309+
2482|[Difference Between Ones and Zeros in Row and Column](./2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
309310

310311
## License
311312

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 2482. Difference Between Ones and Zeros in Row and Column
3+
* https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed m x n binary matrix grid.
7+
*
8+
* A 0-indexed m x n difference matrix diff is created with the following procedure:
9+
* - Let the number of ones in the ith row be onesRowi.
10+
* - Let the number of ones in the jth column be onesColj.
11+
* - Let the number of zeros in the ith row be zerosRowi.
12+
* - Let the number of zeros in the jth column be zerosColj.
13+
* - diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
14+
*
15+
* Return the difference matrix diff.
16+
*/
17+
18+
/**
19+
* @param {number[][]} grid
20+
* @return {number[][]}
21+
*/
22+
var onesMinusZeros = function(grid) {
23+
const rows = new Array(grid.length).fill(0);
24+
const columns = new Array(grid[0].length).fill(0);
25+
26+
grid.forEach((row, r) => row.forEach((value, c) => {
27+
rows[r] += value;
28+
columns[c] += value;
29+
}));
30+
31+
grid.forEach((row, r) => row.forEach((value, c) => {
32+
grid[r][c] = 2 * rows[r] - grid.length + 2 * columns[c] - row.length;
33+
}));
34+
35+
return grid;
36+
};

0 commit comments

Comments
 (0)