File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 524
524
2215|[ Find the Difference of Two Arrays] ( ./2215-find-the-difference-of-two-arrays.js ) |Easy|
525
525
2235|[ Add Two Integers] ( ./2235-add-two-integers.js ) |Easy|
526
526
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|
527
528
2336|[ Smallest Number in Infinite Set] ( ./2336-smallest-number-in-infinite-set.js ) |Medium|
528
529
2349|[ Design a Number Container System] ( ./2349-design-a-number-container-system.js ) |Medium|
529
530
2352|[ Equal Row and Column Pairs] ( ./2352-equal-row-and-column-pairs.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments