Skip to content

Commit c2a5218

Browse files
committed
2767
1 parent 53e48e5 commit c2a5218

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# @param {String} s
2+
# @return {Integer}
3+
def minimum_beautiful_substrings(s)
4+
return -1 if s[0] == '0' || s[-1] == '0'
5+
6+
is_power_of_five = lambda { |num|
7+
while num > 1 && (num % 5 == 0)
8+
num /= 5
9+
end
10+
11+
num == 1
12+
}
13+
14+
dp = Array.new(s.length+1, Float::INFINITY)
15+
dp[s.length] = 0
16+
17+
(s.length-1).downto(0) { |i|
18+
next if s[i] == '0'
19+
(i...s.length).each { |j|
20+
if is_power_of_five.call(s[i..j].to_i(2))
21+
dp[i] = [dp[i], 1 + dp[j+1]].min
22+
end
23+
}
24+
}
25+
26+
dp[0] == Float::INFINITY ? -1 : dp[0]
27+
end

0 commit comments

Comments
 (0)