Skip to content

Commit

Permalink
Solved kth largest
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Sep 3, 2024
1 parent 75541c8 commit 92de8fa
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ add_subdirectory( top-k-frequent-elements )
add_subdirectory( two-sum )
add_subdirectory( valid-parentheses )
add_subdirectory( convert-1d-array-into-2d-array )
add_subdirectory( kth-largest-element-in-a-stream )
23 changes: 23 additions & 0 deletions kth-largest-element-in-a-stream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
project(kth_largest_element_in_a_stream)

add_executable(
kth_largest_element_in_a_stream_tests
tests.cpp
)

add_executable(
kth_largest_element_in_a_stream_runner
runner.cpp
)

target_link_libraries(
kth_largest_element_in_a_stream_tests
GTest::gtest_main
)

target_link_libraries(
kth_largest_element_in_a_stream_runner
utils
)

gtest_discover_tests(kth_largest_element_in_a_stream_tests)
3 changes: 3 additions & 0 deletions kth-largest-element-in-a-stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 2022. Convert 1D Array Into 2D Array

[LeetCode link](https://leetcode.com/problems/convert-1d-array-into-2d-array/)
13 changes: 13 additions & 0 deletions kth-largest-element-in-a-stream/runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <vector>

#include "print_vector.hpp"

#include "solution.hpp"

int main()
{
KthLargest s(3, { 1,2,3 });
std::cout << s.add(4);
return 0;
}
33 changes: 33 additions & 0 deletions kth-largest-element-in-a-stream/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <cassert>

#include <iterator>
#include <vector>
#include <set>

class KthLargest {
std::multiset<int, std::greater<int>> scores;
const int k;
public:
KthLargest(int k, const std::vector<int>& nums) : k(k) {
assert(k > 0);
for (auto score : nums) {
add(score);
}
}

int add(int val) {
if (scores.size() < static_cast<size_t>(k)) {
scores.insert(val);
}
else {
auto it = scores.lower_bound(val);
if (it != scores.end()) {
scores.insert(val);
scores.erase(std::prev(scores.end()));
}
}
return *(scores.rbegin());
}
};
40 changes: 40 additions & 0 deletions kth-largest-element-in-a-stream/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <gtest/gtest.h>

#include <utility>
#include <vector>

#include "solution.hpp"

typedef std::vector<int> ll;
typedef ll::size_type ll_size_type;

class KthLargestTest :public ::testing::TestWithParam<std::tuple<int, ll, ll, ll>> {
};

TEST_P(KthLargestTest, CheckSolution) {
auto param = GetParam();
int k = std::get<0>(param);
const ll initial = std::get<1>(param);
const ll additional = std::get<2>(param);
const ll expected = std::get<3>(param);

KthLargest solution(k, initial);

int result = 0;
EXPECT_EQ(additional.size(), expected.size());

for (ll_size_type i = 0; i < additional.size(); ++i) {
EXPECT_EQ(expected[i], solution.add(additional[i]));
}
}

INSTANTIATE_TEST_SUITE_P(
KthLargestTests,
KthLargestTest,
::testing::Values(
std::tuple<int, ll, ll, ll>(1, { 1, 2, 3 }, { 1, 2, 3 }, { 3, 3, 3 }),
std::tuple<int, ll, ll, ll>(1, { 2 }, { 3, 1 }, { 3, 3 }),
std::tuple<int, ll, ll, ll>(2, { 2, 2, 2, 2 }, { 2, 3, 2 }, { 2, 2 , 2 }),
std::tuple<int, ll, ll, ll>(3, { 4, 5, 8, 2 }, { 3, 5, 10, 9, 4 }, { 4, 5, 5, 8, 8 })
)
);

0 comments on commit 92de8fa

Please sign in to comment.