Skip to content

Commit e7ba01a

Browse files
authored
Create successful-pairs-of-spells-and-potions.cpp
1 parent c9d716f commit e7ba01a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(mlogm + nlogm)
2+
// Space: O(1)
3+
4+
// binary search
5+
class Solution {
6+
public:
7+
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
8+
sort(begin(potions), end(potions));
9+
vector<int> result;
10+
for (const auto& s: spells) {
11+
result.emplace_back(size(potions) - distance(cbegin(potions), lower_bound(cbegin(potions), cend(potions), ceil_divide(success, s))));
12+
}
13+
return result;
14+
}
15+
16+
private:
17+
int64_t ceil_divide(int64_t a, int64_t b) {
18+
return (a + b - 1) / b;
19+
}
20+
};

0 commit comments

Comments
 (0)