fork download
  1. # "A delivery company has n depots numbered 0 ... n-1, connected by n-1 two-way roads so that every depot can reach the HQ at depot 0 (the road network forms a tree). Every depot except the HQ has exactly ONE package that must be driven to the HQ. Each van holds up to `seats` packages, and driving a van across ONE road costs 1 litre of fuel. Vans may combine loads: when routes meet, packages can be pooled into fewer vans.
  2.  
  3. # Given the roads as pairs [a, b] and the value `seats`, return the minimum total litres of fuel needed to bring every package to depot 0.
  4.  
  5. # (1 <= n <= 10^5; roads.length == n-1; 1 <= seats <= 10^5.)
  6.  
  7. # Example 1:
  8. # Input: roads = [[0,1],[0,2],[0,3]], seats = 5
  9. # Output: 3
  10. # Explanation: Depots 1, 2, 3 each drive their package one road to depot 0: 1 + 1 + 1 = 3 litres.
  11.  
  12. # Example 2:
  13. # Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
  14. # Output: 7
  15. # Explanation: Packages pool as they move toward 0; with 2 seats per van the total works out to 7 litres.
  16.  
  17. # Example 3:
  18. # Input: roads = [], seats = 1
  19. # Output: 0
  20. # Explanation: Only the HQ exists; nothing to deliver."
  21.  
  22.  
  23.  
Success #stdin #stdout 0.13s 14096KB
stdin
Standard input is empty
stdout
Standard output is empty