Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bc30ef9

Browse files
committed
Add solution #2141
1 parent b048eeb commit bc30ef9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,773 LeetCode solutions in JavaScript
1+
# 1,774 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1641,6 +1641,7 @@
16411641
2138|[Divide a String Into Groups of Size k](./solutions/2138-divide-a-string-into-groups-of-size-k.js)|Easy|
16421642
2139|[Minimum Moves to Reach Target Score](./solutions/2139-minimum-moves-to-reach-target-score.js)|Medium|
16431643
2140|[Solving Questions With Brainpower](./solutions/2140-solving-questions-with-brainpower.js)|Medium|
1644+
2141|[Maximum Running Time of N Computers](./solutions/2141-maximum-running-time-of-n-computers.js)|Hard|
16441645
2145|[Count the Hidden Sequences](./solutions/2145-count-the-hidden-sequences.js)|Medium|
16451646
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
16461647
2161|[Partition Array According to Given Pivot](./solutions/2161-partition-array-according-to-given-pivot.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 2141. Maximum Running Time of N Computers
3+
* https://leetcode.com/problems/maximum-running-time-of-n-computers/
4+
* Difficulty: Hard
5+
*
6+
* You have n computers. You are given the integer n and a 0-indexed integer array batteries
7+
* where the ith battery can run a computer for batteries[i] minutes. You are interested in
8+
* running all n computers simultaneously using the given batteries.
9+
*
10+
* Initially, you can insert at most one battery into each computer. After that and at any
11+
* integer time moment, you can remove a battery from a computer and insert another battery
12+
* any number of times. The inserted battery can be a totally new battery or a battery from
13+
* another computer. You may assume that the removing and inserting processes take no time.
14+
*
15+
* Note that the batteries cannot be recharged.
16+
*
17+
* Return the maximum number of minutes you can run all the n computers simultaneously.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
* @param {number[]} batteries
23+
* @return {number}
24+
*/
25+
var maxRunTime = function(n, batteries) {
26+
let left = 1;
27+
let right = Math.floor(batteries.reduce((sum, battery) => sum + battery, 0) / n);
28+
29+
while (left < right) {
30+
const mid = Math.floor((left + right + 1) / 2);
31+
const total = batteries.reduce((sum, battery) => sum + Math.min(battery, mid), 0);
32+
33+
if (total >= n * mid) {
34+
left = mid;
35+
} else {
36+
right = mid - 1;
37+
}
38+
}
39+
40+
return left;
41+
};

0 commit comments

Comments
 (0)