From a20b4289614126dc1eb8ede29e5789f9b2438fd0 Mon Sep 17 00:00:00 2001 From: Godfrain Jacques Date: Sun, 24 Nov 2024 21:57:10 -0800 Subject: [PATCH] Update min-meeting-room.js Improve the implementation of a merge Interval. Tested with ```go ... func main() { rooms := minMeetingRooms([]Interval{ Interval{ start: 0, end: 5, }, Interval{ start: 4, end: 7, }, Interval{ start: 2, end: 10, }, Interval{ start: 4, end: 3, }, Interval{ start: 9, end: 19, }, Interval{ start: 3, end: 14, }, }) fmt.Println(rooms) } ``` and result : ```bash go run min-meeting-room.go 4 ``` --- logic/intervals/min-meeting-room.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/logic/intervals/min-meeting-room.js b/logic/intervals/min-meeting-room.js index 3ce22af..0c11956 100644 --- a/logic/intervals/min-meeting-room.js +++ b/logic/intervals/min-meeting-room.js @@ -33,21 +33,27 @@ var question = [ return rooms; } `, - answerGo: `func minMeetingRooms(intervals [][]int) int { - if len(intervals) == 0 { - return 0 - } + answerGo: `type Interval struct { + start int + end int +} - startTimes := make([]int, len(intervals)) - endTimes := make([]int, len(intervals)) +func minMeetingRooms(intervals []Interval) int { + startTimes := make([]int, 0) + endTimes := make([]int, 0) - for i, interval := range intervals { - startTimes[i] = interval[0] - endTimes[i] = interval[1] + for _, v := range intervals { + startTimes = append(startTimes, v.start) + endTimes = append(endTimes, v.end) } - sort.Ints(startTimes) - sort.Ints(endTimes) + sort.Slice(startTimes, func(i, j int) bool { + return startTimes[i] > startTimes[j] + }) + + sort.Slice(endTimes, func(i, j int) bool { + return endTimes[i] > endTimes[j] + }) rooms := 0 j := 0