Skip to content

Instantly share code, notes, and snippets.

@woodRock
Created August 29, 2018 04:01
Show Gist options
  • Select an option

  • Save woodRock/46f077bd8c1663571ef1a37a6493eb54 to your computer and use it in GitHub Desktop.

Select an option

Save woodRock/46f077bd8c1663571ef1a37a6493eb54 to your computer and use it in GitHub Desktop.
Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required. For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
# Sort jobs by finish times so that f1<=f2<=..<=fn
# 𝐴 ← βˆ…, π‘™π‘Žπ‘ π‘‘ ← 0
# for 𝑗 ← 1 to 𝑛
# if 𝑠𝑗 β‰₯ π‘™π‘Žπ‘ π‘‘ then 𝐴 ← 𝐴 βˆͺ {𝑗}, π‘™π‘Žπ‘ π‘‘ ← 𝑓𝑗
# return οΏ½
require('set')
def minimum_room_no(times)
times = times.sort {|a,b| a[1] <=> b[1]}
a = Set.new
last = 0
times.each do |t|
if t[0] >= last
a = a | [t].to_set()
last = t[1]
end
end
return a.length
end
times = [[30, 75], [0, 50], [60, 150]]
puts "Minimum Room Number: #{minimum_room_no(times)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment