Skip to content

Meeting Rooms

Easy

The problem

Each meeting is an Interval with a start time and an end time. Return true if one person can attend every meeting, which means no two meetings overlap, and false otherwise. A meeting may start at the exact moment another one ends.

  • Example 1
    Input: intervals = [[0, 30], [5, 10], [15, 20]]
    Output: false

    The meeting 0 to 30 runs while 5 to 10 is happening.

  • Example 2
    Input: intervals = [[7, 10], [2, 4]]
    Output: true

    One meeting ends at 4 and the other starts at 7.

  • Example 3
    Input: intervals = [[1, 5], [5, 8]]
    Output: true

    The second meeting starts exactly when the first ends, which is allowed.

Limits
  • 0 ≤ len(intervals) ≤ 100,000
  • start < end for every meeting
  • Each example is written as [start, end]

Write it in Go. Try for about 20 minutes on paper first, then open one hint at a time.

Try it here

Write Go. Common packages like fmt and sort are imported for you. Keep the function name and inputs the same.

Hints, one at a time

Nudge

One person can attend all meetings only if no two overlap.

The idea

Sort by start and check that every meeting starts at or after the previous one ends.

Target: O(n log n) time

Go function shape
func CanAttendMeetings(intervals []*Interval) bool
Reference solution

Tested with go test. Try it yourself first, then compare.

// CanAttendMeetings: true if no two meetings overlap.
func CanAttendMeetings(intervals [][]int) bool {
	sorted := slices.Clone(intervals)
	slices.SortFunc(sorted, func(a, b []int) int { return cmp.Compare(a[0], b[0]) })
	for i := 1; i < len(sorted); i++ {
		if sorted[i][0] < sorted[i-1][1] {
			return false
		}
	}
	return true
}