Inputs - [[0, 1], [3, 5], [4, 8], [10, 12], [9, 10]] Output - [[0, 1], [3, 8], [9, 12]]
Logic.
def meeting_times(meetings) meetings = meetings.sort answer = [meetings[0]] meetings[1..-1].each do |starting_time, ending_time| previous_starting_time, previous_ending_time = answer[-1] if starting_time > previous_ending_time answer << [starting_time, ending_time] else answer[-1] = [previous_starting_time, [ending_time, previous_ending_time].max] end end answer end
Nice Ruby hack: If you have an array e.g. players = ["pele", "jordan", "sachin"]
players = ["pele", "jordan", "sachin"]
Then you can assign each of these to 3 different variables with this one line: football, basketball, cricket = players
football, basketball, cricket = players
so you get 3 variables: football = pele basketball = jordan cricket = sachin
football = pele
basketball = jordan
cricket = sachin
Easier than doing players[0], players[1] and so on..
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Inputs - [[0, 1], [3, 5], [4, 8], [10, 12], [9, 10]]
Output - [[0, 1], [3, 8], [9, 12]]
Logic.
Nice Ruby hack: If you have an array e.g.
players = ["pele", "jordan", "sachin"]
Then you can assign each of these to 3 different variables with this one line:
football, basketball, cricket = players
so you get 3 variables:
football = pele
basketball = jordan
cricket = sachin
Easier than doing players[0], players[1] and so on..