add: 230506

This commit is contained in:
Eatswap 2023-05-06 18:41:05 +08:00
parent 71fa620cd9
commit bce7fa8ad0
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 41 additions and 3 deletions

34
cpp/2305/LC230506.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <algorithm>
#include <vector>
/**
* 1498. Number of Subsequences That Satisfy the Given Sum Condition
*
* You are given an array of integers nums and an integer target.
* Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 109 + 7.
*/
class LC230506 {
public:
static int numSubseq(std::vector<int>&, int) noexcept;
};
constexpr int pwr2(int x) noexcept {
unsigned long long cur = 2, ret = 1;
for (; x; (x >>= 1), (cur = (cur * cur) % 1'000'000'007))
if (x & 1)
ret = (ret * cur) % 1'000'000'007;
return ret % 1'000'000'007;
}
int LC230506::numSubseq(std::vector<int>& n, int target) noexcept {
std::sort(n.begin(), n.end());
int ret = 0;
for (int l = 0, r = n.size() - 1; l < n.size(); ++l) {
for (; r > 0 && n[l] + n[r] > target; --r);
ret = (ret + ((!r || r <= l) ? ((n[l] << 1) <= target) : pwr2(r - l))) % 1'000'000'007;
if ((n[l] << 1) > target)
return ret;
}
return ret;
}

View File

@ -60,4 +60,9 @@ public:
static int maxVowels(const std::string&, int) noexcept;
};
class LC230506 {
public:
static int numSubseq(std::vector<int>&, int) noexcept;
};
#endif //LEETCODE_CPP_DEFS_H

View File

@ -12,8 +12,7 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& x) noexcept {
}
int main() {
std::cout << LC230504CN::maxTotalFruits({
{0,3},{6,4},{8,5}
}, 3,2);
std::vector<int> arr {2,3,3,4,6,7};
std::cout << LC230506::numSubseq(arr,12);
return 0;
}