Skip to content

Commit 2f36ea5

Browse files
authored
Merge pull request #90 from remy727/2141-maximum-running-time-of-n-computers
2141. Maximum Running Time of N Computers
2 parents f864fd7 + f062920 commit 2f36ea5

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
| 2130 | Maximum Twin Sum of a Linked List | [Ruby](./algorithms/ruby/2130-maximum-twin-sum-of-a-linked-list.rb) | Medium |
343343
| 2131 | Longest Palindrome by Concatenating Two Letter Words | [Ruby](./algorithms/ruby/2131-longest-palindrome-by-concatenating-two-letter-words.rb) | Medium |
344344
| 2140 | Solving Questions With Brainpower | [Ruby](./algorithms/ruby/2140-solving-questions-with-brainpower.rb) | Medium |
345+
| 2141 | Maximum Running Time of N Computers | [Ruby](./algorithms/ruby/2141-maximum-running-time-of-n-computers.rb) | Hard |
345346
| 2187 | Minimum Time to Complete Trips | [Ruby](./algorithms/ruby/2187-minimum-time-to-complete-trips.rb) [Python3](./algorithms/python3/2187-minimum-time-to-complete-trips.py) | Medium |
346347
| 2215 | Find the Difference of Two Arrays | [Ruby](./algorithms/ruby/2215-find-the-difference-of-two-arrays.rb) | Easy |
347348
| 2218 | Maximum Value of K Coins From Piles | [Ruby](./algorithms/ruby/2218-maximum-value-of-k-coins-from-piles.rb) | Hard |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
# 2141. Maximum Running Time of N Computers
4+
# Hard
5+
# https://leetcode.com/problems/maximum-running-time-of-n-computers
6+
7+
=begin
8+
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.
9+
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.
10+
Note that the batteries cannot be recharged.
11+
Return the maximum number of minutes you can run all the n computers simultaneously.
12+
13+
Example 1:
14+
Input: n = 2, batteries = [3,3,3]
15+
Output: 4
16+
Explanation:
17+
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
18+
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.
19+
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.
20+
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
21+
We can run the two computers simultaneously for at most 4 minutes, so we return 4.
22+
23+
Example 2:
24+
Input: n = 2, batteries = [1,1,1,1]
25+
Output: 2
26+
Explanation:
27+
Initially, insert battery 0 into the first computer and battery 2 into the second computer.
28+
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.
29+
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
30+
We can run the two computers simultaneously for at most 2 minutes, so we return 2.
31+
32+
Constraints:
33+
* 1 <= n <= batteries.length <= 105
34+
* 1 <= batteries[i] <= 109
35+
=end
36+
37+
# @param {Integer} n
38+
# @param {Integer[]} batteries
39+
# @return {Integer}
40+
def max_run_time(n, batteries)
41+
batteries.sort!
42+
43+
extra = batteries[0...-n].sum
44+
45+
live = batteries[-n..]
46+
47+
for i in (0...n - 1)
48+
if extra / (i + 1) < live[i + 1] - live[i]
49+
return live[i] + extra / (i + 1)
50+
end
51+
52+
extra -= (i + 1) * (live[i + 1] - live[i])
53+
end
54+
55+
live[-1] + extra / n
56+
end
57+
58+
# **************** #
59+
# TEST #
60+
# **************** #
61+
62+
require "test/unit"
63+
class Test_max_run_time < Test::Unit::TestCase
64+
def test_
65+
assert_equal 4, max_run_time(2, [3, 3, 3])
66+
assert_equal 2, max_run_time(2, [1, 1, 1, 1])
67+
end
68+
end

0 commit comments

Comments
 (0)