From 8dbef9c71ab72faaabc6d42be94751f800191138 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: Thu, 19 Oct 2023 22:14:12 +0300 Subject: [PATCH] Distance Between Bus Stops --- CMakeLists.txt | 1 + .../distance-between-bus-stops/CMakeLists.txt | 1 + .../distance-between-bus-stops/solution.hpp | 23 +++++++++++ solutions/distance-between-bus-stops/test.cpp | 39 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 solutions/distance-between-bus-stops/CMakeLists.txt create mode 100644 solutions/distance-between-bus-stops/solution.hpp create mode 100644 solutions/distance-between-bus-stops/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bb43c38b..d3f013f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,6 +192,7 @@ add_task(detonate-the-maximum-bombs) add_task(di-string-match) add_task(diameter-of-binary-tree) add_task(difference-between-element-sum-and-digit-sum-of-an-array) +add_task(distance-between-bus-stops) add_task(distinct-subsequences) add_task(distribute-candies-to-people) add_task(distribute-money-to-maximum-children) diff --git a/solutions/distance-between-bus-stops/CMakeLists.txt b/solutions/distance-between-bus-stops/CMakeLists.txt new file mode 100644 index 00000000..b7e7a564 --- /dev/null +++ b/solutions/distance-between-bus-stops/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_distance_between_bus_stops test.cpp) diff --git a/solutions/distance-between-bus-stops/solution.hpp b/solutions/distance-between-bus-stops/solution.hpp new file mode 100644 index 00000000..7b6bc965 --- /dev/null +++ b/solutions/distance-between-bus-stops/solution.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +class Solution { +public: + static int distanceBetweenBusStops(const std::vector &distance, + int start, int destination) { + if (start > destination) { + std::swap(start, destination); + } + + int clockwise = 0, counterclockwise = 0; + for (int i = 0; i < std::ssize(distance); ++i) { + if (i >= start && i < destination) { + clockwise += distance[i]; + } else { + counterclockwise += distance[i]; + } + } + return std::min(clockwise, counterclockwise); + } +}; diff --git a/solutions/distance-between-bus-stops/test.cpp b/solutions/distance-between-bus-stops/test.cpp new file mode 100644 index 00000000..dd498b07 --- /dev/null +++ b/solutions/distance-between-bus-stops/test.cpp @@ -0,0 +1,39 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::vector distance; + int start; + int destination; + int expected; + }; + + std::vector test_cases{ + { + .distance{1, 2, 3, 4}, + .start = 0, + .destination = 1, + .expected = 1, + }, + { + .distance{1, 2, 3, 4}, + .start = 0, + .destination = 2, + .expected = 3, + }, + { + .distance{1, 2, 3, 4}, + .start = 0, + .destination = 3, + .expected = 4, + }, + }; + + for (const auto &[distance, start, destination, expected] : test_cases) { + const auto actual = + Solution::distanceBetweenBusStops(distance, start, destination); + REQUIRE(expected == actual); + } +}