Last active
September 14, 2022 02:21
-
-
Save hucancode/db013b195912435ae826f4ecce1de2f5 to your computer and use it in GitHub Desktop.
check if 2 sorted lists of string have any overlaping item in O(logn)
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
// precondition: a & b are both sorted | |
bool overlap(vector<string>& a, vector<string>& b) { | |
auto i = a.begin(); | |
auto j = b.begin(); | |
auto n = upper_bound(i, a.end(), *b.rbegin()); | |
auto m = upper_bound(j, b.end(), *a.rbegin()); | |
while(i < n && j < m) { | |
if(*i == *j) { | |
return true; | |
} | |
auto nextj = lower_bound(j, m, *i); | |
auto nexti = lower_bound(i, n, *j); | |
i = nexti; | |
j = nextj; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment