cs: add «2300. Successful Pairs of Spells and Potions»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-19 21:51:56 +02:00
parent 0f02ef8e65
commit feaf1b9e17
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -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();
}
}