A repair truck must travel from Riverton to Lakeside through a rectangular network of road sections.
Each road section is either clear or damaged. The truck can use a clear section immediately. A damaged section must be repaired before the truck can use it, and each repair costs one unit.
Riverton is beside the top-left section (0,0) of the map, while Lakeside is beside the bottom-right section (rows-1, columns-1). Find the minimum number of road repairs needed to travel between the cities.
The road network is provided as a List<String>. Each string represents one row and contains comma-separated road conditions:
C represents a clear road section.D represents a damaged road section.CityRoadPlanner
CityRoadPlanner()
public int findMinimumRepairs(List<String> roadMap)
roadMap contains the road sections in top-to-bottom order.rows = roadMap.size()columns road sections.1 ≤ rows, columns ≤ 100,0002 ≤ rows * columns ≤ 100,000C or D.findMinimumRepairs(roadMap = List.of("C,D,C,C,D", "D,D,C,D,C", "C,C,D,D,C", "D,C,C,D,C"))
Output: 2
The truck can repair one section near Riverton, follow the clear southern roads, and repair one more section before reaching Lakeside.
findMinimumRepairs(roadMap = List.of("C,C,D,D", "D,C,D,C", "D,C,C,C", "D,D,D,C"))
Output: 0
A route made entirely of clear road sections already connects the two cities.
findMinimumRepairs(roadMap = List.of("C,D", "D,C"))
Output: 1
Both possible routes contain one damaged section, so exactly one repair is necessary.