-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-FibFrog.rb
83 lines (69 loc) · 1.71 KB
/
11-FibFrog.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
73
74
75
76
77
78
79
80
81
82
83
def solution(a)
n = a.count
return 1 if n == 0
fb = fibonacci_to(n + 1)
fb.shift(2)
dp = [nil] * (n + 1)
dp[0] = 0
for i in (1..n + 1)
next if a[i - 1] == 0
for f in fb
break if i - f < 0
if dp[i - f]
dp[i] = if dp[i]
[dp[i], dp[i - f] + 1].min
else
dp[i - f] + 1
end
end
end
end
# puts a.inspect
# puts fb.inspect
# puts dp.inspect
dp[n + 1] || -1
end
def fibonacci_to(max)
f = [0, 1]
i = 1
while f[i] <= max
i += 1
f[i] = f[i - 2] + f[i - 1]
end
f
end
require "minitest/autorun"
require "byebug"
require "timeout"
# byebug
# solution(nil)
# exit
class TestSolution < Minitest::Test
def test_default
assert_equal 3, solution([0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0] )
end
def test_simple
assert_equal 1, solution([0])
assert_equal 1, solution([0, 0])
assert_equal -1, solution([0, 0, 0])
assert_equal 2, solution([1, 0, 0])
assert_equal 2, solution([0, 1, 0])
assert_equal 2, solution([0, 0, 1])
assert_equal 1, solution([0, 0, 1, 0])
assert_equal 2, solution([0, 0, 1, 0, 0])
assert_equal -1, solution([0, 0, 1, 0, 0, 0])
assert_equal 3, solution([0, 0, 1, 0, 0, 1])
assert_equal 1, solution([0, 0, 1, 0, 0, 0, 0])
end
def test_small
assert_equal 1, solution([])
end
def test_large
Timeout::timeout(6) {
assert_equal -1, solution([0] * 100_000 )
}
Timeout::timeout(6) {
assert_equal 6, solution([1] * 100_000 )
}
end
end