From 169e6b33a2874a2458a187407ab267587cc1bd48 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Mon, 3 Apr 2023 13:13:24 +0800 Subject: [PATCH] add: 230402 --- cpp/2304/230402.cpp | 35 +++++++++++++++++++++++++++++++++++ cpp/2304/CMakeLists.txt | 5 +++-- cpp/CMakeLists.txt | 1 + 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 cpp/2304/230402.cpp diff --git a/cpp/2304/230402.cpp b/cpp/2304/230402.cpp new file mode 100644 index 0000000..2adeb8a --- /dev/null +++ b/cpp/2304/230402.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +/** + * 2300. Successful Pairs of Spells and Potions + * + * You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion. + * You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at least success. + * Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the ith spell. + */ + +class Solution { + using VI = std::vector; + using LL = long long; +public: + static VI successfulPairs(const VI& s, VI& p, LL t) { + VI ret; + std::sort(p.begin(), p.end()); + std::transform(s.begin(), s.end(), std::back_inserter(ret), [&](auto&& x) { + return p.end() - std::lower_bound(p.begin(), p.end(), t / x + !!(t % x)); + }); + return ret; + } +}; + +int main() { + long long x = 7; + std::vector args_p {1, 2, 3, 4, 5}; + for (auto ret = Solution::successfulPairs({5, 1, 7}, args_p, x); auto&& i : ret) { + std::printf("%d\n", i); + } + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 4b02b86..6fcecdc 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -1,6 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.24) PROJECT(2304) -SET(CMAKE_CXX_STANDARD 17) +SET(CMAKE_CXX_STANDARD 23) +SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230402-CN.cpp) +ADD_EXECUTABLE(2304 230402.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 711fcd6..d83ea5c 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -2,6 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.23) PROJECT(leetcode-cpp) SET(CMAKE_CXX_STANDARD 23) +SET(CMAKE_EXPORT_COMPILE_COMMANDS true) IF(MSVC) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")