Skip to content

Commit 26c2d15

Browse files
authored
Merge pull request codeharborhub#2875 from Aditijainnn/Q531_lcsolution
added solution of lc problem 531
2 parents 4145c12 + fd1201c commit 26c2d15

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
id: Lonely-Pixel-I
3+
title: Lonely Pixel I
4+
sidebar_label: 0531- Lonely Pixel I
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Matrix
9+
description: "Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of black lonely pixels."
10+
---
11+
12+
## Problem
13+
Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of black lonely pixels.
14+
15+
A black lonely pixel is a character 'B' that located at a specific position where the same row and same column don't have any other black pixels.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
**Input:** `picture = "[["W","W","B"],["W","B","W"],["B","W","W"]]"`
22+
**Output:** `3`
23+
**Explanation:** All the three 'B's are black lonely pixels.
24+
25+
**Example 2:**
26+
27+
**Input:** ` picture = "[["B","B","B"],["B","B","W"],["B","B","B"]]"`
28+
**Output:** `0`
29+
30+
### Constraints
31+
- m == picture.length
32+
- n == picture[i].length
33+
- `1 <= m, n <= 500`
34+
- picture[i][j] is 'W' or 'B'.
35+
36+
---
37+
38+
## Approach
39+
40+
According to the problem description, we need to count the number of black pixels in each row and column, which are recorded in the arrays rows and cols respectively. Then we traverse each black pixel, check whether there is only one black pixel in its row and column. If so, we increment the answer by one.
41+
42+
### Steps:
43+
44+
1. Initialize two arrays, `rows` and `cols`, to count the number of 'B' pixels in each row and column, respectively.
45+
2. Iterate through the 2D array to populate the `rows` and `cols` arrays with the count of 'B' pixels for each row and column.
46+
3. Initialize a counter `ans` to keep track of the number of lonely pixels.
47+
Iterate through the 2D array again. For each 'B' pixel, check if it is a lonely pixel by verifying if `rows[i]` and `cols[j]` are both equal to 1.
48+
4. Return the count of lonely pixels.
49+
50+
### Solution
51+
52+
#### Java Solution
53+
54+
```java
55+
class Solution {
56+
public int findLonelyPixel(char[][] picture) {
57+
int m = picture.length, n = picture[0].length;
58+
int[] rows = new int[m];
59+
int[] cols = new int[n];
60+
for (int i = 0; i < m; ++i) {
61+
for (int j = 0; j < n; ++j) {
62+
if (picture[i][j] == 'B') {
63+
++rows[i];
64+
++cols[j];
65+
}
66+
}
67+
}
68+
int ans = 0;
69+
for (int i = 0; i < m; ++i) {
70+
for (int j = 0; j < n; ++j) {
71+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
72+
++ans;
73+
}
74+
}
75+
}
76+
return ans;
77+
}
78+
}
79+
```
80+
#### C++ Solution
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int findLonelyPixel(vector<vector<char>>& picture) {
86+
int m = picture.size(), n = picture[0].size();
87+
vector<int> rows(m);
88+
vector<int> cols(n);
89+
for (int i = 0; i < m; ++i) {
90+
for (int j = 0; j < n; ++j) {
91+
if (picture[i][j] == 'B') {
92+
++rows[i];
93+
++cols[j];
94+
}
95+
}
96+
}
97+
int ans = 0;
98+
for (int i = 0; i < m; ++i) {
99+
for (int j = 0; j < n; ++j) {
100+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
101+
++ans;
102+
}
103+
}
104+
}
105+
return ans;
106+
}
107+
};
108+
```
109+
#### Python Solution
110+
111+
```python
112+
class Solution:
113+
def findLonelyPixel(self, picture: List[List[str]]) -> int:
114+
rows = [0] * len(picture)
115+
cols = [0] * len(picture[0])
116+
for i, row in enumerate(picture):
117+
for j, x in enumerate(row):
118+
if x == "B":
119+
rows[i] += 1
120+
cols[j] += 1
121+
ans = 0
122+
for i, row in enumerate(picture):
123+
for j, x in enumerate(row):
124+
if x == "B" and rows[i] == 1 and cols[j] == 1:
125+
ans += 1
126+
return ans
127+
```
128+
### Complexity Analysis
129+
**Time Complexity:** O(m x n)
130+
>Reason: We iterate over each element in the picture array to count the number of 'B' pixels in each row and column.
131+
132+
**Space Complexity:** O(m + n)
133+
>Reason: We use two additional arrays, `rows` and `cols`, each of size mm and nn respectively, to count the number of 'B' pixels in each row and column.
134+
135+
### References
136+
**LeetCode Problem:** Lonely Pixel I

0 commit comments

Comments
 (0)