A factory has several machines that need to be started. Some machines can start only after specific other machines are already running.
Determine whether every machine can be started while satisfying all startup requirements.
FactoryStartupPlanner
boolean canStartAllMachines( int machineCount, List<String> startupRequirements )
machineCount: The number of machines. Machines are numbered from 0 to machineCount - 1.startupRequirements: Each element has the format "machine,requiredMachine", meaning that requiredMachine must be running before machine can start.Return true if all machines can be started. Return false if the startup requirements make this impossible.
startupRequirements has the format "machine,requiredMachine"."4,2" means machine 2 must be running before machine 4 can start.1 ≤ machineCount ≤ 2,0000 ≤ startupRequirements.size() ≤ 5,0000 ≤ machine < machineCount0 ≤ requiredMachine < machineCount canStartAllMachines( machineCount = 6, startupRequirements = ["2,0", "2,1", "3,2", "5,4"] )
Output: true
Machines 0, 1, and 4 can start first. This allows the remaining machines to start afterward.
canStartAllMachines( machineCount = 5, startupRequirements = ["1,0", "3,1", "0,3", "4,2"] )
Output: false
Machines 0, 1, and 3 wait for one another in a circular chain, so they cannot be started.
canStartAllMachines( machineCount = 3, startupRequirements = [] )
Output: true
None of the machines has a startup requirement, so every machine can start immediately.
canStartAllMachines( machineCount = 7, startupRequirements = [ "1,0", "2,0", "4,3", "5,3", "6,4", "6,5" ] )
Output: true
The requirements form separate startup groups without any circular dependency, so all seven machines can eventually start.