-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved Top K most frequent elements.
- Loading branch information
Showing
5 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
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
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
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) |
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
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; | ||
} |
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
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; | ||
} | ||
}; |
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
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}) | ||
) | ||
); |