Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def solution(a, d)
- return 0 if d > a.length
- position = -1
- time = 0
- max_time = 0
- length = a.size
- while true do
- start_position = position + 1
- end_position = position + d
- new_position = -2
- while new_position != position
- return time if end_position >= length
- new_position = position
- unreach_count = 0
- end_position.downto(start_position) do |index|
- max_time = [max_time, a[index]].max
- if a[index] == -1
- unreach_count += 1
- return -1 if unreach_count >= d
- end
- if a[index] <= time && a[index] != -1
- position = index
- break
- end
- end
- start_position = position + 1
- end_position = [position + d, length - 1].min
- end
- if position + d + 1 > length
- return [max_time, time].min
- end
- time += 1
- return [max_time, time].min if time > length
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement