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 a 0-indexed integer arrayarrof size5such thatarr[i]is the number of blocks that contains exactlyi*black cells*.
classSolution { public: vector<longlong> countBlackBlocks(int m, int n, vector<vector<int>>& coordinates){ vector<longlong> res(5); res[0] = 1ll * (m - 1) * (n - 1); set<pair<longlong, longlong>> st; set<pair<longlong, longlong>> blk; auto push = [&](int x, int y) { if(0 <= x and x < m - 1and0 <= y and y < n - 1) { st.insert({x,y}); } }; for(auto c : coordinates) { int x = c[0], y = c[1]; push(x-1,y-1); push(x-1,y); push(x,y-1); push(x,y); blk.insert({x,y}); } int dy[4]{0,1,0,1}, dx[4]{0,0,1,1}; for(auto& [x,y] : st) { int cnt = 0; for(int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if(blk.count({nx,ny})) cnt += 1; } res[0] -= 1; res[cnt] += 1; } return res; } };