From cb6cd0b2143dd63bf1be4fe1c97c2604468b5e78 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Fri, 17 Mar 2023 21:14:50 +0800 Subject: [PATCH] add: 230317-CN --- cpp/2303/230317-CN.cpp | 31 +++++++++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230317-CN.cpp diff --git a/cpp/2303/230317-CN.cpp b/cpp/2303/230317-CN.cpp new file mode 100644 index 0000000..e2f721d --- /dev/null +++ b/cpp/2303/230317-CN.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +/** + * 2389. Longest Subsequence With Limited Sum + * + * You are given an integer array nums of length n, and an integer array queries of length m. + * Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i]. + * A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. + */ + +class Solution { +public: + static std::vector answerQueries(std::vector& n, const std::vector& q) { + std::sort(n.begin(), n.end()); + for (int i = 1; i < n.size(); ++i) + n[i] += n[i - 1]; + std::vector ret(q.size()); + for (int i = 0; i < q.size(); ++i) + ret[i] = std::upper_bound(n.begin(), n.end(), q[i]) - n.begin(); + return ret; + } +}; + +int main() { + std::vector n {4,5,2,1}; + for (int i : Solution::answerQueries(n, {3,10,21})) + std::cout << i << '\n'; + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 1228d50..392d7dc 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230316.cpp) +ADD_EXECUTABLE(2303 230317-CN.cpp)