Skip to content

Commit 35ce1ae

Browse files
committed
Add solution #531
1 parent 1a9eeb6 commit 35ce1ae

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@
517517
528|[Random Pick with Weight](./solutions/0528-random-pick-with-weight.js)|Medium|
518518
529|[Minesweeper](./solutions/0529-minesweeper.js)|Medium|
519519
530|[Minimum Absolute Difference in BST](./solutions/0530-minimum-absolute-difference-in-bst.js)|Easy|
520+
531|[Lonely Pixel I](./solutions/0531-lonely-pixel-i.js)|Medium|
520521
532|[K-diff Pairs in an Array](./solutions/0532-k-diff-pairs-in-an-array.js)|Medium|
521522
535|[Encode and Decode TinyURL](./solutions/0535-encode-and-decode-tinyurl.js)|Medium|
522523
537|[Complex Number Multiplication](./solutions/0537-complex-number-multiplication.js)|Medium|

solutions/0531-lonely-pixel-i.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 531. Lonely Pixel I
3+
* https://leetcode.com/problems/lonely-pixel-i/
4+
* Difficulty: Medium
5+
*
6+
* Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of
7+
* black lonely pixels.
8+
*
9+
* A black lonely pixel is a character 'B' that located at a specific position where the same
10+
* row and same column don't have any other black pixels.
11+
*/
12+
13+
/**
14+
* @param {character[][]} picture
15+
* @return {number}
16+
*/
17+
var findLonelyPixel = function(picture) {
18+
const rows = picture.length;
19+
const cols = picture[0].length;
20+
const rowCounts = new Array(rows).fill(0);
21+
const colCounts = new Array(cols).fill(0);
22+
let result = 0;
23+
24+
for (let row = 0; row < rows; row++) {
25+
for (let col = 0; col < cols; col++) {
26+
if (picture[row][col] === 'B') {
27+
rowCounts[row]++;
28+
colCounts[col]++;
29+
}
30+
}
31+
}
32+
33+
for (let row = 0; row < rows; row++) {
34+
for (let col = 0; col < cols; col++) {
35+
if (picture[row][col] === 'B' && rowCounts[row] === 1 && colCounts[col] === 1) {
36+
result++;
37+
}
38+
}
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)