AlgoMaster Logo
AlgoMasterBalance Contiguous Range Shardshard

Balance Contiguous Range Shards

hard

Range sharding must preserve key order: adjacent key buckets assigned to one shard form a contiguous range. The goal is to place range boundaries so the busiest shard is as small as possible.

Design a RangeShardBalancer class:

  • RangeShardBalancer() creates a stateless planner.
  • int minimumMaxLoad(int[] bucketLoads, int numShards) returns the smallest possible maximum shard load after splitting all buckets into exactly numShards non-empty contiguous ranges.

bucketLoads[i] is the load of the next ordered key bucket. A bucket cannot be split or reordered. The load of a shard is the sum of its buckets.

Example 1:

Input:

Output:

Explanation: [10,20,30] and [40] have loads 60 and 40. Every alternative has a maximum of at least 60.

Example 2:

Input:

Output:

Explanation: The ranges [7,2,5] and [10,8] have loads 14 and 18.

Constraints

  • 1 <= bucketLoads.length <= 10^5
  • 0 <= bucketLoads[i] <= 10^6
  • 1 <= numShards <= bucketLoads.length
  • The total bucket load fits in a signed 32-bit integer.
  • At most 100 calls are made to minimumMaxLoad.
Hints

Loading...
CallReturns
new RangeShardBalancer()null
minimumMaxLoad([10,20,30,40], 2)60

Splitting after 30 gives loads 60 and 40. No two contiguous ranges can both have maximum load below 60.

Run checks these cases. Submit also runs a larger hidden set.