|
| 1 | +// 2300. Successful Pairs of Spells and Potions |
| 2 | +// You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion.// You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at least success. |
| 3 | +// Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the ith spell. |
| 4 | + |
| 5 | +// Example 1: |
| 6 | +// Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7 |
| 7 | +// Output: [4,0,3] |
| 8 | +// Explanation: |
| 9 | +// - 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful. |
| 10 | +// - 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful. |
| 11 | +// - 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful. |
| 12 | +// Thus, [4,0,3] is returned. |
| 13 | +// Example 2: |
| 14 | +// Input: spells = [3,1,2], potions = [8,5,8], success = 16 |
| 15 | +// Output: [2,0,2] |
| 16 | +// Explanation: |
| 17 | +// - 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful. |
| 18 | +// - 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. |
| 19 | +// - 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. |
| 20 | +// Thus, [2,0,2] is returned. |
| 21 | + |
| 22 | +// Constraints: |
| 23 | +// n == spells.length |
| 24 | +// m == potions.length |
| 25 | +// 1 <= n, m <= 105 |
| 26 | +// 1 <= spells[i], potions[i] <= 105 |
| 27 | +// 1 <= success <= 1010 |
| 28 | +public class Solution { |
| 29 | + public int[] SuccessfulPairs(int[] spells, int[] potions, long success) { |
| 30 | + // Sort the potions array |
| 31 | + Array.Sort(potions); |
| 32 | + |
| 33 | + // Initialize the array to store the results |
| 34 | + int[] successPair = new int[spells.Length]; |
| 35 | + |
| 36 | + // Iterate through each spell |
| 37 | + for(int i = 0; i < spells.Length; i++){ |
| 38 | + // Perform a binary search to find the minimum index of the potion that meets the success criteria |
| 39 | + int min = BinarySearch(potions, spells[i], success); |
| 40 | + // Calculate the number of successful pairs |
| 41 | + successPair[i] = potions.Length - min; |
| 42 | + } |
| 43 | + |
| 44 | + // Return the array with the counts of successful pairs |
| 45 | + return successPair; |
| 46 | + } |
| 47 | + |
| 48 | + // Helper method to perform binary search |
| 49 | + private int BinarySearch(int[] potions, int spell, long success) { |
| 50 | + int left = 0; |
| 51 | + int right = potions.Length; |
| 52 | + |
| 53 | + // Perform binary search |
| 54 | + while (left < right) { |
| 55 | + int mid = left + (right - left) / 2; |
| 56 | + if ((long) spell * potions[mid] >= success) { |
| 57 | + right = mid; // Look for a smaller index on the left side |
| 58 | + } else { |
| 59 | + left = mid + 1; // Look for a larger index on the right side |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return left; |
| 64 | + } |
| 65 | +} |
0 commit comments