Skip to content

Instantly share code, notes, and snippets.

@enderahmetyurt
Created July 7, 2025 08:56
Show Gist options
  • Save enderahmetyurt/8df3dbf587ad630f24c801bdb27c6887 to your computer and use it in GitHub Desktop.
Save enderahmetyurt/8df3dbf587ad630f24c801bdb27c6887 to your computer and use it in GitHub Desktop.
Given an array of fireworks representing a series going off, write a function to find the "grand finale" of the show!
def grand_finale_start(fireworks)
best = { start: nil, length: 0 }
(0...fireworks.size).each do |start_idx|
fireworks[start_idx..].each_with_index do |_, offset|
window = fireworks[start_idx..start_idx + offset]
next unless valid_window?(window)
if window.size > best[:length]
best[:start] = start_idx
best[:length] = window.size
end
end
end
best[:start]
end
private
def valid_window?(window)
average_size(window) >= 5 &&
window.all? { |f| f[:velocity] >= 3 } &&
height_range(window) <= 10
end
def average_size(window)
window.sum { |f| f[:size] }.to_f / window.size
end
def height_range(window)
heights = window.map { |f| f[:height] }
heights.max - heights.min
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment