Skip to content

Latest commit

ย 

History

History
34 lines (30 loc) ยท 930 Bytes

invidam.go.md

File metadata and controls

34 lines (30 loc) ยท 930 Bytes

Intuition

It is a simple greedy problem. ๊ฐ„๋‹จํ•œ ๊ทธ๋ฆฌ๋”” ๋ถ„๋ฅ˜์˜ ๋ฌธ์ œ๋‹ค.

Approach

  1. To find earliest interval, sort intervals by start time in ascending order.
  2. After sorting, while iterating through the array, check for conflict: the current interval's start time shoud be smaller than the next interval's end time.

Complexity

  • Time complexity: $$O(nlog(n))$$
  • Space complexity: $$O(n)$$

(n is length of intervals)

Code

func CanAttendMeetings(intervals []*Interval) bool {
	sort.Slice(intervals, func(i, j int) bool {
		return intervals[i].Start < intervals[j].Start
	})

	curr := &Interval{-1, -1}
	for _, next := range intervals {
		if curr.End > next.Start {
			return false
		}
		curr = next
	}

	return true
}

What I learned

GoLang Sort