From bf4d751c65cb00ca637e73f46e34e7d49c85f7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BE=D1=80=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Sun, 15 Oct 2023 23:09:32 +0300 Subject: [PATCH] Neither Minimum nor Maximum --- CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + .../neither-minimum-nor-maximum/solution.hpp | 15 ++++++++++ .../neither-minimum-nor-maximum/test.cpp | 30 +++++++++++++++++++ .../points-that-intersect-with-cars/test.cpp | 16 +++++----- 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 solutions/neither-minimum-nor-maximum/CMakeLists.txt create mode 100644 solutions/neither-minimum-nor-maximum/solution.hpp create mode 100644 solutions/neither-minimum-nor-maximum/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 371f07d7..085a18f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -418,6 +418,7 @@ add_task(n-queens) add_task(n-queens-ii) add_task(n-th-tribonacci-number) add_task(nearest-exit-from-entrance-in-maze) +add_task(neither-minimum-nor-maximum) add_task(new-21-game) add_task(next-greater-element-i) add_task(next-greater-element-ii) diff --git a/solutions/neither-minimum-nor-maximum/CMakeLists.txt b/solutions/neither-minimum-nor-maximum/CMakeLists.txt new file mode 100644 index 00000000..d2ff3976 --- /dev/null +++ b/solutions/neither-minimum-nor-maximum/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_neither_minimum_nor_maximum test.cpp) diff --git a/solutions/neither-minimum-nor-maximum/solution.hpp b/solutions/neither-minimum-nor-maximum/solution.hpp new file mode 100644 index 00000000..a30bcd15 --- /dev/null +++ b/solutions/neither-minimum-nor-maximum/solution.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +class Solution { +public: + static int findNonMinOrMax(std::vector nums) { + if (nums.size() < 3) { + return -1; + } + std::sort(nums.begin(), nums.begin() + 3); + return nums[1]; + } +}; diff --git a/solutions/neither-minimum-nor-maximum/test.cpp b/solutions/neither-minimum-nor-maximum/test.cpp new file mode 100644 index 00000000..ba36246f --- /dev/null +++ b/solutions/neither-minimum-nor-maximum/test.cpp @@ -0,0 +1,30 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::vector nums; + int expected; + }; + + std::vector test_cases{ + { + .nums{3, 2, 1, 4}, + .expected = 2, + }, + { + .nums{1, 2}, + .expected = -1, + }, + { + .nums{2, 1, 3}, + .expected = 2, + }, + }; + + for (const auto &[nums, expected] : test_cases) { + const auto actual = Solution::findNonMinOrMax(nums); + REQUIRE(expected == actual); + } +} diff --git a/solutions/points-that-intersect-with-cars/test.cpp b/solutions/points-that-intersect-with-cars/test.cpp index a5fff059..37a4457e 100644 --- a/solutions/points-that-intersect-with-cars/test.cpp +++ b/solutions/points-that-intersect-with-cars/test.cpp @@ -9,14 +9,14 @@ TEST_CASE("Simple") { }; std::vector test_cases{ - // { - // .nums{{3, 6}, {1, 5}, {4, 7}}, - // .expected = 7, - // }, - // { - // .nums{{1, 3}, {5, 8}}, - // .expected = 7, - // }, + { + .nums{{3, 6}, {1, 5}, {4, 7}}, + .expected = 7, + }, + { + .nums{{1, 3}, {5, 8}}, + .expected = 7, + }, { .nums{{4, 4}, {9, 10}, {9, 10}, {3, 8}}, .expected = 8,