|
| 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