
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Lonely Pixel I in C++
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 −
#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
- Related Articles
- Lonely Pixel II in C++
- How to get the value of a specific pixel in OpenCV using C++?
- Setting Transparency Based on Pixel Values in Matplotlib
- How to read the pixel value from the multichannel image in OpenCV using C++?
- Review of the New Google’s Pixel
- How to read the pixel value of a single channel image in OpenCV using C++?
- Get pixel color from canvas with HTML
- How to return pixel depth of the screen in JavaScript?
- How to change Pixel values using Direct Access Method in OpenCV?
- How to set a single pixel using imagesetpixel() function in PHP?
- How to get pixel coordinates for Matplotlib-generated scatterplot?
- How to change Pixel Values using 'at' Method in OpenCV?
- How to access and modify pixel value in an image using OpenCV Python?
- How to get the pixel depth and color depth of a screen in JavaScript?
- How to view the pixel values of an image using scikit-learn in Python?
