@@ -91510,7 +91510,7 @@ <h3 id="solution-1-enumeration">Solution 1: Enumeration</h3>
9151091510<p>We can enumerate all <span class="arithmatex">\(i\)</span>, <span class="arithmatex">\(j\)</span>, and <span class="arithmatex">\(k\)</span> where <span class="arithmatex">\(i \lt j \lt k\)</span>, and check if they simultaneously satisfy <span class="arithmatex">\(|\textit{arr}[i] - \textit{arr}[j]| \le a\)</span>, <span class="arithmatex">\(|\textit{arr}[j] - \textit{arr}[k]| \le b\)</span>, and <span class="arithmatex">\(|\textit{arr}[i] - \textit{arr}[k]| \le c\)</span>. If they do, we increment the answer by one.</p>
9151191511<p>After enumerating all possible triplets, we get the answer.</p>
9151291512<p>The time complexity is <span class="arithmatex">\(O(n^3)\)</span>, where <span class="arithmatex">\(n\)</span> is the length of the array <span class="arithmatex">\(\textit{arr}\)</span>. The space complexity is <span class="arithmatex">\(O(1)\)</span>.</p>
91513- <div class="tabbed-set tabbed-alternate" data-tabs="1:5 "><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label></div>
91513+ <div class="tabbed-set tabbed-alternate" data-tabs="1:7 "><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" />< div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label><label for="__tabbed_1_6">Rust</label><label for="__tabbed_1_7">C# </label></div>
9151491514<div class="tabbed-content">
9151591515<div class="tabbed-block">
9151691516<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
@@ -91686,6 +91686,82 @@ <h3 id="solution-1-enumeration">Solution 1: Enumeration</h3>
9168691686<span class="p">}</span>
9168791687</code></pre></div></td></tr></table></div>
9168891688</div>
91689+ <div class="tabbed-block">
91690+ <div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
91691+ <span class="normal"> 2</span>
91692+ <span class="normal"> 3</span>
91693+ <span class="normal"> 4</span>
91694+ <span class="normal"> 5</span>
91695+ <span class="normal"> 6</span>
91696+ <span class="normal"> 7</span>
91697+ <span class="normal"> 8</span>
91698+ <span class="normal"> 9</span>
91699+ <span class="normal">10</span>
91700+ <span class="normal">11</span>
91701+ <span class="normal">12</span>
91702+ <span class="normal">13</span>
91703+ <span class="normal">14</span>
91704+ <span class="normal">15</span>
91705+ <span class="normal">16</span>
91706+ <span class="normal">17</span>
91707+ <span class="normal">18</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Solution</span><span class="w"> </span><span class="p">{</span>
91708+ <span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="nf">count_good_triplets</span><span class="p">(</span><span class="n">arr</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o"><</span><span class="kt">i32</span><span class="o">></span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">:</span><span class="w"> </span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">:</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">-></span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span>
91709+ <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">.</span><span class="n">len</span><span class="p">();</span>
91710+ <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ans</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
91711+
91712+ <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
91713+ <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
91714+ <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
91715+ <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">j</span><span class="p">]).</span><span class="n">abs</span><span class="p">()</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">k</span><span class="p">]).</span><span class="n">abs</span><span class="p">()</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">k</span><span class="p">]).</span><span class="n">abs</span><span class="p">()</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">{</span>
91716+ <span class="w"> </span><span class="n">ans</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
91717+ <span class="w"> </span><span class="p">}</span>
91718+ <span class="w"> </span><span class="p">}</span>
91719+ <span class="w"> </span><span class="p">}</span>
91720+ <span class="w"> </span><span class="p">}</span>
91721+
91722+ <span class="w"> </span><span class="n">ans</span>
91723+ <span class="w"> </span><span class="p">}</span>
91724+ <span class="p">}</span>
91725+ </code></pre></div></td></tr></table></div>
91726+ </div>
91727+ <div class="tabbed-block">
91728+ <div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
91729+ <span class="normal"> 2</span>
91730+ <span class="normal"> 3</span>
91731+ <span class="normal"> 4</span>
91732+ <span class="normal"> 5</span>
91733+ <span class="normal"> 6</span>
91734+ <span class="normal"> 7</span>
91735+ <span class="normal"> 8</span>
91736+ <span class="normal"> 9</span>
91737+ <span class="normal">10</span>
91738+ <span class="normal">11</span>
91739+ <span class="normal">12</span>
91740+ <span class="normal">13</span>
91741+ <span class="normal">14</span>
91742+ <span class="normal">15</span>
91743+ <span class="normal">16</span>
91744+ <span class="normal">17</span>
91745+ <span class="normal">18</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">public</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">Solution</span><span class="w"> </span><span class="p">{</span>
91746+ <span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">CountGoodTriplets</span><span class="p">(</span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">arr</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
91747+ <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span>
91748+ <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ans</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
91749+
91750+ <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
91751+ <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
91752+ <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">k</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
91753+ <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">Math</span><span class="p">.</span><span class="n">Abs</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">j</span><span class="p">])</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">Math</span><span class="p">.</span><span class="n">Abs</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">k</span><span class="p">])</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">Math</span><span class="p">.</span><span class="n">Abs</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">k</span><span class="p">])</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
91754+ <span class="w"> </span><span class="o">++</span><span class="n">ans</span><span class="p">;</span>
91755+ <span class="w"> </span><span class="p">}</span>
91756+ <span class="w"> </span><span class="p">}</span>
91757+ <span class="w"> </span><span class="p">}</span>
91758+ <span class="w"> </span><span class="p">}</span>
91759+
91760+ <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">ans</span><span class="p">;</span>
91761+ <span class="w"> </span><span class="p">}</span>
91762+ <span class="p">}</span>
91763+ </code></pre></div></td></tr></table></div>
91764+ </div>
9168991765</div>
9169091766</div>
9169191767<!-- solution:end -->
0 commit comments