173. Maximum Power Assigned To Machines

Asked in

Maximum Power Assigned To Machines
You are given a list of power sources, where each source has a certain amount of power.

You are also given a number machineCount, representing the number of machines that need to receive power.

Each machine must be assigned power from exactly one power source. A machine cannot combine power from multiple sources.

A single power source can distribute its power to multiple machines, as long as the total power allocated from that source does not exceed its capacity.

Your task is to find the maximum possible value of the minimum power assigned to any machine.

In other words, choose the largest integer x such that it is possible to assign at least x power to every machine.

Class

PowerSourceAllocator

Method Signature

int maximumMinimumPower(List<Integer> sources, int machineCount)
  • sources is a list where sources[i] represents the total power capacity of the i-th power source.
  • machineCount is the total number of machines that must receive power.
  • Each machine must receive power from exactly one source.
  • A machine cannot receive combined power from multiple sources.
  • One source may power multiple machines.
  • The total power assigned from one source must not exceed that source's capacity.
  • The method returns the maximum possible minimum power that can be assigned to every machine.

Power Assignment Rules

For a chosen value x, a source with capacity p can power:
p / x machines using integer division.

The value x is feasible if x > 0 and
total number of machines that can be powered across all sources is at least machineCount.

The answer is the largest feasible value of x.

Constraints

  • 1 ≤ sources.size() ≤ 100,000
  • 1 ≤ sources[i] ≤ 100,000,000
  • 1 ≤ machineCount ≤ 100,000,000

Examples

Example 1

maximumMinimumPower(sources = [5, 8, 6], machineCount = 4)
Output: 4

Explanation:
If every machine gets power 4:
  • Source 5 can power 1 machine.
  • Source 8 can power 2 machines.
  • Source 6 can power 1 machine.
Total machines powered = 1 + 2 + 1 = 4.

Power 5 is not possible because the sources can power only:
5 / 5 + 8 / 5 + 6 / 5 = 1 + 1 + 1 = 3 machines.

Therefore, the answer is 4.

Example 2

maximumMinimumPower(sources = [10, 10, 10], machineCount = 5)
Output: 5

Explanation:
Each source can power 2 machines with power 5.
Total machines powered = 2 + 2 + 2 = 6, which is enough.

Power 6 is not possible because each source can power only 1 machine with power 6.
Total machines powered = 3, which is less than 5.

Example 3

maximumMinimumPower(sources = [2, 3, 4], machineCount = 5)
Output: 1

Explanation:
Power 2 can power:
2 / 2 + 3 / 2 + 4 / 2 = 1 + 1 + 2 = 4 machines.

This is not enough for 5 machines.
Power 1 can power all 5 machines, so the answer is 1.

Example 4

maximumMinimumPower(sources = [1, 2, 3], machineCount = 10)
Output: 0

Explanation:
The total available power is only 6.
Since there are 10 machines, it is impossible to give every machine at least 1 power.
Therefore, the maximum possible minimum power is 0.




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