Created
May 14, 2021 03:16
-
-
Save vrat28/76eb6ca1b677cd7d374cb382cd48c863 to your computer and use it in GitHub Desktop.
Interval List Intersections
This file contains 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
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