Created
February 23, 2016 16:17
-
-
Save Frencil/f4fecfa1b59952e4bb70 to your computer and use it in GitHub Desktop.
Candidate solutions sent after the fact
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
int solution(std::vector<int> &A, std::vector<int> &B) | |
{ | |
std::map<int, int> disjoints; | |
for (std::size_t i = 0; i < A.size(); ++i) | |
{ | |
auto it = disjoints.lower_bound(B[i]); | |
if (it == disjoints.end()) | |
{ | |
disjoints.emplace(A[i], B[i]); | |
} | |
else | |
{ | |
if (B[i] >= it->first && A[i] <= it->second) | |
{ | |
std::pair<int, int> new_pair(std::min(it->first, A[i]), std::max(it->second, B[i])); | |
disjoints.erase(it); | |
disjoints.emplace(new_pair.first, new_pair.second); | |
} | |
else if (it != disjoints.begin()) | |
{ | |
--it; | |
if (B[i] >= it->first && A[i] <= it->second) | |
{ | |
std::pair<int, int> new_pair(std::min(it->first, A[i]), std::max(it->second, B[i])); | |
disjoints.erase(it); | |
disjoints.emplace(new_pair.first, new_pair.second); | |
} | |
else | |
{ | |
disjoints.emplace(A[i], B[i]); | |
} | |
} | |
else | |
{ | |
disjoints.emplace(A[i], B[i]); | |
} | |
} | |
} | |
return disjoints.size(); | |
} | |
int solution2(int x, std::vector<int> &arr) | |
{ | |
std::size_t total_equal_to = 0; | |
for (std::size_t i = 0; i < arr.size(); ++i) | |
{ | |
if (arr[i] == x) | |
++total_equal_to; | |
} | |
std::size_t total_not_equal_to = arr.size() - total_equal_to; | |
std::size_t left_equal_to = 0; | |
for (std::size_t i = 0; i < arr.size(); ++i) | |
{ | |
if (arr[i] == x) | |
++left_equal_to; | |
std::size_t left_not_equal_to = (i + 1) - left_equal_to; | |
std::size_t right_not_equal_to = total_not_equal_to - left_not_equal_to; | |
if (left_equal_to == right_not_equal_to) // I had typo here before | |
return i + 1; | |
} | |
return -1; // Does not exist. Should never happen. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment