Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 14, 2021 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/76eb6ca1b677cd7d374cb382cd48c863 to your computer and use it in GitHub Desktop.
Save vrat28/76eb6ca1b677cd7d374cb382cd48c863 to your computer and use it in GitHub Desktop.
Interval List Intersections
class Solution {
func intervalIntersection(_ firstList: [[Int]], _ secondList: [[Int]]) -> [[Int]] {
var output = [[Int]]()
var p1 = 0, p2 = 0
while p1<firstList.count , p2<secondList.count{
let first = firstList[p1]
let second = secondList[p2]
let start = max(first[0],second[0])
let end = min(second[1],first[1])
if start <= end {
output.append([start,end])
}
if second[1] > first[1]{
p1 += 1
}
else{
p2 += 1
}
}
return output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment