From 6d258ca1e9ecd4f9ef6f6aead0216ae930902b8c Mon Sep 17 00:00:00 2001 From: Soap Date: Wed, 28 Aug 2019 20:27:45 -0700 Subject: [PATCH] [Sort] Refactor solution to Meeting Rooms II --- Sort/MeetingRoomsII.swift | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Sort/MeetingRoomsII.swift b/Sort/MeetingRoomsII.swift index d098e4fe..5513c40e 100644 --- a/Sort/MeetingRoomsII.swift +++ b/Sort/MeetingRoomsII.swift @@ -15,21 +15,23 @@ */ class MeetingRoomsII { - func minMeetingRooms(_ intervals: [Interval]) -> Int { - let starts = intervals.map { interval in interval.start }.sorted() - let ends = intervals.map { interval in interval.end }.sorted() - var i = 0, j = 0, count = 0 + func minMeetingRooms(_ intervals: [[Int]]) -> Int { + let startingTimes = intervals.map { interval in interval[0] }.sorted() + let endingTimes = intervals.map { interval in interval[1] }.sorted() + let intervalsCount = intervals.count - while i < starts.count && j < ends.count { - if starts[i] < ends[j] { - count += 1 - i += 1 + var i = 0, j = 0, meetingRoomsNum = 0 + + while i < intervalsCount && j < intervalsCount { + if startingTimes[i] < endingTimes[j] { + meetingRoomsNum += 1 } else { - i += 1 j += 1 } - } + + i += 1 + } - return count + return meetingRoomsNum } -} \ No newline at end of file +}