diff --git a/cs/successful-pairs-of-spells-and-potions.cs b/cs/successful-pairs-of-spells-and-potions.cs new file mode 100644 index 0000000..6a4301c --- /dev/null +++ b/cs/successful-pairs-of-spells-and-potions.cs @@ -0,0 +1,25 @@ +public class Solution { + public int[] SuccessfulPairs(int[] spells, int[] potions, long success) { + int getSuccessful(long strength) { + var (left, right) = (0, potions.Length); + + while (left < right) { + var mid = (left + right) / 2; + if (strength * potions[mid] >= success) { + right = mid; + } else { + left = mid + 1; + } + } + + return potions.Length - left; + } + + // Ensort potions to allow bisection. + Array.Sort(potions); + + return spells + .Select(s => getSuccessful(s)) + .ToArray(); + } +}