Lonely Pixel I in C++

C++Server Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Suppose we have a picture consisting of black and white pixels, we have to find the number of black lonely pixels. Here the picture is represented by a 2D char array consisting of 'B' and 'W', for the black and white pixels respectively.

A black lonely pixel is actually 'B' that located at a specific position where the same row and same column don't have any other black pixels.

If the input is like −

 W W B W B W B W W

Output will be 3. Because all the three 'B's are black lonely pixels.

To solve this, we will follow these steps −

• n := size of picture

• m := (if n is non-zero, then column size, otherwise 0)

• Define two arrays row and col of size n

• ret := 0, firstRow := 0

• for initialize i := 0, when i < n, update (increase i by 1), do −

• for initialize j := 0, when j < m, update (increase j by 1), do −

• if picture[i, j] is same as 'B', then −

• if picture[0, j] < 'Y' and picture[0, j] is not equal to 'V', then −

• (increase picture[0, j] by 1)

• if i is same as 0, then −

• (increase firstRow by 1)

• otherwise when picture[i, 0] < 'Y' and picture[i, 0] is not equal to 'V', then −

• (increase picture[i, 0] by 1)

• for initialize i := 0, when i < n, update (increase i by 1), do −

• for initialize j := 0, when j < m, update (increase j by 1), do −

• if picture[i, j] < 'W' and (picture[0, j] is same as 'C' or picture[0, j] is same as 'X'), then −

• if i is same as 0, then −

• ret := (if ret + firstRow is same as 1, then 1, otherwise 0)

• otherwise when picture[i, 0] is same as 'C' or picture[i, 0] is same as 'X', then −

• (increase ret by 1)

• return ret

Example

Let us see the following implementation to get better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
int n = picture.size();
int m = n ? picture[0].size() : 0;
vector<int< row(n);
vector<int< col(m);
int ret = 0;
int firstRow = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B') {
if (picture[0][j] < 'Y' && picture[0][j] != 'V'){
picture[0][j]++;
}
if (i == 0)
firstRow++;
else if (picture[i][0] < 'Y' && picture[i][0] != 'V') {
picture[i][0]++;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] < 'W' && (picture[0][j] == 'C' || picture[0][j] == 'X')) {
if (i == 0)
ret += firstRow == 1 ? 1 : 0;
else if (picture[i][0] == 'C' || picture[i][0] == 'X')
ret++;
}
}
}
return ret;
}
};
main(){
Solution ob;
vector<vector<char>> v = {{'W','W','B'},{'W','B','W'},{'B','W','W'}};
cout << (ob.findLonelyPixel(v));
}

Input

{{'W','W','B'},{'W','B','W'},{'B','W','W'}}

Output

3
Updated on 19-Nov-2020 10:17:47