# Solving Leetcode Interviews in Seconds with AI: Number of Black Blocks


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2768" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# 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 [x, y], [x + 1, y], [x, y + 1], and [x + 1, y + 1]. Return a 0-indexed integer array arr of size 5 such that arr[i] is the number of blocks that contains exactly i black cells.   Example 1:  Input: m = 3, n = 3, coordinates = [[0,0]] Output: [3,1,0,0,0] Explanation: The grid looks like this:  There is only 1 block with one black cell, and it is the block starting with cell [0,0]. The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells.  Thus, we return [3,1,0,0,0].   Example 2:  Input: m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]] Output: [0,2,2,0,0] Explanation: The grid looks like this:  There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]). The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell. Therefore, we return [0,2,2,0,0].    Constraints:  2 <= m <= 105 2 <= n <= 105 0 <= coordinates.length <= 104 coordinates[i].length == 2 0 <= coordinates[i][0] < m 0 <= coordinates[i][1] < n It is guaranteed that coordinates contains pairwise distinct coordinates.  

	# Explanation
	Here's a breakdown of the approach, complexity, and the Python code:

*   **High-Level Approach:**
    *   Use a set to efficiently store the black cell coordinates for fast lookup.
    *   Iterate through all possible top-left corners of 2x2 blocks in the grid.
    *   For each block, count the number of black cells it contains and increment the corresponding count in the `arr` array.

*   **Complexity:**
    *   Runtime Complexity: O(m\*n + k) where k is the length of coordinates matrix. In the worst-case, it will be O(m\*n).
    *   Storage Complexity: O(k), where k is the number of black cells stored in the set.

	
	# Code
	```python
	def countBlackBlocks(m: int, n: int, coordinates: list[list[int]]) -> list[int]:
    """
    Counts the number of 2x2 blocks in a grid that contain a specific number of black cells.

    Args:
        m: The number of rows in the grid.
        n: The number of columns in the grid.
        coordinates: A list of coordinates representing black cells.

    Returns:
        A list of length 5 where arr[i] is the number of blocks with i black cells.
    """

    black_cells = set()
    for x, y in coordinates:
        black_cells.add((x, y))

    arr = [0] * 5
    for i in range(m - 1):
        for j in range(n - 1):
            count = 0
            if (i, j) in black_cells:
                count += 1
            if (i + 1, j) in black_cells:
                count += 1
            if (i, j + 1) in black_cells:
                count += 1
            if (i + 1, j + 1) in black_cells:
                count += 1
            arr[count] += 1

    return arr
	```
			
