Skip to content

visual walkthrough

Successful Pairs of Spells and Potions

Sort once, then binary search per query.

Solve on LeetCode

The idea

For one spell, a potion works if it's at least ⌈success / spell⌉. After sorting, those potions sit together at the end.

So each spell needs one binary search for where that block starts, and the count is everything from there.

Complexity

approachtimespace
Every pairO(n · m)O(1)
Sort + binary searchO((n + m) log m)O(m)

More walkthroughs