Skip to content

Commit

Permalink
Minor update and add new file
Browse files Browse the repository at this point in the history
  • Loading branch information
William-Weston committed Jun 8, 2024
1 parent becb4be commit dc4158d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Development/Mate In One Move/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enable_testing()

find_package( GTest REQUIRED )

# change to target_sources
set( TEST_SOURCE
src/Solver.cpp
tests/mate_in_one_move.t.cpp
Expand All @@ -57,6 +58,7 @@ target_compile_options( ${TEST_SUITE}
-pg
-ftest-coverage
-fsanitize=address
-fno-omit-frame-pointer
-fsanitize=undefined
-fsanitize=shift
-fsanitize=shift-exponent
Expand All @@ -74,11 +76,11 @@ target_compile_options( ${TEST_SUITE}
-fsanitize=float-cast-overflow
-fsanitize=enum
-fsanitize=pointer-overflow
-fsanitize-address-use-after-scope
-fsanitize-address-use-after-scope
-fstack-protector-all
)

target_link_options(${TEST_SUITE}
target_link_options( ${TEST_SUITE}
PRIVATE
-fsanitize=undefined
-fsanitize=address
Expand All @@ -91,4 +93,4 @@ target_include_directories( ${TEST_SUITE} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/in
target_include_directories( ${TEST_SUITE} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src" )

include( GoogleTest )
gtest_discover_tests( ${TEST_SUITE} )
gtest_discover_tests( ${TEST_SUITE} )
81 changes: 81 additions & 0 deletions cookieselection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <cassert>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include <queue>



class Holding_Area final
{
public:
auto add( int diameter ) -> void;
auto request() -> int;

private:
using max_heap = std::priority_queue<int, std::vector<int>, std::less<int>>;
using min_heap = std::priority_queue<int, std::vector<int>, std::greater<int>>;

max_heap max_queue = {};
min_heap min_queue = {};
};

auto
Holding_Area::add( int diameter ) -> void
{
if ( max_queue.empty() || diameter >= max_queue.top() )
{
min_queue.push( diameter );
}
else
{
max_queue.push( diameter );
min_queue.push( max_queue.top() );
max_queue.pop();
}
if ( min_queue.size() > ( max_queue.size() + 1 ) )
{
max_queue.push( min_queue.top() );
min_queue.pop();
}
}

auto
Holding_Area::request() -> int
{
assert( min_queue.size() );

auto cookie = min_queue.top();
min_queue.pop();

if ( max_queue.size() > min_queue.size() )
{
min_queue.push( max_queue.top() );
max_queue.pop();
}

return cookie;
}

auto
main() -> int
{
auto holding_area = Holding_Area{};

for ( std::string input; std::cin >> input; )
{
if ( input == "#" ) // request
{
std::cout << holding_area.request() << '\n';
}
else // cookie arrival
{
int const diameter = std::stoi( input );
holding_area.add( diameter );
}
}

return EXIT_SUCCESS;
}

0 comments on commit dc4158d

Please sign in to comment.