From feaf1b9e1743991086c4a5c70e73ded7dd80428f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 19 Aug 2024 21:51:56 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB2300.=20Successful=20Pairs=20?= =?UTF-8?q?of=20Spells=20and=20Potions=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/successful-pairs-of-spells-and-potions.cs | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cs/successful-pairs-of-spells-and-potions.cs 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(); + } +}