[LeetCode] Successful Pairs of Spells and Potions

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> successfulPairs(vector<int>& A, vector<int>& B, long long success) {
vector<int> res;
sort(begin(B), end(B));
for(auto& a : A) {
long long req = success / a;
if(req * a != success) req++;
auto ans = end(B) - lower_bound(begin(B), end(B), req);
res.push_back(ans);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/12/PS/LeetCode/successful-pairs-of-spells-and-potions/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.