Skip to content

Commit 9762e34

Browse files
committed
problem21
1 parent 9021554 commit 9762e34

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

problem020/problem020.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ func Run(firstList *linkedList, secondList *linkedList) int {
4343
shorterOne, longerOne = firstList, secondList
4444
}
4545

46-
shorterOneCursor, longerOneCursor := shorterOne, longerOne
4746
for i := 0; i < lengthDifference; i++ {
48-
longerOneCursor = longerOneCursor.next
47+
longerOne = longerOne.next
4948
}
50-
for shorterOneCursor.value != longerOneCursor.value {
51-
shorterOneCursor = shorterOneCursor.next
52-
longerOneCursor = longerOneCursor.next
49+
for shorterOne.value != longerOne.value {
50+
shorterOne = shorterOne.next
51+
longerOne = longerOne.next
5352
}
54-
return shorterOneCursor.value
53+
return shorterOne.value
5554
}

problem021/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Daily Coding Problem: Problem #21 [Easy]
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Snapchat.
6+
7+
Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required.
8+
9+
For example, given `[(30, 75), (0, 50), (60, 150)]`, you should return 2.

problem021/problem021.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package problem021
2+
3+
import (
4+
"sort"
5+
)
6+
7+
type Schedule struct {
8+
startsAt int
9+
endsAt int
10+
}
11+
12+
func NewLecture(startsAt int, endsAt int) Schedule {
13+
return Schedule{
14+
startsAt: startsAt,
15+
endsAt: endsAt,
16+
}
17+
}
18+
19+
const (
20+
START = 0
21+
END = 1
22+
)
23+
24+
type event struct {
25+
category int
26+
time int
27+
}
28+
29+
func Run(lectureSchedules []Schedule) int {
30+
events := make([]event, 0, len(lectureSchedules)*2)
31+
for _, lectureSchedule := range lectureSchedules {
32+
events = append(events,
33+
event{
34+
category: START,
35+
time: lectureSchedule.startsAt,
36+
}, event{
37+
category: END,
38+
time: lectureSchedule.endsAt,
39+
})
40+
}
41+
sort.Slice(events, func(i, j int) bool { return events[i].time <= events[j].time })
42+
43+
classroomCount, maxClassroomCount := 0, 0
44+
for _, event := range events {
45+
if event.category == START {
46+
classroomCount++
47+
if classroomCount > maxClassroomCount {
48+
maxClassroomCount = classroomCount
49+
}
50+
} else {
51+
classroomCount--
52+
}
53+
}
54+
return maxClassroomCount
55+
}

problem021/problem021_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package problem021
2+
3+
import "testing"
4+
5+
func TestRun(t *testing.T) {
6+
lectures := []Schedule{NewLecture(30, 75), NewLecture(0, 50), NewLecture(60, 150)}
7+
if Run(lectures) != 2 {
8+
t.FailNow()
9+
}
10+
}
11+
12+
//[(30, 75), (0, 50), (60, 150)],

0 commit comments

Comments
 (0)