-
-
Save neilljordan/ad6213186225798080802aa3d2a4c02b 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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