Skip to content

Commit adb9612

Browse files
authored
Merge pull request neetcode-gh#2709 from tahzeer/patch-5
Create 2300-successful-pairs-of-spells-and-potions.cpp
2 parents 7a05e3e + eb2f059 commit adb9612

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Time: O(N * logN)
2+
// Space: O(N)
3+
4+
class Solution {
5+
public:
6+
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
7+
sort(potions.begin(), potions.end());
8+
9+
int n = spells.size();
10+
int m = potions.size();
11+
vector<int> pairs(n);
12+
13+
for(int i = 0; i < n; i++) {
14+
int spell = spells[i];
15+
16+
int start = 0, end = m - 1;
17+
int curr;
18+
19+
while(start <= end) {
20+
curr = start + (end-start)/2;
21+
long long strength = (long long)potions[curr] * (long long)spell;
22+
if(strength < success) {
23+
start = curr + 1;
24+
}
25+
else {
26+
end = curr - 1;
27+
}
28+
}
29+
pairs[i] = m - start;
30+
}
31+
32+
return pairs;
33+
}
34+
};

0 commit comments

Comments
 (0)