Advertisement
Loriowar

Monkey and river

May 30th, 2020
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.46 KB | None | 0 0
  1. def solution(a, d)
  2.   return 0 if a.size < d
  3.  
  4.   length = a.size
  5.   position = 0
  6.  
  7.   (1..(length * 2)).each do |time|
  8.     allowed_raduis_data = a[position..([position+d-1, length].min)]
  9.     max_jump_position = allowed_raduis_data.reverse.find_index{|el| el != -1 && (el-time) <= 0}
  10.     next if max_jump_position.nil?
  11.  
  12.     position += allowed_raduis_data.size - max_jump_position
  13.  
  14.     return [time, length, a.max].min if length - position < d
  15.   end
  16.  
  17.   -1
  18. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement