481. Service Deployment Order
Service Deployment Order

A platform runs serviceCount microservices and infrastructure components, labeled from 0 to serviceCount - 1. Find an order to start all components while respecting their dependencies.

Method Signature

List<Integer> findDeploymentOrder(int serviceCount, List<String> dependencies)
  • serviceCount: total number of components.
  • dependencies: each string "A,B" means component B must be running before component A can start. Repeated pairs have the same effect as a single pair.
  • Return every component exactly once. At each step, start the component with the smallest ID among those whose dependencies are all running and which have not started yet.
  • If a dependency cycle prevents starting all components, return an empty list.

Constraints

  • 1 ≤ serviceCount ≤ 100,000
  • 0 ≤ dependencies.size() ≤ 200,000
  • Each dependency contains exactly two comma-separated integer IDs, without spaces.
  • 0 ≤ A, B < serviceCount

Examples

Example 1

findDeploymentOrder(serviceCount = 5, dependencies = List.of("0,2", "1,2", "3,0", "3,1"))

Output: [2, 0, 1, 3, 4]

Components 2 and 4 are initially ready. Starting 2 makes 0 and 1 ready. Always selecting the smallest ready ID gives the displayed order.

Example 2

findDeploymentOrder(serviceCount = 4, dependencies = List.of("0,1", "1,0", "3,2"))

Output: []

Components 0 and 1 depend on each other, so starting every component is impossible.

Example 3

findDeploymentOrder(serviceCount = 3, dependencies = List.of())

Output: [0, 1, 2]

With no dependencies, components start in increasing ID order.



Please use Laptop/Desktop or any other large screen to add/edit code.