<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: maksimdygai</title>
    <description>The latest articles on DEV Community by maksimdygai (@maksimdygai).</description>
    <link>https://dev.to/maksimdygai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F724208%2F613ade62-8f30-4142-8b9d-b666f20dd670.jpeg</url>
      <title>DEV Community: maksimdygai</title>
      <link>https://dev.to/maksimdygai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maksimdygai"/>
    <language>en</language>
    <item>
      <title>Leetcode - 2300. Successful Pairs of Spells and Potions - JavaScript</title>
      <dc:creator>maksimdygai</dc:creator>
      <pubDate>Mon, 03 Apr 2023 11:32:17 +0000</pubDate>
      <link>https://dev.to/maksimdygai/leetcode-2300-successful-pairs-of-spells-and-potions-with-javascript-4m5p</link>
      <guid>https://dev.to/maksimdygai/leetcode-2300-successful-pairs-of-spells-and-potions-with-javascript-4m5p</guid>
      <description>&lt;p&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given two positive integer arrays spells and potions, of length n and m respectively, where &lt;code&gt;spells[i]&lt;/code&gt; represents the strength of the ith spell and &lt;code&gt;potions[j]&lt;/code&gt; represents the strength of the jth potion.&lt;br&gt;
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.&lt;br&gt;
Return an integer array pairs of length n where &lt;code&gt;pairs[i]&lt;/code&gt; is the number of potions that will form a successful pair with the &lt;code&gt;i&lt;/code&gt;th spell.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
Output: [4,0,3]
Explanation:
- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
Thus, [4,0,3] is returned.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: spells = [3,1,2], potions = [8,5,8], success = 16
Output: [2,0,2]
Explanation:
- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. 
- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. 
Thus, [2,0,2] is returned.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Idea&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need to check how every spell works with potions. But for the latter, we don't need to check every one. Instead, we can sort the array and stop checking at the element which gives us the result greater than &lt;code&gt;success&lt;/code&gt;. Traversing the sorted array based on binary search will give the best possible time complexity. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var successfulPairs = function(spells, potions, success) {
    let result = [];

    potions.sort((a, b) =&amp;gt; a - b);

    for(const spell of spells) {
        let l = 0;
        let r = potions.length - 1;
        let count = potions.length;

        while(l &amp;lt;= r) {
            let m = Math.floor((l + r) / 2);

            if(spell * potions[m] &amp;gt;= success) {
                r = m - 1;
                count = m;
            } else {
                l = m + 1;
            }
        }

        result.push(potions.length - count);
    }

    return result;
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The solution works in &lt;code&gt;O(nlogm + mlogm)&lt;/code&gt; time complexity and explained in &lt;a href="https://youtu.be/OKnm5oyAhWg"&gt;this video&lt;/a&gt; (Python version) by NeetCodeIO&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
