You are given two integers m and n representing the dimensions of a
0-indexedm 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 a0-indexed integer arrayarrof size5such thatarr[i]is the number of blocks that contains exactlyiblack cells.
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].
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].
Each black cell affects up to 4 blocks (top-left corners). For each black cell, increment the count for each block it belongs to. Count blocks by number of black cells.
#include<vector>#include<unordered_map>usingnamespace std;
classSolution {
public: vector<longlong> countBlackBlocks(int m, int n, vector<vector<int>>& coordinates) {
unordered_map<longlong, int> cnt;
for (auto& c : coordinates) {
int x = c[0], y = c[1];
for (int dx =0; dx <=1; ++dx)
for (int dy =0; dy <=1; ++dy) {
int i = x-dx, j = y-dy;
if (i >=0&& j >=0&& i < m-1&& j < n-1)
cnt[(longlong)i*n+j]++;
}
}
vector<longlong> res(5);
for (auto& [_, v] : cnt) res[v]++;
res[0] = (longlong)(m-1)*(n-1) - cnt.size();
return res;
}
};
import java.util.*;
classSolution {
publiclong[]countBlackBlocks(int m, int n, int[][] coordinates) {
Map<Long, Integer> cnt =new HashMap<>();
for (int[] c : coordinates) {
int x = c[0], y = c[1];
for (int dx = 0; dx <= 1; ++dx)
for (int dy = 0; dy <= 1; ++dy) {
int i = x-dx, j = y-dy;
if (i >= 0 && j >= 0 && i < m-1 && j < n-1)
cnt.put((long)i*n+j, cnt.getOrDefault((long)i*n+j, 0)+1);
}
}
long[] res =newlong[5];
for (int v : cnt.values()) res[v]++;
res[0]= (long)(m-1)*(n-1) - cnt.size();
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funcountBlackBlocks(m: Int, n: Int, coordinates: Array<IntArray>): LongArray {
val cnt = mutableMapOf<Long, Int>()
for (c in coordinates) {
val x = c[0]; val y = c[1]
for (dx in0..1) for (dy in0..1) {
val i = x-dx; val j = y-dy
if (i >=0&& j >=0&& i < m-1&& j < n-1)
cnt[i.toLong()*n+j] = cnt.getOrDefault(i.toLong()*n+j, 0)+1 }
}
val res = LongArray(5)
for (v in cnt.values) res[v]++ res[0] = (m-1).toLong()*(n-1) - cnt.size
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcountBlackBlocks(self, m: int, n: int, coordinates: List[List[int]]) -> List[int]:
from collections import Counter
cnt = Counter()
for x, y in coordinates:
for dx in range(2):
for dy in range(2):
i, j = x-dx, y-dy
if0<= i < m-1and0<= j < n-1:
cnt[i*n+j] +=1 res = [0]*5for v in cnt.values(): res[v] +=1 res[0] = (m-1)*(n-1) - len(cnt)
return res
use std::collections::HashMap;
impl Solution {
pubfncount_black_blocks(m: i32, n: i32, coordinates: Vec<Vec<i32>>) -> Vec<i64> {
letmut cnt = HashMap::new();
for c in coordinates {
let x = c[0]; let y = c[1];
for dx in0..=1 {
for dy in0..=1 {
let i = x-dx; let j = y-dy;
if i >=0&& j >=0&& i < m-1&& j < n-1 {
let key = (i asi64)*n asi64+ j asi64;
*cnt.entry(key).or_insert(0) +=1;
}
}
}
}
letmut res =vec![0i64; 5];
for v in cnt.values() { res[*v asusize] +=1; }
res[0] = (m asi64-1)*(n asi64-1) - cnt.len() asi64;
res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
functioncountBlackBlocks(m: number, n: number, coordinates: number[][]):number[] {
constcnt=newMap<number, number>();
for (const [x, y] ofcoordinates) {
for (letdx=0; dx<=1; ++dx)
for (letdy=0; dy<=1; ++dy) {
consti=x-dx, j=y-dy;
if (i>=0&&j>=0&&i<m-1&&j<n-1) {
constkey=i*n+j;
cnt.set(key, (cnt.get(key)||0)+1);
}
}
}
constres= Array(5).fill(0);
for (constvofcnt.values()) res[v]++;
res[0] = (m-1)*(n-1) -cnt.size;
returnres;
}