Last active
August 23, 2018 05:16
-
-
Save jasonjho/ff45a1fd8b87b549dad174d2419b2e2c to your computer and use it in GitHub Desktop.
This file contains 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
class Interval | |
attr_accessor :start, :end | |
def initialize(start, _end) | |
@start = start | |
@end = _end | |
end | |
end | |
def uncovered(intervals) | |
sorted = intervals.sort_by {|i| i.start} | |
results = [sorted.first] | |
sorted.each_cons(2) { |x, y| | |
target = results[-1] | |
results << y if not y.start.between?(target.start, target.end) | |
results[-1].end = y.end if not y.end.between?(target.start, target.end) | |
} | |
uncovered = [] | |
results.inject { |x, y| | |
uncovered << Interval.new(x.end, y.start) | |
y | |
} | |
uncovered | |
end | |
def main | |
intervals = [Interval.new(2, 6), Interval.new(9, 12), Interval.new(8, 9), Interval.new(18, 21), Interval.new(4, 7), Interval.new(10, 11)] | |
uncovered(intervals).each { |interval| p "#{interval.start}, #{interval.end}" } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment