diff --git a/5_conflicting_appointments.cpp b/5_conflicting_appointments.cpp new file mode 100644 index 0000000..bb4195d --- /dev/null +++ b/5_conflicting_appointments.cpp @@ -0,0 +1,55 @@ +/* Problem: + * https://leetcode.com/problems/non-overlapping-intervals/ + */ + +#include +#include +#include +#include + +struct interval { + int start; + int end; +}; + +std::ostream &operator<<(std::ostream &out, const interval i) { + out << "start: " << i.start << " end: " << i.end << "\n"; + return out; +} + +int solution(std::vector> &&input) { + std::sort(input.begin(), input.end()); + std::list intervals; + for (auto i : input) + intervals.push_back({i[0], i[1]}); + + int deletions = 0; + auto left = intervals.begin(); + std::list::iterator right = left; + right++; + while (right != intervals.end()) { + if (left->end > right->start) { + deletions++; + if (left->end <= right->end) { + intervals.erase(right); + right = left; + right++; + } else { + intervals.erase(left); + left = right; + right++; + } + } else { + left++; + right++; + } + } + return deletions; +} + +int main() { + std::cout << solution({{1, 2}, {2, 3}, {3, 4}, {1, 3}}) << '\n' + << solution({{1, 2}, {1, 2}, {1, 2}}) << '\n' + << solution({{1, 2}, {2, 3}}) << '\n'; + return 0; +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c7fde9..3c68e99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,3 +12,5 @@ add_executable(1_longest_substring_with_k_distinct_characters 1_longest_substrin add_executable(2_num_of_islands 2_num_of_islands.cpp) add_executable(3_squares_of_a_sorted_array 3_squares_of_a_sorted_array.cpp) + +add_executable(5_conflicting_appointments 5_conflicting_appointments.cpp)