Class Solution

java.lang.Object
g2701_2800.s2768_number_of_black_blocks.Solution

public class Solution extends Object
2768 - Number of Black Blocks.<p>Medium</p> <p>You are given two integers <code>m</code> and <code>n</code> representing the dimensions of a <strong>0-indexed</strong> <code>m x n</code> grid.</p> <p>You are also given a <strong>0-indexed</strong> 2D integer matrix <code>coordinates</code>, where <code>coordinates[i] = [x, y]</code> indicates that the cell with coordinates <code>[x, y]</code> is colored <strong>black</strong>. All cells in the grid that do not appear in <code>coordinates</code> are <strong>white</strong>.</p> <p>A block is defined as a <code>2 x 2</code> submatrix of the grid. More formally, a block with cell <code>[x, y]</code> as its top-left corner where <code>0 <= x < m - 1</code> and <code>0 <= y < n - 1</code> contains the coordinates <code>[x, y]</code>, <code>[x + 1, y]</code>, <code>[x, y + 1]</code>, and <code>[x + 1, y + 1]</code>.</p> <p>Return <em>a <strong>0-indexed</strong> integer array</em> <code>arr</code> <em>of size</em> <code>5</code> <em>such that</em> <code>arr[i]</code> <em>is the number of blocks that contains exactly</em> <code>i</code> <em><strong>black</strong> cells</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> m = 3, n = 3, coordinates = [[0,0]]</p> <p><strong>Output:</strong> [3,1,0,0,0]</p> <p><strong>Explanation:</strong> The grid looks like this: <img src="https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png" alt="" /></p> <p>There is only 1 block with one black cell, and it is the block starting with cell [0,0].</p> <p>The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells.</p> <p>Thus, we return [3,1,0,0,0].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]]</p> <p><strong>Output:</strong> [0,2,2,0,0]</p> <p><strong>Explanation:</strong> The grid looks like this: <img src="https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png" alt="" /></p> <p>There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]).</p> <p>The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell.</p> <p>Therefore, we return [0,2,2,0,0].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= m <= 10<sup>5</sup></code></li> <li><code>2 <= n <= 10<sup>5</sup></code></li> <li><code>0 <= coordinates.length <= 10<sup>4</sup></code></li> <li><code>coordinates[i].length == 2</code></li> <li><code>0 <= coordinates[i][0] < m</code></li> <li><code>0 <= coordinates[i][1] < n</code></li> <li>It is guaranteed that <code>coordinates</code> contains pairwise distinct coordinates.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countBlackBlocks

      public long[] countBlackBlocks(int m, int n, int[][] coordinates)