diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d40ee7..771d906 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,8 @@ include(CTest) enable_testing() include(GoogleTest) -add_subdirectory( two-sum ) -add_subdirectory( longest-substring-without-repeating-characters ) +add_subdirectory( contains-duplicate ) add_subdirectory( roman-numerals ) -add_subdirectory( contains-duplicate ) \ No newline at end of file +add_subdirectory( longest-substring-without-repeating-characters ) +add_subdirectory( top-k-frequent-elements ) +add_subdirectory( two-sum ) diff --git a/top-k-frequent-elements/CMakeLists.txt b/top-k-frequent-elements/CMakeLists.txt new file mode 100644 index 0000000..f35f023 --- /dev/null +++ b/top-k-frequent-elements/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.14) +project(top_k_frequent_elements) + +# GoogleTest requires at least C++14 +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +enable_testing() + +add_executable( + frequency_runner + runner.cpp +) + +add_executable( + frequency_tests + tests.cpp +) +target_link_libraries( + frequency_tests + GTest::gtest_main +) +include_directories(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES}) + +gtest_discover_tests(frequency_tests) diff --git a/top-k-frequent-elements/runner.cpp b/top-k-frequent-elements/runner.cpp new file mode 100644 index 0000000..e03f2bf --- /dev/null +++ b/top-k-frequent-elements/runner.cpp @@ -0,0 +1,31 @@ +#include +#include + +#include "solution.hpp" + +std::ostream& operator<<(std::ostream& os, const std::vector& vector) +{ + os << "["; + bool first = true; + for (const int i : vector) + { + if (!first) + os<<","; + os< input = {1}; + const int k = 1; + Solution s; + std::cout << input << ": " << s.topKFrequent(input, k) << std::endl; + + return 1; +} \ No newline at end of file diff --git a/top-k-frequent-elements/solution.hpp b/top-k-frequent-elements/solution.hpp new file mode 100644 index 0000000..9c44e4f --- /dev/null +++ b/top-k-frequent-elements/solution.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include + +class Solution +{ +public: + std::vector topKFrequent(const std::vector &nums, int k) + { + std::unordered_map frequencies; + for (int i : nums) + { + frequencies[i]++; + } + std::vector> intermediate(frequencies.begin(), frequencies.end()); + std::sort(intermediate.begin(), intermediate.end(), [](const auto &left, const auto &right) + { return left.second > right.second; }); + + std::vector result; + for (int i = 0; i < k; ++i) + { + result.push_back(intermediate[i].first); + } + + return result; + } +}; \ No newline at end of file diff --git a/top-k-frequent-elements/tests.cpp b/top-k-frequent-elements/tests.cpp new file mode 100644 index 0000000..6503d50 --- /dev/null +++ b/top-k-frequent-elements/tests.cpp @@ -0,0 +1,35 @@ +#include + +#include +#include +#include + +#include "solution.hpp" + +class FrequencyTest :public ::testing::TestWithParam, int, std::vector>> { +protected: + Solution solution; +}; + +TEST_P(FrequencyTest, CheckSolution) { + const std::vector input = std::get<0>(GetParam()); + int k = std::get<1>(GetParam()); + const std::vector expected = std::get<2>(GetParam()); + + std::vector actual = solution.topKFrequent(input, k); + + std::sort(actual.begin(), actual.end()); + + EXPECT_EQ(expected, actual); +} + +INSTANTIATE_TEST_SUITE_P( + FrequencyTests, + FrequencyTest, + ::testing::Values( + std::make_tuple, int, std::vector>({1}, 1, {1}), + std::make_tuple, int, std::vector>({1, 1}, 1, {1}), + std::make_tuple, int, std::vector>({1, 1, 2}, 1, {1}), + std::make_tuple, int, std::vector>({1, 1, 2}, 2, {1, 2}) + ) + ); \ No newline at end of file