Count Safe Pins
At Pinterest, maintaining a healthy and safe content ecosystem is paramount. Boards are curated collections of pins, and sometimes, certain pins might be flagged due to content policy violations. These flagged pins can potentially compromise other nearby pins, requiring a careful moderation process. This problem asks you to simulate a simplified content scan to identify and count 'safe' pins.
Implement a function countSafePins that takes a 2D integer matrix, board, representing a Pinterest board. Your task is to count the total number of 'safe' pins on this board.
A pin is defined as follows:
1 represents a regular pin.0 represents an empty space.-1 represents a flagged (problematic) pin.A regular pin (1) is considered compromised if it is directly adjacent (horizontally or vertically, not diagonally) to any flagged pin (-1). A regular pin is considered safe if it is not compromised.
board (number[][]) - A 2D array of integers representing the Pinterest board. Each element board[r][c] will be 0, 1, or -1.number - The total count of safe pins on the board.Examples
Example 1
Input:
board = [[1, 1, 1], [1, -1, 1], [1, 1, 1]]
Output:
4
Note:
The flagged pin at (1,1) compromises its direct neighbors: (0,1), (1,0), (1,2), and (2,1). The pins at (0,0), (0,2), (2,0), and (2,2) are regular pins not adjacent to any flagged pin, hence they are safe.
Example 2
Input:
board = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
Output:
4
Note:
There are no flagged pins (-1) on the board. Therefore, all regular pins (1) at (0,0), (0,2), (2,0), and (2,2) are considered safe.
Example 3
Input:
board = [[-1, 1], [1, 1]]
Output:
1
Note:
The flagged pin at (0,0) compromises its direct neighbors: (0,1) and (1,0). The pin at (1,1) is a regular pin and is not adjacent to the flagged pin (0,0) (it's diagonal), so it is safe.
Constraints
1 <= board.length <= 1001 <= board[0].length <= 100board[r][c] will be 0, 1, or -1.Loading editor...