Skip to content

Instantly share code, notes, and snippets.

@loicginoux
Last active December 19, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicginoux/8616aeec54d85409f6e9008f42bbc651 to your computer and use it in GitHub Desktop.
Save loicginoux/8616aeec54d85409f6e9008f42bbc651 to your computer and use it in GitHub Desktop.
Daily Coding Problem: Problem #21
# 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.
def run(intervals_array)
rooms = []
intervals_array.each do |interval|
if rooms.empty?
rooms << [interval]
else
room_found = false
rooms.each do |room|
room.each do |room_interval|
if !overlapping(room_interval, interval)
room_found = room
break
end
end
if room_found
room << interval
break
end
end
if !room_found
rooms << [interval]
end
end
end
p rooms
return rooms.length
end
def overlapping(interval1, interval2)
(interval2[0] >= interval1[0] && interval2[0] <= interval1[1]) || (interval2[1] >= interval1[0] && interval2[1] <= interval1[1])
end
run([[0,2], [1, 5], [4,7], [7,8]]) == 2
run([[0,9], [1, 5], [4,7], [7,8]]) == 3
run([[30, 75], [0, 50], [60, 150]]) == 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment