Created
November 24, 2021 10:16
-
-
Save mrd000/4bbfbafd2611ca0443889d176fc91d57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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