Advertisement

Number of Black Blocks - LeetCode 2768 Solution

Number of Black Blocks - Complete Solution Guide

Number of Black Blocks is LeetCode problem 2768, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two integers m and n representing the dimensions of a 0-indexed m x n grid. You are also given a 0-indexed 2D integer matrix coordinates , where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black . All cells in the grid that do not appear in coordinates are white . A block is defined as a 2 x 2 submatrix of the grid. More formally, a block with cell [x, y] as its top-left corner where 0 <= x < m - 1 and 0 <= y < n - 1 contains the coordinates [

Detailed Explanation

The problem asks us to analyze a grid of size m x n where some cells are colored black based on given coordinates. We need to count the number of 2x2 blocks (submatrices) that contain exactly 0, 1, 2, 3, or 4 black cells. The input is the grid dimensions (m, n) and a list of coordinates representing the black cells. The output is an array of size 5, where each element represents the count of blocks with the corresponding number of black cells.

Solution Approach

The solution iterates through the given black cell coordinates. For each black cell at (r, c), it checks the 2x2 blocks that could potentially contain this cell. These are the blocks with top-left corners at (r-1, c-1), (r-1, c), (r, c-1), and (r, c). For each of these potential blocks, it verifies if the block is within the grid boundaries. If it is, it increments the black cell count for that block using a hash table. After processing all the black cells, it calculates the total number of blocks, counts the number of affected blocks (those in the hash table), and calculates the number of blocks with zero black cells. Finally, it iterates through the hash table, counts the number of blocks with 1 to 4 black cells, and returns the result.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (or dictionary) to store the count of black cells in each 2x2 block.
  2. Step 2: Iterate through the given coordinates of black cells.
  3. Step 3: For each black cell (r, c), iterate through the possible top-left corners of 2x2 blocks that may include it: (r-1, c-1), (r-1, c), (r, c-1), and (r, c).
  4. Step 4: For each potential top-left corner (i, j), check if it is a valid block within the grid boundaries (0 <= i < m - 1 and 0 <= j < n - 1).
  5. Step 5: If the block is valid, increment the count of black cells for that block in the hash table.
  6. Step 6: After processing all black cells, calculate the total number of possible 2x2 blocks: (m - 1) * (n - 1).
  7. Step 7: Calculate the number of blocks with 0 black cells: total blocks - number of affected blocks (size of the hash table).
  8. Step 8: Iterate through the hash table and count the blocks with 1, 2, 3, and 4 black cells.
  9. Step 9: Return the array containing the counts of blocks with 0, 1, 2, 3, and 4 black cells.

Key Insights

  • Insight 1: Instead of iterating through all possible 2x2 blocks, we only need to focus on the blocks adjacent to the given black cells. This significantly reduces the number of blocks to check.
  • Insight 2: Using a hash table (or dictionary) to store and update the counts of black cells in each affected block is crucial for efficient counting.
  • Insight 3: After counting the blocks with 1 to 4 black cells, the count of blocks with 0 black cells can be efficiently calculated by subtracting the sum of counts of blocks with 1 to 4 black cells from the total number of possible blocks (m-1)*(n-1).

Complexity Analysis

Time Complexity: O(K)

Space Complexity: O(K)

Topics

This problem involves: Array, Hash Table, Enumeration.

Companies

Asked at: Block, Capital One, SIG, Visa, X.