Skip to content

Commit

Permalink
Create 0853-Car-Fleet.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
voski authored Oct 18, 2022
1 parent 14409d1 commit 2b45ae3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ruby/0853-Car-Fleet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @param {Integer} target
# @param {Integer[]} position
# @param {Integer[]} speed
# @return {Integer}
def car_fleet(target, position, speed)
cars = position.zip(speed).sort_by(&:first)

stack = []
cars.each do |car|
position, speed = car
time = (target - position) / speed.to_f
while !stack.empty? && stack[-1] <= time
prev = stack.pop
end
stack << time
end

stack.length
end

0 comments on commit 2b45ae3

Please sign in to comment.