Skip to content

Instantly share code, notes, and snippets.

@mrd000
Created November 24, 2021 10:16
Show Gist options
  • Select an option

  • Save mrd000/4bbfbafd2611ca0443889d176fc91d57 to your computer and use it in GitHub Desktop.

Select an option

Save mrd000/4bbfbafd2611ca0443889d176fc91d57 to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/interval-list-intersections/
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
var result [][]int
if len(firstList) == 0 || len(secondList) == 0 {
return result
}
firstListPos := 0
secondListPos := 0
for ; firstListPos < len(firstList) && secondListPos < len(secondList) ; {
if firstList[firstListPos][0] <= secondList[secondListPos][0] {
if firstList[firstListPos][1] < secondList[secondListPos][0] {
firstListPos++
} else if firstList[firstListPos][1] <= secondList[secondListPos][1] {
result = append(result, []int{secondList[secondListPos][0], firstList[firstListPos][1]})
firstListPos++
} else {
result = append(result, []int{secondList[secondListPos][0], secondList[secondListPos][1]})
secondListPos++
}
} else {
if secondList[secondListPos][1] < firstList[firstListPos][0] {
secondListPos++
} else if secondList[secondListPos][1] <= firstList[firstListPos][1] {
result = append(result, []int{firstList[firstListPos][0], secondList[secondListPos][1]})
secondListPos++
} else {
result = append(result, []int{firstList[firstListPos][0], firstList[firstListPos][1]})
firstListPos++
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment