Skip to content

Commit 1c3be40

Browse files
committed
Add solution #1582
1 parent 86bfc85 commit 1c3be40

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,382 LeetCode solutions in JavaScript
1+
# 1,383 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1207,6 +1207,7 @@
12071207
1577|[Number of Ways Where Square of Number Is Equal to Product of Two Numbers](./solutions/1577-number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers.js)|Medium|
12081208
1578|[Minimum Time to Make Rope Colorful](./solutions/1578-minimum-time-to-make-rope-colorful.js)|Medium|
12091209
1579|[Remove Max Number of Edges to Keep Graph Fully Traversable](./solutions/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.js)|Hard|
1210+
1582|[Special Positions in a Binary Matrix](./solutions/1582-special-positions-in-a-binary-matrix.js)|Easy|
12101211
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12111212
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12121213
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1582. Special Positions in a Binary Matrix
3+
* https://leetcode.com/problems/special-positions-in-a-binary-matrix/
4+
* Difficulty: Easy
5+
*
6+
* Given an m x n binary matrix mat, return the number of special positions in mat.
7+
*
8+
* A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and
9+
* column j are 0 (rows and columns are 0-indexed).
10+
*/
11+
12+
/**
13+
* @param {number[][]} mat
14+
* @return {number}
15+
*/
16+
var numSpecial = function(mat) {
17+
const rows = mat.length;
18+
const cols = mat[0].length;
19+
const rowSums = new Array(rows).fill(0);
20+
const colSums = new Array(cols).fill(0);
21+
let result = 0;
22+
23+
for (let i = 0; i < rows; i++) {
24+
for (let j = 0; j < cols; j++) {
25+
if (mat[i][j] === 1) {
26+
rowSums[i]++;
27+
colSums[j]++;
28+
}
29+
}
30+
}
31+
32+
for (let i = 0; i < rows; i++) {
33+
for (let j = 0; j < cols; j++) {
34+
if (mat[i][j] === 1 && rowSums[i] === 1 && colSums[j] === 1) {
35+
result++;
36+
}
37+
}
38+
}
39+
40+
return result;
41+
};

0 commit comments

Comments
 (0)