File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 517
517
528|[ Random Pick with Weight] ( ./solutions/0528-random-pick-with-weight.js ) |Medium|
518
518
529|[ Minesweeper] ( ./solutions/0529-minesweeper.js ) |Medium|
519
519
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|
520
521
532|[ K-diff Pairs in an Array] ( ./solutions/0532-k-diff-pairs-in-an-array.js ) |Medium|
521
522
535|[ Encode and Decode TinyURL] ( ./solutions/0535-encode-and-decode-tinyurl.js ) |Medium|
522
523
537|[ Complex Number Multiplication] ( ./solutions/0537-complex-number-multiplication.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments