From 3cc6b0c5e22d890b9bcb3e9018e03b7ed9743947 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Mon, 13 Jun 2022 17:43:57 +0800 Subject: [PATCH] add: 220611 [cpp] --- cpp/2206/220611.cpp | 86 +++++++++++++++++++++++++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220611.cpp diff --git a/cpp/2206/220611.cpp b/cpp/2206/220611.cpp new file mode 100644 index 0000000..58d070a --- /dev/null +++ b/cpp/2206/220611.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +/** + * 1658. Minimum Operations to Reduce X to Zero + * You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations. + * Return the minimum number of operations to reduce x to exactly 0 if it is possible, otherwise, return -1. + * + * Runtime: 179 ms, faster than 94.38% of C++ online submissions for Minimum Operations to Reduce X to Zero. + * Memory Usage: 98.5 MB, less than 60.71% of C++ online submissions for Minimum Operations to Reduce X to Zero. + */ + +class Solution { +public: + static int minOperations(const std::vector& ns, const int x) { + const int n = ns.size(); + int sum = std::accumulate(ns.begin(), ns.end(), 0); + + if (sum < x) + return -1; + if (sum == x) + return n; + + int sid = n, + pid = 0, + ret = 0x7FFFFFFF; + + // Un-comment this. + // ns.push_back(0); + for (; sid-- >= 0; sum -= ns[n - sid - 1]) { + while (sum < x && pid < n) + sum += ns[pid++]; + if (sum == x) + ret = std::min(ret, pid + sid + 1); + } + + return ret == 0x7FFFFFFF ? -1 : ret; + } +}; + +/* + * Runtime: 215 ms, faster than 81.01% of C++ online submissions for Minimum Operations to Reduce X to Zero. + * Memory Usage: 107.2 MB, less than 34.90% of C++ online submissions for Minimum Operations to Reduce X to Zero. + */ + +class SolutionOld { +public: + static int minOperations(const std::vector& ns, const int x) { + int* ps = new int[ns.size() + 1] {}; + int* ss = new int[ns.size() + 1] {}; + const int n = ns.size(); + + for (int i = 1; i <= n; ++i) + ps[i] = ps[i - 1] + ns[i - 1]; + for (int i = 1; i <= n; ++i) + ss[i] = ss[i - 1] + ns[n - i]; + + if (ps[n] < x) + return -1; + if (ps[n] == x) + return n; + + int sid = std::upper_bound(ss, ss + n + 1, x) - ss, + pid = 0, + ret = (ss[sid] == x) ? sid : 0x7FFFFFFF; + for (--sid; sid >= 0; --sid) { + while (ps[pid] + ss[sid] < x && pid < n) + ++pid; + if (ps[pid] + ss[sid] == x) + ret = std::min(ret, pid + sid); + } + + return ret == 0x7FFFFFFF ? -1 : ret; + } +}; + +int main() { + std::cout << Solution::minOperations({5,2,3,1,1},5) << "\n"; + std::cout << Solution::minOperations({3,2,20,1,1,3},10) << "\n"; + std::cout << Solution::minOperations({1,1},3) << "\n"; + std::cout << Solution::minOperations({1,1,4,2,3},5) << "\n"; + std::cout << Solution::minOperations({5,6,7,8,9},4) << "\n"; + return 0; +} diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index c88a883..a687c49 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220613.cpp) +ADD_EXECUTABLE(2206 220611.cpp)