diff --git a/CMakeLists.txt b/CMakeLists.txt index d784ead6..ff7555a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ add_task(backspace-string-compare) add_task(balanced-binary-tree) add_task(basic-calculator) add_task(basic-calculator-ii) +add_task(best-poker-hand) add_task(best-sightseeing-pair) add_task(best-time-to-buy-and-sell-stock) add_task(best-time-to-buy-and-sell-stock-ii) diff --git a/solutions/best-poker-hand/CMakeLists.txt b/solutions/best-poker-hand/CMakeLists.txt new file mode 100644 index 00000000..000ef67e --- /dev/null +++ b/solutions/best-poker-hand/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_best_poker_hand test.cpp) diff --git a/solutions/best-poker-hand/solution.hpp b/solutions/best-poker-hand/solution.hpp new file mode 100644 index 00000000..2443d513 --- /dev/null +++ b/solutions/best-poker-hand/solution.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include + +class Solution { +public: + static std::string bestHand(const std::vector &ranks, + const std::vector &suits) { + if (std::set(suits.begin(), suits.end()).size() == 1) { + return "Flush"; + } + + std::map count; + int max = 0; + for (auto r : ranks) { + max = std::max(max, ++count[r]); + } + if (max > 2) { + return "Three of a Kind"; + } + if (max > 1) { + return "Pair"; + } + return "High Card"; + } +}; diff --git a/solutions/best-poker-hand/test.cpp b/solutions/best-poker-hand/test.cpp new file mode 100644 index 00000000..fa780df6 --- /dev/null +++ b/solutions/best-poker-hand/test.cpp @@ -0,0 +1,34 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::vector ranks; + std::vector suits; + std::string expected; + }; + + std::vector test_cases{ + { + .ranks{13, 2, 3, 1, 9}, + .suits{'a', 'a', 'a', 'a', 'a'}, + .expected = "Flush", + }, + { + .ranks{4, 4, 2, 4, 4}, + .suits{'d', 'a', 'a', 'b', 'c'}, + .expected = "Three of a Kind", + }, + { + .ranks{10, 10, 2, 12, 9}, + .suits{'a', 'b', 'c', 'a', 'd'}, + .expected = "Pair", + }, + }; + + for (const auto &[ranks, suits, expected] : test_cases) { + const auto actual = Solution::bestHand(ranks, suits); + REQUIRE(expected == actual); + } +}