Skip to content

Commit c48139a

Browse files
authored
Create 2300. Successful Pairs of Spells and Potions (#902)
2 parents 89d61a5 + 1738c67 commit c48139a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
#define ll long long
4+
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
5+
sort(potions.begin(), potions.end());
6+
vector<int> ans(spells.size());
7+
8+
for (int i = 0; i < spells.size(); i++) {
9+
ll sp = spells[i];
10+
ll s = 0, e = potions.size() - 1;
11+
int ind = -1;
12+
13+
while (s <= e) {
14+
int mid = (s + e) / 2;
15+
if (potions[mid] * sp >= success) {
16+
ind = mid;
17+
e = mid - 1;
18+
} else {
19+
s = mid + 1;
20+
}
21+
}
22+
23+
ans[i] = (ind == -1) ? 0 : (potions.size() - ind);
24+
}
25+
return ans;
26+
}
27+
};

0 commit comments

Comments
 (0)