Problem · Tree

Minimum Satellite Data Transfer Iterations

Learn this problem
HardHSBC logoHSBCINTERNOA

Problem statement

A space organization has numSatellite satellites with IDs from 0 to numSatellite - 1. Satellite 0 initially has a data packet that must reach every satellite.

The directed pairs in connections form a tree rooted at satellite 0. For each pair [sender, receiver], sender may transfer the data directly to receiver.

Transfer model

  • During one iteration, every satellite that had the data at the start of that iteration may transfer it to at most one direct child that does not yet have it.
  • All transfers selected for an iteration finish together at the end of that iteration.
  • A satellite that receives the data may begin forwarding it in the next iteration.
  • A satellite may send again in later iterations until all of its direct children have received the data.
  • Each satellite has at most maxSatellites direct children.

You may choose the order in which every satellite contacts its children. Return the minimum number of iterations needed for all satellites to receive the data.

Function

minimumDataTransferIterations(numSatellite: int, connections: int[][], maxSatellites: int) → int

Examples

Example 1

numSatellite = 2connections = [[0,1]]maxSatellites = 1return = 1

Satellite 0 transfers the data to satellite 1 in the first iteration.

Example 2

numSatellite = 6connections = [[0,1],[0,2],[1,3],[1,4],[3,5]]maxSatellites = 2return = 3

One optimal schedule is:

  1. Satellite 0 sends to 1.
  2. Satellite 0 sends to 2, while 1 sends to 3.
  3. Satellite 1 sends to 4, while 3 sends to 5.

All satellites have the data after three iterations.

Example 3

numSatellite = 5connections = [[0,1],[0,2],[0,3],[0,4]]maxSatellites = 4return = 4

Satellite 0 can transfer to only one child per iteration. It therefore needs four iterations to contact all four children.

Constraints

  • 2 <= numSatellite <= 10^4
  • connections.length == numSatellite - 1
  • Every connection is a two-element array [sender, receiver] with distinct IDs in the range 0 through numSatellite - 1.
  • The directed connections form a tree rooted at satellite 0, so every other satellite is reachable exactly once.
  • 1 <= maxSatellites < numSatellite
  • Every satellite has at most maxSatellites direct children.

More HSBC problems

drafts saved locally
public int minimumDataTransferIterations(int numSatellite, int[][] connections, int maxSatellites) {
  // write your code here
}
numSatellite2
connections[[0,1]]
maxSatellites1
expected1
checking account