Skip to content

Commit

Permalink
Solved Top K most frequent elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Jun 5, 2023
1 parent 4dd1c7a commit 90407df
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
add_subdirectory( longest-substring-without-repeating-characters )
add_subdirectory( top-k-frequent-elements )
add_subdirectory( two-sum )
25 changes: 25 additions & 0 deletions top-k-frequent-elements/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions top-k-frequent-elements/runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <vector>

#include "solution.hpp"

std::ostream& operator<<(std::ostream& os, const std::vector<int>& vector)
{
os << "[";
bool first = true;
for (const int i : vector)
{
if (!first)
os<<",";
os<<i;
first = false;
}

os << "]";

return os;
}

int main()
{
const std::vector<int> input = {1};
const int k = 1;
Solution s;
std::cout << input << ": " << s.topKFrequent(input, k) << std::endl;

return 1;
}
29 changes: 29 additions & 0 deletions top-k-frequent-elements/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <algorithm>
#include <vector>
#include <unordered_map>

class Solution
{
public:
std::vector<int> topKFrequent(const std::vector<int> &nums, int k)
{
std::unordered_map<int, int> frequencies;
for (int i : nums)
{
frequencies[i]++;
}
std::vector<std::pair<int, int>> intermediate(frequencies.begin(), frequencies.end());
std::sort(intermediate.begin(), intermediate.end(), [](const auto &left, const auto &right)
{ return left.second > right.second; });

std::vector<int> result;
for (int i = 0; i < k; ++i)
{
result.push_back(intermediate[i].first);
}

return result;
}
};
35 changes: 35 additions & 0 deletions top-k-frequent-elements/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <gtest/gtest.h>

#include <algorithm>
#include <tuple>
#include <vector>

#include "solution.hpp"

class FrequencyTest :public ::testing::TestWithParam<std::tuple<std::vector<int>, int, std::vector<int>>> {
protected:
Solution solution;
};

TEST_P(FrequencyTest, CheckSolution) {
const std::vector<int> input = std::get<0>(GetParam());
int k = std::get<1>(GetParam());
const std::vector<int> expected = std::get<2>(GetParam());

std::vector<int> 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<std::vector<int>, int, std::vector<int>>({1}, 1, {1}),
std::make_tuple<std::vector<int>, int, std::vector<int>>({1, 1}, 1, {1}),
std::make_tuple<std::vector<int>, int, std::vector<int>>({1, 1, 2}, 1, {1}),
std::make_tuple<std::vector<int>, int, std::vector<int>>({1, 1, 2}, 2, {1, 2})
)
);

0 comments on commit 90407df

Please sign in to comment.