Solved: 25 - Container Adapters
This commit is contained in:
@@ -1,20 +1,55 @@
|
|||||||
#include "container_adapters.hpp"
|
#include "container_adapters.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
namespace stl_containers {
|
namespace stl_containers {
|
||||||
|
|
||||||
auto is_balanced_parentheses(const std::string& text) -> bool {
|
auto is_balanced_parentheses(const std::string& text) -> bool {
|
||||||
(void)text;
|
std::stack<char> s;
|
||||||
|
for (const auto c: text) {
|
||||||
|
switch (c) {
|
||||||
|
case '(':
|
||||||
|
s.push(')');
|
||||||
|
break;
|
||||||
|
case '[':
|
||||||
|
s.push(']');
|
||||||
|
break;
|
||||||
|
case '{':
|
||||||
|
s.push('}');
|
||||||
|
break;
|
||||||
|
case ')':
|
||||||
|
case ']':
|
||||||
|
case '}': {
|
||||||
|
if (const auto n = s.top(); c != n) return false;
|
||||||
|
s.pop();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s.empty()) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto simulate_queue(const std::vector<int>& arrivals) -> std::vector<int> {
|
auto simulate_queue(const std::vector<int>& arrivals) -> std::vector<int> {
|
||||||
(void)arrivals;
|
auto v = std::vector<int>{};
|
||||||
return {};
|
const auto push = [&v](const int val){v.push_back(val);};
|
||||||
|
std::ranges::for_each(arrivals, push);
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto top_k_largest(const std::vector<int>& data, int k) -> std::vector<int> {
|
auto top_k_largest(const std::vector<int>& data, const int k) -> std::vector<int> {;
|
||||||
(void)data; (void)k;
|
auto v = std::vector<int>{};
|
||||||
return {};
|
std::ranges::for_each(data, [&v](const auto& p){v.push_back(p);});
|
||||||
|
// Remove duplicates
|
||||||
|
std::set<int> s{v.begin(), v.end()};
|
||||||
|
v = std::vector<int>{s.begin(), s.end()};
|
||||||
|
std::ranges::sort(v, std::ranges::greater{});
|
||||||
|
v.resize(k);
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace stl_containers
|
} // namespace stl_containers
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ TEST(ContainerAdapters, SimulateQueue) {
|
|||||||
|
|
||||||
TEST(ContainerAdapters, TopKLargest) {
|
TEST(ContainerAdapters, TopKLargest) {
|
||||||
EXPECT_EQ(stl_containers::top_k_largest({3, 1, 4, 1, 5}, 3), (std::vector<int>{5, 4, 3}));
|
EXPECT_EQ(stl_containers::top_k_largest({3, 1, 4, 1, 5}, 3), (std::vector<int>{5, 4, 3}));
|
||||||
|
EXPECT_EQ(stl_containers::top_k_largest({3, 1, 4, 1, 5, 5, 5}, 3), (std::vector<int>{5, 4, 3}));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user