Formatted question description: https://leetcode.ca/all/533.html

533. Lonely Pixel II

Level

Medium

Description

Given a picture consisting of black and white pixels, and a positive integer N, find the number of black pixels located at some specific row R and column C that align with all the following rules:

  1. Row R and column C both contain exactly N black pixels.
  2. For all rows that have a black pixel at column C, they should be exactly the same as row R.

The picture is represented by a 2D char array consisting of ‘B’ and ‘W’, which means black and white pixels respectively.

Example:

Input:                                            
[['W', 'B', 'W', 'B', 'B', 'W'],    
 ['W', 'B', 'W', 'B', 'B', 'W'],    
 ['W', 'B', 'W', 'B', 'B', 'W'],    
 ['W', 'W', 'B', 'W', 'B', 'W']] 

N = 3
Output: 6
Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3).
        0    1    2    3    4    5         column index                                            
0    [['W', 'B', 'W', 'B', 'B', 'W'],    
1     ['W', 'B', 'W', 'B', 'B', 'W'],    
2     ['W', 'B', 'W', 'B', 'B', 'W'],    
3     ['W', 'W', 'B', 'W', 'B', 'W']]    
row index

Take 'B' at row R = 0 and column C = 1 as an example:
Rule 1, row R = 0 and column C = 1 both have exactly N = 3 black pixels. 
Rule 2, the rows have black pixel at column C = 1 are row 0, row 1 and row 2. They are exactly the same as row R = 0.

Note:

  1. The range of width and height of the input 2D array is [1,200].

Solution

Since this problem requires the rows should have exactly the same columns that have black pixels, map each row as a string to the count of the row, which means the number of rows that have this content.

Only consider the rows that have counts exactly N. For each such row, the row should contain exactly N black pixels, and for each position that is a black pixel, count the number of black pixels in the column. If there are exactly N black pixels in the column, then add N to the result. Finally, return the result.

  • class Solution {
        public int findBlackPixel(char[][] picture, int N) {
            if (picture == null || picture.length == 0 || picture[0].length == 0)
                return 0;
            Map<String, Integer> rowCountMap = new HashMap<String, Integer>();
            int rows = picture.length, columns = picture[0].length;
            for (int i = 0; i < rows; i++) {
                String row = new String(picture[i]);
                int count = rowCountMap.getOrDefault(row, 0);
                count++;
                rowCountMap.put(row, count);
            }
            int pixels = 0;
            Set<String> rowSet = rowCountMap.keySet();
            for (String rowStr : rowSet) {
                int count = rowCountMap.getOrDefault(rowStr, 0);
                if (count != N)
                    continue;
                List<Integer> columnsList = new ArrayList<Integer>();
                for (int column = 0; column < columns; column++) {
                    if (rowStr.charAt(column) == 'B')
                        columnsList.add(column);
                }
                if (columnsList.size() != N)
                    continue;
                for (int column : columnsList) {
                    int columnCount = 0;
                    for (int row = 0; row < rows; row++) {
                        if (picture[row][column] == 'B')
                            columnCount++;
                    }
                    if (columnCount == N)
                        pixels += N;
                }
            }
            return pixels;
        }
    }
    
  • Todo
    
  • class Solution(object):
      def findBlackPixel(self, picture, N):
        """
        :type picture: List[List[str]]
        :type N: int
        :rtype: int
        """
        rowSign = {}
        ans = 0
        col = {}
        row = {}
        for i in range(len(picture)):
          sign = "".join(picture[i])
          rowSign[sign] = rowSign.get(sign, 0) + 1 if sign.count("B") == N else 0
          for j in range(len(picture[0])):
            if picture[i][j] == "B":
              col[j] = col.get(j, 0) + 1
        for key in rowSign:
          if rowSign[key] == N:
            for j in range(len(key)):
              if key[j] == "B" and col.get(j, 0) == N:
                ans += N
        return ans
    
    

All Problems

All Solutions