Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8025350

Browse files
committedFeb 12, 2025
Add solution #2300
1 parent 6127caf commit 8025350

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@
524524
2215|[Find the Difference of Two Arrays](./2215-find-the-difference-of-two-arrays.js)|Easy|
525525
2235|[Add Two Integers](./2235-add-two-integers.js)|Easy|
526526
2244|[Minimum Rounds to Complete All Tasks](./2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
527+
2300|[Successful Pairs of Spells and Potions](./2300-successful-pairs-of-spells-and-potions.js)|Medium|
527528
2336|[Smallest Number in Infinite Set](./2336-smallest-number-in-infinite-set.js)|Medium|
528529
2349|[Design a Number Container System](./2349-design-a-number-container-system.js)|Medium|
529530
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 2300. Successful Pairs of Spells and Potions
3+
* https://leetcode.com/problems/successful-pairs-of-spells-and-potions/
4+
* Difficulty: Medium
5+
*
6+
* You are given two positive integer arrays spells and potions, of length n and m respectively,
7+
* where spells[i] represents the strength of the ith spell and potions[j] represents the strength
8+
* of the jth potion.
9+
*
10+
* You are also given an integer success. A spell and potion pair is considered successful if the
11+
* product of their strengths is at least success.
12+
*
13+
* Return an integer array pairs of length n where pairs[i] is the number of potions that will form
14+
* a successful pair with the ith spell.
15+
*/
16+
17+
/**
18+
* @param {number[]} spells
19+
* @param {number[]} potions
20+
* @param {number} success
21+
* @return {number[]}
22+
*/
23+
var successfulPairs = function(spells, potions, success) {
24+
const result = [];
25+
potions.sort((a, b) => a - b);
26+
27+
for (let i = 0; i < spells.length; i++) {
28+
let left = 0;
29+
let right = potions.length - 1;
30+
31+
while (left <= right) {
32+
const mid = Math.floor((left + right) / 2);
33+
if (spells[i] * potions[mid] < success) {
34+
left = mid + 1;
35+
} else {
36+
right = mid - 1;
37+
}
38+
}
39+
40+
result[i] = potions.length - left;
41+
}
42+
43+
return result;
44+
};

0 commit comments

Comments
 (0)
Please sign in to comment.