-
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.
- Loading branch information
Showing
6 changed files
with
113 additions
and
0 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,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) |
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,3 @@ | ||
# 2022. Convert 1D Array Into 2D Array | ||
|
||
[LeetCode link](https://leetcode.com/problems/convert-1d-array-into-2d-array/) |
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,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; | ||
} |
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,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()); | ||
} | ||
}; |
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,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 }) | ||
) | ||
); |