-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2064-minimized-maximum-of-products-distributed-to-any-store.rb
72 lines (63 loc) · 2.65 KB
/
2064-minimized-maximum-of-products-distributed-to-any-store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true
# 2064. Minimized Maximum of Products Distributed to Any Store
# https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store
# Medium
=begin
You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.
You need to distribute all products to the retail stores following these rules:
* A store can only be given at most one product type but can be given any amount of it.
* After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.
Return the minimum possible x.
Example 1:
Input: n = 6, quantities = [11,6]
Output: 3
Explanation: One optimal way is:
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
Example 2:
Input: n = 7, quantities = [15,10,10]
Output: 5
Explanation: One optimal way is:
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
Example 3:
Input: n = 1, quantities = [100000]
Output: 100000
Explanation: The only optimal way is:
- The 100000 products of type 0 are distributed to the only store.
The maximum number of products given to any store is max(100000) = 100000.
Constraints:
* m == quantities.length
* 1 <= m <= n <= 105
* 1 <= quantities[i] <= 105
=end
# @param {Integer} n
# @param {Integer[]} quantities
# @return {Integer}
def minimized_maximum(n, quantities)
ok = quantities.max
ng = 0
while (ok - ng).abs > 1
mid = (ok + ng) / 2
if quantities.sum { |q| (q + mid - 1) / mid } <= n
ok = mid
else
ng = mid
end
end
ok
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_minimized_maximum < Test::Unit::TestCase
def test_
assert_equal 3, minimized_maximum(6, [11, 6])
assert_equal 5, minimized_maximum(7, [15, 10, 10])
assert_equal 100000, minimized_maximum(1, [100000])
end
end