FastPrepCount Groups of Diseased Plants

Count Groups of Diseased Plants

Yellow.ai logoYellow.ai● MediumFULLTIMEPHONE SCREEN

Problem statement

The rectangular grid grid contains healthy plants, H, and diseased plants, D. Return the number of people needed when one person handles each connected group of diseased plants.

Diseased cells are connected through shared horizontal or vertical edges. Diagonal contact does not connect groups. An empty grid or a grid containing no diseased plants needs 0 people.

Function

countDiseasedGroups(grid: char[][]) → int

Examples

Example 1

grid = [["H","H","D","D","H"],["H","H","D","H","H"],["D","H","H","H","D"],["D","D","H","H","D"],["H","H","H","D","H"]]return = 4

The four groups have 3, 3, 2, and 1 diseased cells. The bottom-row diseased cell touches the right-side group only diagonally, so it needs a separate person.

Example 2

grid = [["D","H"],["H","D"]]return = 2

The two diseased cells share no horizontal or vertical edge, so two people are needed.

Constraints

  • 0 <= grid.length <= 150.
  • Every row has the same number of cells, between 0 and 150 inclusive.
  • Every cell is H or D.

More Yellow.ai problems

See Yellow.ai hiring insights
Saved
public int countDiseasedGroups(char[][] grid) {
    // Write your code here.
}
Checking account…