mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «2300. Successful Pairs of Spells and Potions»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
0f02ef8e65
commit
feaf1b9e17
1 changed files with 25 additions and 0 deletions
25
cs/successful-pairs-of-spells-and-potions.cs
Normal file
25
cs/successful-pairs-of-spells-and-potions.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue