Advertisement

Successful Pairs of Spells and Potions - LeetCode 2300 Solution

Successful Pairs of Spells and Potions - Complete Solution Guide

Successful Pairs of Spells and Potions is LeetCode problem 2300, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two positive integer arrays spells and potions , of length n and m respectively, where spells[i] represents the strength of the i th spell and potions[j] represents the strength of the j th potion. You are also given an integer success . A spell and potion pair is considered successful if the product of their strengths is at least success . Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the i th spell. Example

Detailed Explanation

The problem requires us to find, for each spell in the `spells` array, how many potions in the `potions` array can form a successful pair. A successful pair is defined as a spell and potion whose product is greater than or equal to a given `success` value. The goal is to return an array `pairs` where `pairs[i]` represents the number of potions that form a successful pair with the `i`-th spell.

Solution Approach

The provided solution uses a binary search approach after sorting the `potions` array. For each spell, it calculates the minimum potion strength required for a successful pair, then uses binary search (specifically, `bisect_left` or a similar implementation) to find the index of the first potion that meets this minimum strength. The number of potions to the right of this index (inclusive) represents the number of successful pairs for that spell.

Step-by-Step Algorithm

  1. Step 1: Sort the `potions` array in ascending order. This enables efficient searching.
  2. Step 2: Iterate through the `spells` array. For each `spell` value:
  3. Step 3: Calculate the minimum required potion strength: `min_potion_strength = (success + spell - 1) // spell`. This ensures we get the ceiling of `success / spell` when using integer division, meaning we always round up to the nearest integer.
  4. Step 4: Use binary search (e.g., `bisect_left` in Python, or a custom binary search function in Java/C/C++) to find the index of the first potion in the sorted `potions` array that is greater than or equal to `min_potion_strength`.
  5. Step 5: Calculate the number of successful pairs for the current spell: `count = m - index`, where `m` is the length of the `potions` array and `index` is the index found in the previous step.
  6. Step 6: Add the `count` to the `pairs` array.
  7. Step 7: Return the `pairs` array.

Key Insights

  • Insight 1: Sorting the `potions` array allows us to use binary search for efficient counting.
  • Insight 2: For each spell, we need to find the minimum potion strength required to form a successful pair: `potion >= success / spell`. We must handle integer division carefully.
  • Insight 3: Using `bisect_left` (or equivalent binary search) helps find the index of the first potion that meets the minimum strength requirement. The number of potions from that index to the end of the `potions` array are the successful pairs.

Complexity Analysis

Time Complexity: O(n log m)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Binary Search, Sorting.

Companies

Asked at: Goldman Sachs.