FastPrepStable Workload Redistribution Trees
Problem · Tree

Stable Workload Redistribution Trees

Learn this problem
HardTower Research Capital logoTower Research CapitalNEW GRADOA

Problem statement

You are given an integer array workloads of length n. Machine i is labeled i + 1 and initially holds workloads[i] units. The total workload is exactly n.

Choose a communication network that is a tree on the n labeled machines. After the tree is fixed, you may repeatedly choose two machines whose shortest-path distance in the tree is exactly 2 and move one workload unit from either chosen machine to the other.

A labeled tree is stable when some sequence of these moves leaves every machine with exactly one unit. Return the number of distinct stable labeled trees modulo 1000000007.

Function

countStableTrees(workloads: int[]) → int

Examples

Example 1

workloads = [1]return = 1

There is one labeled tree on one machine, and its workload is already one.

Example 2

workloads = [0,1,2]return = 1

The middle-workload machine must be alone on one side of the tree bipartition. This fixes the only stable tree: it is adjacent to both other machines.

Example 3

workloads = [0,0,2,2]return = 8

Each valid bipartition side contains one zero-workload and one two-workload machine. There are two unordered valid bipartitions, and each supports four labeled spanning trees.

Constraints

  • 1 <= workloads.length <= 120; this length is n.
  • 0 <= workloads[i] <= n.
  • The sum of all values in workloads is exactly n.
drafts saved locally
public int countStableTrees(int[] workloads) {
    // TODO: return the number of stable labeled trees modulo 1_000_000_007.
}
workloads[1]
expected1
checking account