FastPrepMinimum Cleaning Robot Runs
Problem · Matrix

Minimum Cleaning Robot Runs

Learn this problem
MediumHSBC logoHSBCNEW GRADOA

Problem statement

A building plan is represented by a rectangular array of strings. Each field contains one of three symbols:

  • . is clean floor.
  • * is dirty floor.
  • # is a wall.

A room is a maximal group of non-wall fields connected in the four cardinal directions. In one run, a cleaning robot starts in one room and cleans every dirty field in that room. It cannot cross a wall or leave the plan.

Return the minimum number of robot runs needed to clean every dirty field.

Function

solution(plan: String[]) → int

Examples

Example 1

plan = ["..#..",".*#*.","..#.."]return = 2

The vertical wall separates the floor into two rooms. Each room contains a dirty field, so the robot needs one run in each room.

Example 2

plan = ["....",".##.",".*#.","...."]return = 1

All non-wall fields are connected around the wall block. The single dirty field is therefore cleaned in one run.

Example 3

plan = ["###","#.#","###"]return = 0

The only floor field is already clean, so no robot run is required.

Constraints

  • 1 <= plan.length and every row has the same positive length.
  • plan.length * plan[0].length <= 200000.
  • Every character is ., *, or #.

More HSBC problems

drafts saved locally
public int solution(String[] plan) {
    // Write your code here.
}
plan["..#..",".*#*.","..#.."]
expected2
checking account