-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuccessfulPairs.ts
38 lines (29 loc) · 1.01 KB
/
successfulPairs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
type SuccessfulPairs = (spells: number[], potions: number[], success: number) => number[];
/**
* Accepted
*/
export const successfulPairs: SuccessfulPairs = (spells, potions, success) => {
potions.sort((a, b) => a - b);
const pairs: number[] = [];
// Helper function to perform binary search
const binarySearch = (spell: number): number => {
let low = 0;
let high = potions.length - 1;
const requiredStrength = Math.ceil(success / spell);
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (potions[mid] >= requiredStrength) {
high = mid - 1; // Continue to search in the left half
} else {
low = mid + 1; // Search in the right half
}
}
return low; // Low is the first index where potions[i] * spell >= success
};
// For each spell, find the count of successful pairs
for (const spell of spells) {
const index = binarySearch(spell);
pairs.push(potions.length - index); // Successful pairs count
}
return pairs;
};