You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries.
Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.
Note that the batteries cannot be recharged.
Return the maximum number of minutes you can run all the n computers simultaneously.
Example 1:
Input: n = 2, batteries = [3,3,3] Output: 4 Explanation: Initially, insert battery 0 into the first computer and battery 1 into the second computer. After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute. At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead. By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running. We can run the two computers simultaneously for at most 4 minutes, so we return 4.
Example 2:
Input: n = 2, batteries = [1,1,1,1] Output: 2 Explanation: Initially, insert battery 0 into the first computer and battery 2 into the second computer. After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer. After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running. We can run the two computers simultaneously for at most 2 minutes, so we return 2.
Constraints:
1 <= n <= batteries.length <= 1051 <= batteries[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the maximum running time, we will explore every possible amount of time we can run the computers. We will check if each possible time is achievable by trying out all combinations of batteries.
Here's how the algorithm would work step-by-step:
def max_running_time_brute_force(num_computers, batteries):
total_battery_power = sum(batteries)
# Binary search for the maximum possible running time
low_time = 1
high_time = total_battery_power // num_computers
max_possible_time = 0
while low_time <= high_time:
mid_time = (low_time + high_time) // 2
# Check if the mid_time is a possible running time
possible = is_running_time_possible(num_computers, batteries, mid_time)
# If the mid_time is possible, try a higher time
if possible:
max_possible_time = mid_time
low_time = mid_time + 1
# If the mid_time is not possible, try a lower time
else:
high_time = mid_time - 1
return max_possible_time
def is_running_time_possible(num_computers, batteries, running_time):
batteries.sort(reverse=True)
# We only need to check the 'n' largest batteries
usable_batteries = batteries[:num_computers]
total_power = 0
for battery in usable_batteries:
total_power += min(battery, running_time)
#Checking if the batteries can actually provide the power for the running time
return total_power >= num_computers * running_time
The most efficient way to solve this problem is by figuring out the highest possible running time and then checking if we can actually achieve it. We do this by focusing on using the smallest batteries effectively to reach that target time.
Here's how the algorithm would work step-by-step:
def max_running_time(number_of_computers, battery_power):
total_battery_power = sum(battery_power)
max_possible_running_time = total_battery_power // number_of_computers
battery_power.sort()
# We must assess if the batteries realistically support this time
while battery_power and battery_power[-1] > max_possible_running_time:
total_battery_power -= battery_power.pop()
number_of_computers -= 1
# Recalculate the max possible running time based on updated parameters
max_possible_running_time = total_battery_power // number_of_computers
# Batteries can achieve this target
return max_possible_running_time| Case | How to Handle |
|---|---|
| batteries is null or empty | Return 0 if batteries is null or empty as no running time is possible. |
| n is 0 | Return 0 if n is 0 since there are no computers to run. |
| batteries array size is less than n | Return 0 as there are not enough batteries to power all computers. |
| All battery values are 0 | Return 0 as no computer can run with batteries having 0 capacity. |
| Large battery values leading to potential integer overflow when calculating sum | Use long data type to store intermediate sums and results to avoid overflow. |
| n is very large, close to the batteries array size | The binary search should still work efficiently as it narrows the search range quickly, without requiring special adjustments for nearly equal sizes. |
| batteries array contains duplicate values | Duplicates are handled correctly by the algorithm as the binary search finds the maximum possible running time regardless of duplicate battery values. |
| batteries array contains a single very large battery and n is a small number | The algorithm correctly identifies the maximum running time as the average of available power divided across the computers. |