-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-MinMaxDivision.rb
60 lines (52 loc) · 1.18 KB
/
12-MinMaxDivision.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
# you can use puts for debugging purposes, e.g.
# puts "this is a debug message"
def solution(k, m, a)
from = a.max
to = a.inject(:+)
min = from
while from <= to
mid = (from + to) / 2
if check(mid, k, m, a)
min = mid
to = mid - 1
else
from = mid + 1
end
end
min
end
def check(max, k, m, a)
used_blocks = 1
block_sum = 0
a.each { |v|
block_sum += v
if block_sum > max
block_sum = v
used_blocks += 1
return false if used_blocks > k
end
}
return true
end
require "minitest/autorun"
require "byebug"
require "timeout"
class TestSolution < Minitest::Test
def test_default
assert_equal 6, solution(3, 5, [2, 1, 5, 1, 2, 2, 2])
end
def test_simple_1
assert_equal 4, solution(2, 4, [3, 1, 1, 1, 1, 1])
end
def test_zeros
assert_equal 0, solution(3, 4, [0, 0, 0, 0])
end
def test_ones
assert_equal 2, solution(2, 4, [1, 1, 1, 1])
end
def test_large
Timeout::timeout(6) {
assert_equal 1, solution(100_000, 0, [1] * 100_000)
}
end
end