Skip to content

Commit

Permalink
Neither Minimum nor Maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 15, 2023
1 parent d6da8b5 commit bf4d751
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions solutions/neither-minimum-nor-maximum/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_neither_minimum_nor_maximum test.cpp)
15 changes: 15 additions & 0 deletions solutions/neither-minimum-nor-maximum/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <algorithm>
#include <vector>

class Solution {
public:
static int findNonMinOrMax(std::vector<int> nums) {
if (nums.size() < 3) {
return -1;
}
std::sort(nums.begin(), nums.begin() + 3);
return nums[1];
}
};
30 changes: 30 additions & 0 deletions solutions/neither-minimum-nor-maximum/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::vector<int> nums;
int expected;
};

std::vector<TestCase> 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);
}
}
16 changes: 8 additions & 8 deletions solutions/points-that-intersect-with-cars/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ TEST_CASE("Simple") {
};

std::vector<TestCase> 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,
Expand Down

0 comments on commit bf4d751

Please sign in to comment.