Skip to content

Commit 1ba8ed6

Browse files
[LEET-531] add 531
1 parent ae3fe15 commit 1ba8ed6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
55
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
66
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EncodeandDecodeTinyURL.java) | O(1) |O(n) | Medium | Design
7+
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LonelyPixelI.java) | O(m*n) |O(1) | Medium |
78
|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumAbsoluteDifferenceinBST.java) | O(n) |O(n) | Easy| DFS
89
|529|[Minesweeper](https://leetcode.com/problems/minesweeper/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Minesweeper.java) | O(m*n) |O(k) | Medium | BFS
910
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DetectCapital.java) | O(n) |O(1) | Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given a picture consisting of black and white pixels, find the number of black lonely pixels.
5+
6+
The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.
7+
8+
A black lonely pixel is character 'B' that located at a specific position where the same row and same column don't have any other black pixels.
9+
10+
Example:
11+
Input:
12+
[['W', 'W', 'B'],
13+
['W', 'B', 'W'],
14+
['B', 'W', 'W']]
15+
16+
Output: 3
17+
Explanation: All the three 'B's are black lonely pixels.
18+
Note:
19+
The range of width and height of the input 2D array is [1,500].
20+
*/
21+
public class LonelyPixelI {
22+
23+
public int findLonelyPixel(char[][] picture) {
24+
int m = picture.length;
25+
int n = picture[0].length;
26+
int count = 0;
27+
for (int i = 0; i < m; i++) {
28+
for (int j = 0; j < n; j++) {
29+
if (picture[i][j] == 'B' && isLonely(i, j, picture, m, n)) {
30+
count++;
31+
}
32+
}
33+
}
34+
return count;
35+
}
36+
37+
private boolean isLonely(int row, int col, char[][] picture, int m, int n) {
38+
for (int i = 0; i < m; i++) {
39+
if (i != row) {
40+
if (picture[i][col] == 'B') return false;
41+
}
42+
}
43+
44+
for (int j = 0; j < n; j++) {
45+
if (j != col) {
46+
if (picture[row][j] == 'B') return false;
47+
}
48+
}
49+
return true;
50+
}
51+
52+
}

0 commit comments

Comments
 (0)