From b8078e940b0c399ea501b4b3a4bf028e9ad44c37 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: Wed, 20 Sep 2023 08:50:12 +0300 Subject: [PATCH] Minimum Operations to Reduce X to Zero --- CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + .../solution.hpp | 31 +++++++++++++++++++ .../test.cpp | 26 ++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 solutions/minimum-operations-to-reduce-x-to-zero/CMakeLists.txt create mode 100644 solutions/minimum-operations-to-reduce-x-to-zero/solution.hpp create mode 100644 solutions/minimum-operations-to-reduce-x-to-zero/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e975e592..8e9328ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -346,6 +346,7 @@ add_task(minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix) add_task(minimum-number-of-increments-on-subarrays-to-form-a-target-array) add_task(minimum-number-of-vertices-to-reach-all-nodes) add_task(minimum-one-bit-operations-to-make-integers-zero) +add_task(minimum-operations-to-reduce-x-to-zero) add_task(minimum-path-sum) add_task(minimum-penalty-for-a-shop) add_task(minimum-remove-to-make-valid-parentheses) diff --git a/solutions/minimum-operations-to-reduce-x-to-zero/CMakeLists.txt b/solutions/minimum-operations-to-reduce-x-to-zero/CMakeLists.txt new file mode 100644 index 00000000..3d6067ec --- /dev/null +++ b/solutions/minimum-operations-to-reduce-x-to-zero/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_minimum_operations_to_reduce_x_to_zero test.cpp) diff --git a/solutions/minimum-operations-to-reduce-x-to-zero/solution.hpp b/solutions/minimum-operations-to-reduce-x-to-zero/solution.hpp new file mode 100644 index 00000000..b1230d62 --- /dev/null +++ b/solutions/minimum-operations-to-reduce-x-to-zero/solution.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include + +class Solution { +public: + static int minOperations(const std::vector &nums, int x) { + const auto sum = std::accumulate(nums.begin(), nums.end(), 0); + const auto length = maxSubArrayLen(nums, sum - x); + return length == -1 ? -1 : nums.size() - length; + } + +private: + static int maxSubArrayLen(const std::vector &nums, int target_sum) { + const int n = nums.size(); + int window_sum = 0; + int length = -1; + for (int l = 0, r = 0; r < n; ++r) { + window_sum += nums[r]; + while (window_sum > target_sum && l <= r) { + window_sum -= nums[l++]; + } + if (window_sum == target_sum) { + length = std::max(length, r - l + 1); + } + } + return length; + } +}; diff --git a/solutions/minimum-operations-to-reduce-x-to-zero/test.cpp b/solutions/minimum-operations-to-reduce-x-to-zero/test.cpp new file mode 100644 index 00000000..6420391f --- /dev/null +++ b/solutions/minimum-operations-to-reduce-x-to-zero/test.cpp @@ -0,0 +1,26 @@ +#include + +#include + +TEST_CASE("Simple") { + { + std::vector nums{1, 1, 4, 2, 3}; + const int x = 5; + REQUIRE(2 == Solution::minOperations(nums, x)); + } + { + std::vector nums{5, 6, 7, 8, 9}; + const int x = 4; + REQUIRE(-1 == Solution::minOperations(nums, x)); + } + { + std::vector nums{3, 2, 20, 1, 1, 3}; + const int x = 10; + REQUIRE(5 == Solution::minOperations(nums, x)); + } + { + std::vector nums{1, 2, 3, 4}; + const int x = 10; + REQUIRE(4 == Solution::minOperations(nums, x)); + } +}