Skip to content

Commit 92de8fa

Browse files
committed
Solved kth largest
1 parent 75541c8 commit 92de8fa

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ add_subdirectory( top-k-frequent-elements )
4343
add_subdirectory( two-sum )
4444
add_subdirectory( valid-parentheses )
4545
add_subdirectory( convert-1d-array-into-2d-array )
46+
add_subdirectory( kth-largest-element-in-a-stream )
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
project(kth_largest_element_in_a_stream)
2+
3+
add_executable(
4+
kth_largest_element_in_a_stream_tests
5+
tests.cpp
6+
)
7+
8+
add_executable(
9+
kth_largest_element_in_a_stream_runner
10+
runner.cpp
11+
)
12+
13+
target_link_libraries(
14+
kth_largest_element_in_a_stream_tests
15+
GTest::gtest_main
16+
)
17+
18+
target_link_libraries(
19+
kth_largest_element_in_a_stream_runner
20+
utils
21+
)
22+
23+
gtest_discover_tests(kth_largest_element_in_a_stream_tests)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 2022. Convert 1D Array Into 2D Array
2+
3+
[LeetCode link](https://leetcode.com/problems/convert-1d-array-into-2d-array/)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
#include "print_vector.hpp"
5+
6+
#include "solution.hpp"
7+
8+
int main()
9+
{
10+
KthLargest s(3, { 1,2,3 });
11+
std::cout << s.add(4);
12+
return 0;
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
5+
#include <iterator>
6+
#include <vector>
7+
#include <set>
8+
9+
class KthLargest {
10+
std::multiset<int, std::greater<int>> scores;
11+
const int k;
12+
public:
13+
KthLargest(int k, const std::vector<int>& nums) : k(k) {
14+
assert(k > 0);
15+
for (auto score : nums) {
16+
add(score);
17+
}
18+
}
19+
20+
int add(int val) {
21+
if (scores.size() < static_cast<size_t>(k)) {
22+
scores.insert(val);
23+
}
24+
else {
25+
auto it = scores.lower_bound(val);
26+
if (it != scores.end()) {
27+
scores.insert(val);
28+
scores.erase(std::prev(scores.end()));
29+
}
30+
}
31+
return *(scores.rbegin());
32+
}
33+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <utility>
4+
#include <vector>
5+
6+
#include "solution.hpp"
7+
8+
typedef std::vector<int> ll;
9+
typedef ll::size_type ll_size_type;
10+
11+
class KthLargestTest :public ::testing::TestWithParam<std::tuple<int, ll, ll, ll>> {
12+
};
13+
14+
TEST_P(KthLargestTest, CheckSolution) {
15+
auto param = GetParam();
16+
int k = std::get<0>(param);
17+
const ll initial = std::get<1>(param);
18+
const ll additional = std::get<2>(param);
19+
const ll expected = std::get<3>(param);
20+
21+
KthLargest solution(k, initial);
22+
23+
int result = 0;
24+
EXPECT_EQ(additional.size(), expected.size());
25+
26+
for (ll_size_type i = 0; i < additional.size(); ++i) {
27+
EXPECT_EQ(expected[i], solution.add(additional[i]));
28+
}
29+
}
30+
31+
INSTANTIATE_TEST_SUITE_P(
32+
KthLargestTests,
33+
KthLargestTest,
34+
::testing::Values(
35+
std::tuple<int, ll, ll, ll>(1, { 1, 2, 3 }, { 1, 2, 3 }, { 3, 3, 3 }),
36+
std::tuple<int, ll, ll, ll>(1, { 2 }, { 3, 1 }, { 3, 3 }),
37+
std::tuple<int, ll, ll, ll>(2, { 2, 2, 2, 2 }, { 2, 3, 2 }, { 2, 2 , 2 }),
38+
std::tuple<int, ll, ll, ll>(3, { 4, 5, 8, 2 }, { 3, 5, 10, 9, 4 }, { 4, 5, 5, 8, 8 })
39+
)
40+
);

0 commit comments

Comments
 (0)