Skip to content

Commit 8f9e315

Browse files
chore: add LeetCode daily solution
1 parent b2f26f3 commit 8f9e315

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Maximum Running Time of N Computers (Hard)
2+
3+
**Problem ID:** 2141
4+
**Date:** 2025-12-01
5+
**Link:** https://leetcode.com/problems/maximum-running-time-of-n-computers/
6+
7+
## Approach
8+
9+
To solve the problem of determining the maximum running time for `n` computers using the given batteries, we can employ a binary search approach combined with a greedy strategy.
10+
11+
### Main Idea:
12+
The core idea is to use binary search to find the maximum time `T` for which all `n` computers can run simultaneously. For each candidate time `T`, we need to check if it is feasible to keep all computers running for that duration using the available batteries.
13+
14+
### Steps to Approach:
15+
16+
1. **Binary Search Setup**:
17+
- Set the lower bound (`low`) to 0 and the upper bound (`high`) to the sum of all battery capacities divided by `n`, which represents the maximum possible time if all batteries were perfectly utilized.
18+
19+
2. **Feasibility Check**:
20+
- For a mid-point `T` during the binary search, calculate the total energy needed to run `n` computers for `T` minutes, which is `n * T`.
21+
- Next, calculate the total energy provided by the batteries. For each battery, if its capacity is greater than `T`, it can only contribute `T` minutes to the total. Otherwise, it contributes its full capacity.
22+
- Sum these contributions to determine if the total energy from the batteries is at least `n * T`.
23+
24+
3. **Binary Search Execution**:
25+
- If the total energy from batteries is sufficient for `n * T`, then it is feasible to run the computers for `T` minutes, and we can try for a longer duration by moving the `low` pointer up.
26+
- If not, adjust the `high` pointer down to search for a shorter time.
27+
28+
4. **Termination**:
29+
- The binary search continues until `low` exceeds `high`, at which point the maximum feasible time found is the answer.
30+
31+
### Data Structures:
32+
- An array to hold the battery capacities.
33+
- Simple integer variables for managing the binary search bounds and calculations.
34+
35+
### Complexity:
36+
- The time complexity is `O(m log T)`, where `m` is the number of batteries and `T` is the maximum time being searched. The binary search runs in `O(log T)` iterations, and each feasibility check involves iterating through the batteries, which takes `O(m)` time.
37+
38+
This approach efficiently narrows down the maximum running time by leveraging the properties of binary search and greedy summation of battery contributions, ensuring that we can handle the upper limits of the input constraints effectively.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public long maxRunTime(int n, int[] batteries) {
5+
long totalPower = 0;
6+
for (int battery : batteries) {
7+
totalPower += battery;
8+
}
9+
10+
long left = 1, right = totalPower / n;
11+
12+
while (left < right) {
13+
long mid = right - (right - left) / 2;
14+
if (canRunAllComputers(mid, n, batteries)) {
15+
left = mid;
16+
} else {
17+
right = mid - 1;
18+
}
19+
}
20+
21+
return left;
22+
}
23+
24+
private boolean canRunAllComputers(long time, int n, int[] batteries) {
25+
long total = 0;
26+
for (int battery : batteries) {
27+
total += Math.min(battery, time);
28+
}
29+
return total >= time * n;
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var maxRunTime = function(n, batteries) {
2+
let left = 0, right = Math.floor(batteries.reduce((a, b) => a + b, 0) / n);
3+
4+
while (left < right) {
5+
const mid = Math.floor((left + right + 1) / 2);
6+
let total = 0;
7+
8+
for (const battery of batteries) {
9+
total += Math.min(battery, mid);
10+
}
11+
12+
if (total >= mid * n) {
13+
left = mid;
14+
} else {
15+
right = mid - 1;
16+
}
17+
}
18+
19+
return left;
20+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxRunTime(self, n: int, batteries: List[int]) -> int:
3+
total_power = sum(batteries)
4+
left, right = 1, total_power // n
5+
6+
while left < right:
7+
mid = (left + right + 1) // 2
8+
if sum(min(b, mid) for b in batteries) >= mid * n:
9+
left = mid
10+
else:
11+
right = mid - 1
12+
13+
return left

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
291291
- 2025-11-24 — [Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/) (Easy) → `Easy/2025-11-24-1018-Binary-Prefix-Divisible-By-5`
292292
- 2025-11-25 — [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) (Medium) → `Medium/2025-11-25-1015-Smallest-Integer-Divisible-by-K`
293293
- 2025-11-30 — [Make Sum Divisible by P](https://leetcode.com/problems/make-sum-divisible-by-p/) (Medium) → `Medium/2025-11-30-1590-Make-Sum-Divisible-by-P`
294+
- 2025-12-01 — [Maximum Running Time of N Computers](https://leetcode.com/problems/maximum-running-time-of-n-computers/) (Hard) → `Hard/2025-12-01-2141-Maximum-Running-Time-of-N-Computers`

0 commit comments

Comments
 (0)