1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-06 07:51:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-08-10 21:35:14 +08:00
parent dee13a03bd
commit f6b8cd3b4b
81 changed files with 19629 additions and 9957 deletions

View File

@@ -0,0 +1,50 @@
<p data-end="128" data-start="0">You are given an integer array <code data-end="37" data-start="31">nums</code> of length <code data-end="51" data-start="48">n</code>.</p>
<p data-end="128" data-start="0">An array is <strong data-end="76" data-start="65">trionic</strong> if there exist indices <code data-end="117" data-start="100">0 &lt; p &lt; q &lt; n &minus; 1</code> such that:</p>
<ul>
<li data-end="170" data-start="132"><code data-end="144" data-start="132">nums[0...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="211" data-start="173"><code data-end="185" data-start="173">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="252" data-start="214"><code data-end="228" data-start="214">nums[q...n &minus; 1]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="315" data-is-last-node="" data-is-only-node="" data-start="254">Return <code data-end="267" data-start="261">true</code> if <code data-end="277" data-start="271">nums</code> is trionic, otherwise return <code data-end="314" data-start="307">false</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,4,2,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Pick <code data-end="91" data-start="84">p = 2</code>, <code data-end="100" data-start="93">q = 4</code>:</p>
<ul>
<li><code data-end="130" data-start="108">nums[0...2] = [1, 3, 5]</code> is strictly increasing (<code data-end="166" data-start="155">1 &lt; 3 &lt; 5</code>).</li>
<li><code data-end="197" data-start="175">nums[2...4] = [5, 4, 2]</code> is strictly decreasing (<code data-end="233" data-start="222">5 &gt; 4 &gt; 2</code>).</li>
<li><code data-end="262" data-start="242">nums[4...5] = [2, 6]</code> is strictly increasing (<code data-end="294" data-start="287">2 &lt; 6</code>).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to pick <code>p</code> and <code>q</code> to form the required three segments.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="41" data-start="26"><code data-end="39" data-start="26">3 &lt;= n &lt;= 100</code></li>
<li data-end="70" data-start="44"><code data-end="70" data-start="44">-1000 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

View File

@@ -0,0 +1,59 @@
<p data-end="191" data-start="0">You are given an integer array <code data-end="61" data-start="55">nums</code> of length <code data-end="75" data-start="72">n</code>.</p>
<p data-end="191" data-start="0">A <strong data-end="99" data-is-only-node="" data-start="79">trionic subarray</strong> is a contiguous subarray <code data-end="136" data-start="125">nums[l...r]</code> (with <code data-end="158" data-start="143">0 &lt;= l &lt; r &lt; n</code>) for which there exist indices <code>l &lt; p &lt; q &lt; r</code> such that:</p>
<ul>
<li data-end="267" data-start="230"><code data-end="241" data-start="230">nums[l...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="307" data-start="270"><code data-end="281" data-start="270">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="347" data-start="310"><code data-end="321" data-start="310">nums[q...r]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="609" data-is-last-node="" data-is-only-node="" data-start="349">Return the <strong>maximum</strong> sum of any trionic subarray in <code data-end="417" data-start="411">nums</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,-2,-1,-3,0,2,-1]</span></p>
<p><strong>Output:</strong> <span class="example-io">-4</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="129" data-start="72">Pick <code data-end="99" data-start="92">l = 1</code>, <code data-end="108" data-start="101">p = 2</code>, <code data-end="117" data-start="110">q = 3</code>, <code data-end="126" data-start="119">r = 5</code>:</p>
<ul>
<li data-end="203" data-start="132"><code data-end="166" data-start="132">nums[l...p] = nums[1...2] = [-2, -1]</code> is strictly increasing (<code data-end="200" data-start="191">-2 &lt; -1</code>).</li>
<li data-end="277" data-start="206"><code data-end="240" data-start="206">nums[p...q] = nums[2...3] = [-1, -3]</code> is strictly decreasing (<code data-end="274" data-start="265">-1 &gt; -3</code>)</li>
<li data-end="396" data-start="280"><code data-end="316" data-start="280">nums[q...r] = nums[3...5] = [-3, 0, 2]</code> is strictly increasing (<code data-end="353" data-start="341">-3 &lt; 0 &lt; 2</code>).</li>
<li data-end="396" data-start="280">Sum = <code>(-2) + (-1) + (-3) + 0 + 2 = -4</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,2,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="519" data-start="462">Pick <code data-end="489" data-start="482">l = 0</code>, <code data-end="498" data-start="491">p = 1</code>, <code data-end="507" data-start="500">q = 2</code>, <code data-end="516" data-start="509">r = 3</code>:</p>
<ul>
<li data-end="589" data-start="522"><code data-end="554" data-start="522">nums[l...p] = nums[0...1] = [1, 4]</code> is strictly increasing (<code data-end="586" data-start="579">1 &lt; 4</code>).</li>
<li data-end="659" data-start="592"><code data-end="624" data-start="592">nums[p...q] = nums[1...2] = [4, 2]</code> is strictly decreasing (<code data-end="656" data-start="649">4 &gt; 2</code>).</li>
<li data-end="754" data-is-last-node="" data-start="662"><code data-end="694" data-start="662">nums[q...r] = nums[2...3] = [2, 7]</code> is strictly increasing (<code data-end="726" data-start="719">2 &lt; 7</code>).</li>
<li data-end="754" data-is-last-node="" data-start="662">Sum = <code>1 + 4 + 2 + 7 = 14</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="883" data-start="851"><code data-end="881" data-start="851">4 &lt;= n = nums.length &lt;= 10<sup>5</sup></code></li>
<li data-end="914" data-start="886"><code data-end="912" data-start="886">-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li data-end="978" data-is-last-node="" data-start="917">It is guaranteed that at least one trionic subarray exists.</li>
</ul>

View File

@@ -0,0 +1,43 @@
<p>You are given an integer <code>n</code>.</p>
<p>A number is called <strong>special</strong> if:</p>
<ul>
<li>It is a <strong><span data-keyword="palindrome-integer">palindrome</span></strong>.</li>
<li>Every digit <code>k</code> in the number appears <strong>exactly</strong> <code>k</code> times.</li>
</ul>
<p>Return the <strong>smallest</strong> special number <strong>strictly </strong>greater than <code>n</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">22</span></p>
<p><strong>Explanation:</strong></p>
<p>22 is the smallest special number greater than 2, as it is a palindrome and the digit 2 appears exactly 2 times.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 33</span></p>
<p><strong>Output:</strong> <span class="example-io">212</span></p>
<p><strong>Explanation:</strong></p>
<p>212 is the smallest special number greater than 33, as it is a palindrome and the digits 1 and 2 appear exactly 1 and 2 times respectively.<br />
</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>

View File

@@ -0,0 +1,51 @@
<p>You are given an integer array <code>nums</code> with a length divisible by 3.</p>
<p>You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their <strong>median</strong>, and remove the selected elements from the array.</p>
<p>The <strong>median</strong> of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.</p>
<p>Return the <strong>maximum</strong> possible sum of the medians computed from the selected elements.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3,2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, <code>nums</code> becomes <code>[2, 1, 2]</code>.</li>
<li>In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, <code>nums</code> becomes empty.</li>
</ul>
<p>Hence, the sum of the medians is <code>3 + 2 = 5</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,10,10,10,10]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, <code>nums</code> becomes <code>[1, 10, 10]</code>.</li>
<li>In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, <code>nums</code> becomes empty.</li>
</ul>
<p>Hence, the sum of the medians is <code>10 + 10 = 20</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li>
<li><code>nums.length % 3 == 0</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,104 @@
<p>You are given two integers <code>n</code> and <code>k</code>.</p>
<p>For any positive integer <code>x</code>, define the following sequence:</p>
<ul>
<li><code>p<sub>0</sub> = x</code></li>
<li><code>p<sub>i+1</sub> = popcount(p<sub>i</sub>)</code> for all <code>i &gt;= 0</code>, where <code>popcount(y)</code> is the number of set bits (1&#39;s) in the binary representation of <code>y</code>.</li>
</ul>
<p>This sequence will eventually reach the value 1.</p>
<p>The <strong>popcount-depth</strong> of <code>x</code> is defined as the <strong>smallest</strong> integer <code>d &gt;= 0</code> such that <code>p<sub>d</sub> = 1</code>.</p>
<p>For example, if <code>x = 7</code> (binary representation <code>&quot;111&quot;</code>). Then, the sequence is: <code>7 &rarr; 3 &rarr; 2 &rarr; 1</code>, so the popcount-depth of 7 is 3.</p>
<p>Your task is to determine the number of integers in the range <code>[1, n]</code> whose popcount-depth is <strong>exactly</strong> equal to <code>k</code>.</p>
<p>Return the number of such integers.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The following integers in the range <code>[1, 4]</code> have popcount-depth exactly equal to 1:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">x</th>
<th align="center" style="border: 1px solid black;">Binary</th>
<th align="left" style="border: 1px solid black;">Sequence</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>&quot;10&quot;</code></td>
<td align="left" style="border: 1px solid black;"><code>2 &rarr; 1</code></td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;"><code>&quot;100&quot;</code></td>
<td align="left" style="border: 1px solid black;"><code>4 &rarr; 1</code></td>
</tr>
</tbody>
</table>
<p>Thus, the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The following integers in the range <code>[1, 7]</code> have popcount-depth exactly equal to 2:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">x</th>
<th style="border: 1px solid black;">Binary</th>
<th style="border: 1px solid black;">Sequence</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>&quot;11&quot;</code></td>
<td style="border: 1px solid black;"><code>3 &rarr; 2 &rarr; 1</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">5</td>
<td style="border: 1px solid black;"><code>&quot;101&quot;</code></td>
<td style="border: 1px solid black;"><code>5 &rarr; 2 &rarr; 1</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">6</td>
<td style="border: 1px solid black;"><code>&quot;110&quot;</code></td>
<td style="border: 1px solid black;"><code>6 &rarr; 2 &rarr; 1</code></td>
</tr>
</tbody>
</table>
<p>Thus, the answer is 3.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
<li><code>0 &lt;= k &lt;= 5</code></li>
</ul>

View File

@@ -0,0 +1,277 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>For any positive integer <code>x</code>, define the following sequence:</p>
<ul>
<li><code>p<sub>0</sub> = x</code></li>
<li><code>p<sub>i+1</sub> = popcount(p<sub>i</sub>)</code> for all <code>i &gt;= 0</code>, where <code>popcount(y)</code> is the number of set bits (1&#39;s) in the binary representation of <code>y</code>.</li>
</ul>
<p>This sequence will eventually reach the value 1.</p>
<p>The <strong>popcount-depth</strong> of <code>x</code> is defined as the <strong>smallest</strong> integer <code>d &gt;= 0</code> such that <code>p<sub>d</sub> = 1</code>.</p>
<p>For example, if <code>x = 7</code> (binary representation <code>&quot;111&quot;</code>). Then, the sequence is: <code>7 &rarr; 3 &rarr; 2 &rarr; 1</code>, so the popcount-depth of 7 is 3.</p>
<p>You are also given a 2D integer array <code>queries</code>, where each <code>queries[i]</code> is either:</p>
<ul>
<li><code>[1, l, r, k]</code> - <strong>Determine</strong> the number of indices <code>j</code> such that <code>l &lt;= j &lt;= r</code> and the <strong>popcount-depth</strong> of <code>nums[j]</code> is equal to <code>k</code>.</li>
<li><code>[2, idx, val]</code> - <strong>Update</strong> <code>nums[idx]</code> to <code>val</code>.</li>
</ul>
<p>Return an integer array <code>answer</code>, where <code>answer[i]</code> is the number of indices for the <code>i<sup>th</sup></code> query of type <code>[1, l, r, k]</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,4], queries = [[1,0,1,1],[2,1,1],[1,0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,1]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>queries[i]</code></th>
<th style="border: 1px solid black;"><code>nums</code></th>
<th style="border: 1px solid black;">binary(<code>nums</code>)</th>
<th style="border: 1px solid black;">popcount-<br />
depth</th>
<th style="border: 1px solid black;"><code>[l, r]</code></th>
<th style="border: 1px solid black;"><code>k</code></th>
<th style="border: 1px solid black;">Valid<br />
<code>nums[j]</code></th>
<th style="border: 1px solid black;">updated<br />
<code>nums</code></th>
<th style="border: 1px solid black;">Answer</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[1,0,1,1]</td>
<td style="border: 1px solid black;">[2,4]</td>
<td style="border: 1px solid black;">[10, 100]</td>
<td style="border: 1px solid black;">[1, 1]</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">2</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[2,1,1]</td>
<td style="border: 1px solid black;">[2,4]</td>
<td style="border: 1px solid black;">[10, 100]</td>
<td style="border: 1px solid black;">[1, 1]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">[2,1]</td>
<td style="border: 1px solid black;">&mdash;</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[1,0,1,0]</td>
<td style="border: 1px solid black;">[2,1]</td>
<td style="border: 1px solid black;">[10, 1]</td>
<td style="border: 1px solid black;">[1, 0]</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, the final <code>answer</code> is <code>[2, 1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,6], queries = [[1,0,2,2],[2,1,4],[1,1,2,1],[1,0,1,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1,0]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>queries[i]</code></th>
<th style="border: 1px solid black;"><code>nums</code></th>
<th style="border: 1px solid black;">binary(<code>nums</code>)</th>
<th style="border: 1px solid black;">popcount-<br />
depth</th>
<th style="border: 1px solid black;"><code>[l, r]</code></th>
<th style="border: 1px solid black;"><code>k</code></th>
<th style="border: 1px solid black;">Valid<br />
<code>nums[j]</code></th>
<th style="border: 1px solid black;">updated<br />
<code>nums</code></th>
<th style="border: 1px solid black;">Answer</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[1,0,2,2]</td>
<td style="border: 1px solid black;">[3, 5, 6]</td>
<td style="border: 1px solid black;">[11, 101, 110]</td>
<td style="border: 1px solid black;">[2, 2, 2]</td>
<td style="border: 1px solid black;">[0, 2]</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[0, 1, 2]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">3</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[2,1,4]</td>
<td style="border: 1px solid black;">[3, 5, 6]</td>
<td style="border: 1px solid black;">[11, 101, 110]</td>
<td style="border: 1px solid black;">[2, 2, 2]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">[3, 4, 6]</td>
<td style="border: 1px solid black;">&mdash;</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[1,1,2,1]</td>
<td style="border: 1px solid black;">[3, 4, 6]</td>
<td style="border: 1px solid black;">[11, 100, 110]</td>
<td style="border: 1px solid black;">[2, 1, 2]</td>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">[1,0,1,0]</td>
<td style="border: 1px solid black;">[3, 4, 6]</td>
<td style="border: 1px solid black;">[11, 100, 110]</td>
<td style="border: 1px solid black;">[2, 1, 2]</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">0</td>
</tr>
</tbody>
</table>
<p>Thus, the final <code>answer</code> is <code>[3, 1, 0]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2], queries = [[1,0,1,1],[2,0,3],[1,0,0,1],[1,0,0,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,0,1]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>queries[i]</code></th>
<th style="border: 1px solid black;"><code>nums</code></th>
<th style="border: 1px solid black;">binary(<code>nums</code>)</th>
<th style="border: 1px solid black;">popcount-<br />
depth</th>
<th style="border: 1px solid black;"><code>[l, r]</code></th>
<th style="border: 1px solid black;"><code>k</code></th>
<th style="border: 1px solid black;">Valid<br />
<code>nums[j]</code></th>
<th style="border: 1px solid black;">updated<br />
<code>nums</code></th>
<th style="border: 1px solid black;">Answer</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[1,0,1,1]</td>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">[1, 10]</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[2,0,3]</td>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">[1, 10]</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">[3, 2]</td>
<td style="border: 1px solid black;">&nbsp;</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[1,0,0,1]</td>
<td style="border: 1px solid black;">[3, 2]</td>
<td style="border: 1px solid black;">[11, 10]</td>
<td style="border: 1px solid black;">[2, 1]</td>
<td style="border: 1px solid black;">[0, 0]</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">0</td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">[1,0,0,2]</td>
<td style="border: 1px solid black;">[3, 2]</td>
<td style="border: 1px solid black;">[11, 10]</td>
<td style="border: 1px solid black;">[2, 1]</td>
<td style="border: 1px solid black;">[0, 0]</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[0]</td>
<td style="border: 1px solid black;">&mdash;</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, the final <code>answer</code> is <code>[1, 0, 1]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>15</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 3</code> or <code>4</code>
<ul>
<li><code>queries[i] == [1, l, r, k]</code> or,</li>
<li><code>queries[i] == [2, idx, val]</code></li>
<li><code>0 &lt;= l &lt;= r &lt;= n - 1</code></li>
<li><code>0 &lt;= k &lt;= 5</code></li>
<li><code>0 &lt;= idx &lt;= n - 1</code></li>
<li><code>1 &lt;= val &lt;= 10<sup>15</sup></code></li>
</ul>
</li>
</ul>

View File

@@ -0,0 +1,63 @@
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>An array is considered <strong>balanced</strong> if the value of its <strong>maximum</strong> element is <strong>at most</strong> <code>k</code> times the <strong>minimum</strong> element.</p>
<p>You may remove <strong>any</strong> number of elements from <code>nums</code> without making it <strong>empty</strong>.</p>
<p>Return the <strong>minimum</strong> number of elements to remove so that the remaining array is balanced.</p>
<p><strong>Note:</strong> An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[2] = 5</code> to get <code>nums = [2, 1]</code>.</li>
<li>Now <code>max = 2</code>, <code>min = 1</code> and <code>max &lt;= min * k</code> as <code>2 &lt;= 1 * 2</code>. Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,6,2,9], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[0] = 1</code> and <code>nums[3] = 9</code> to get <code>nums = [6, 2]</code>.</li>
<li>Now <code>max = 6</code>, <code>min = 2</code> and <code>max &lt;= min * k</code> as <code>6 &lt;= 2 * 3</code>. Thus, the answer is 2.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,6], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since <code>nums</code> is already balanced as <code>6 &lt;= 4 * 2</code>, no elements need to be removed.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,85 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>Partition the array into <strong>three</strong> (possibly empty) <span data-keyword="subsequence-array">subsequences</span> <code>A</code>, <code>B</code>, and <code>C</code> such that every element of <code>nums</code> belongs to <strong>exactly</strong> one subsequence.</p>
<p>Your goal is to <strong>maximize</strong> the value of: <code>XOR(A) + AND(B) + XOR(C)</code></p>
<p>where:</p>
<ul>
<li><code>XOR(arr)</code> denotes the bitwise XOR of all elements in <code>arr</code>. If <code>arr</code> is empty, its value is defined as 0.</li>
<li><code>AND(arr)</code> denotes the bitwise AND of all elements in <code>arr</code>. If <code>arr</code> is empty, its value is defined as 0.</li>
</ul>
<p>Return the <strong>maximum</strong> value achievable.</p>
<p><strong>Note:</strong> If multiple partitions result in the same <strong>maximum</strong> sum, you can consider any one of them.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal partition is:</p>
<ul>
<li><code>A = [3], XOR(A) = 3</code></li>
<li><code>B = [2], AND(B) = 2</code></li>
<li><code>C = [], XOR(C) = 0</code></li>
</ul>
<p>The maximum value of: <code>XOR(A) + AND(B) + XOR(C) = 3 + 2 + 0 = 5</code>. Thus, the answer is 5.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal partition is:</p>
<ul>
<li><code>A = [1], XOR(A) = 1</code></li>
<li><code>B = [2], AND(B) = 2</code></li>
<li><code>C = [3], XOR(C) = 3</code></li>
</ul>
<p>The maximum value of: <code>XOR(A) + AND(B) + XOR(C) = 1 + 2 + 3 = 6</code>. Thus, the answer is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,6,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal partition is:</p>
<ul>
<li><code>A = [7], XOR(A) = 7</code></li>
<li><code>B = [2,3], AND(B) = 2</code></li>
<li><code>C = [6], XOR(C) = 6</code></li>
</ul>
<p>The maximum value of: <code>XOR(A) + AND(B) + XOR(C) = 7 + 2 + 6 = 15</code>. Thus, the answer is 15.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 19</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,44 @@
<p>You are given a positive integer <code>n</code>. Determine whether <code>n</code> is divisible by the <strong>sum </strong>of the following two values:</p>
<ul>
<li>
<p>The <strong>digit sum</strong> of <code>n</code> (the sum of its digits).</p>
</li>
<li>
<p>The <strong>digit</strong> <strong>product</strong> of <code>n</code> (the product of its digits).</p>
</li>
</ul>
<p>Return <code>true</code> if <code>n</code> is divisible by this sum; otherwise, return <code>false</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 99</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Since 99 is divisible by the sum (9 + 9 = 18) plus product (9 * 9 = 81) of its digits (total 99), the output is true.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 23</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>Since 23 is not divisible by the sum (2 + 3 = 5) plus product (2 * 3 = 6) of its digits (total 11), the output is false.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,122 @@
<p>You are given a string <code>s</code> of length <code>n</code> and an integer array <code>order</code>, where <code>order</code> is a <strong><span data-keyword="permutation">permutation</span></strong> of the numbers in the range <code>[0, n - 1]</code>.</p>
<p>Starting from time <code>t = 0</code>, replace the character at index <code>order[t]</code> in <code>s</code> with <code>&#39;*&#39;</code> at each time step.</p>
<p>A <strong><span data-keyword="substring-nonempty">substring</span></strong> is <strong>valid</strong> if it contains <strong>at least</strong> one <code>&#39;*&#39;</code>.</p>
<p>A string is <strong>active</strong> if the total number of <strong>valid</strong> substrings is greater than or equal to <code>k</code>.</p>
<p>Return the <strong>minimum</strong> time <code>t</code> at which the string <code>s</code> becomes <strong>active</strong>. If it is impossible, return -1.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;, order = [1,0,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>t</code></th>
<th style="border: 1px solid black;"><code>order[t]</code></th>
<th style="border: 1px solid black;">Modified <code>s</code></th>
<th style="border: 1px solid black;">Valid Substrings</th>
<th style="border: 1px solid black;">Count</th>
<th style="border: 1px solid black;">Active<br />
(Count &gt;= k)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&quot;a*c&quot;</code></td>
<td style="border: 1px solid black;"><code>&quot;*&quot;</code>, <code>&quot;a*&quot;</code>, <code>&quot;*c&quot;</code>, <code>&quot;a*c&quot;</code></td>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;">Yes</td>
</tr>
</tbody>
</table>
<p>The string <code>s</code> becomes active at <code>t = 0</code>. Thus, the answer is 0.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;cat&quot;, order = [0,2,1], k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>t</code></th>
<th style="border: 1px solid black;"><code>order[t]</code></th>
<th style="border: 1px solid black;">Modified <code>s</code></th>
<th style="border: 1px solid black;">Valid Substrings</th>
<th style="border: 1px solid black;">Count</th>
<th style="border: 1px solid black;">Active<br />
(Count &gt;= k)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&quot;*at&quot;</code></td>
<td style="border: 1px solid black;"><code>&quot;*&quot;</code>, <code>&quot;*a&quot;</code>, <code>&quot;*at&quot;</code></td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">No</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&quot;*a*&quot;</code></td>
<td style="border: 1px solid black;"><code>&quot;*&quot;</code>, <code>&quot;*a&quot;</code>, <code>&quot;<code inline="">*a*&quot;</code></code>, <code>&quot;<code inline="">a*&quot;</code></code>, <code>&quot;*&quot;</code></td>
<td style="border: 1px solid black;">5</td>
<td style="border: 1px solid black;">No</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&quot;***&quot;</code></td>
<td style="border: 1px solid black;">All substrings (contain <code>&#39;*&#39;</code>)</td>
<td style="border: 1px solid black;">6</td>
<td style="border: 1px solid black;">Yes</td>
</tr>
</tbody>
</table>
<p>The string <code>s</code> becomes active at <code>t = 2</code>. Thus, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;xy&quot;, order = [0,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>Even after all replacements, it is impossible to obtain <code>k = 4</code> valid substrings. Thus, the answer is -1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
<li><code>order.length == n</code></li>
<li><code>0 &lt;= order[i] &lt;= n - 1</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>order</code> is a permutation of integers from 0 to <code>n - 1</code>.</li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,45 @@
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>x</code>, <code>y</code>, and <code>k</code>.</p>
<p>The integers <code>x</code> and <code>y</code> represent the row and column indices of the <strong>top-left</strong> corner of a <strong>square</strong> submatrix and the integer <code>k</code> represents the size (side length) of the square submatrix.</p>
<p>Your task is to flip the submatrix by reversing the order of its rows vertically.</p>
<p>Return the updated matrix.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/07/20/gridexmdrawio.png" style="width: 300px; height: 116px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/07/20/gridexm2drawio.png" style="width: 350px; height: 68px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 50</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 100</code></li>
<li><code>0 &lt;= x &lt; m</code></li>
<li><code>0 &lt;= y &lt; n</code></li>
<li><code>1 &lt;= k &lt;= min(m - x, n - y)</code></li>
</ul>

View File

@@ -0,0 +1,63 @@
<p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight</code> of length <code data-end="76" data-start="73">n</code>, representing the weights of <code data-end="109" data-start="106">n</code> parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment</strong> is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced</strong> if the weight of the <strong data-end="284" data-start="269">last parcel</strong> is <strong>strictly less</strong> than the <strong data-end="329" data-start="311">maximum weight</strong> among all parcels in that shipment.</p>
<p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping</strong>, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment</strong> (parcels may remain unshipped).</p>
<p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number</strong> of balanced shipments that can be formed.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [2,5,1,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:</p>
<ul>
<li data-end="163" data-start="140">Shipment 1: <code>[2, 5, 1]</code>
<ul>
<li data-end="195" data-start="168">Maximum parcel weight = 5</li>
<li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it&#39;s balanced.</li>
</ul>
</li>
<li data-end="299" data-start="279">Shipment 2: <code>[4, 3]</code>
<ul>
<li data-end="331" data-start="304">Maximum parcel weight = 4</li>
<li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it&#39;s balanced.</li>
</ul>
</li>
</ul>
<p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [4,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="635" data-start="574">No balanced shipment can be formed in this case:</p>
<ul>
<li data-end="772" data-start="639">A shipment <code>[4, 4]</code> has maximum weight 4 and the last parcel&#39;s weight is also 4, which is not strictly less. Thus, it&#39;s not balanced.</li>
<li data-end="885" data-start="775">Single-parcel shipments <code>[4]</code> have the last parcel weight equal to the maximum parcel weight, thus not balanced.</li>
</ul>
<p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 &lt;= weight[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,42 @@
<p>You are given an <code>m x n</code> matrix <code>grid</code> and a positive integer <code>k</code>. An <strong>island</strong> is a group of <strong>positive</strong> integers (representing land) that are <strong>4-directionally</strong> connected (horizontally or vertically).</p>
<p>The <strong>total value</strong> of an island is the sum of the values of all cells in the island.</p>
<p>Return the number of islands with a total value <strong>divisible by</strong> <code>k</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png" style="width: 200px; height: 200px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The grid contains four islands. The islands highlighted in blue have a total value that is divisible by 5, while the islands highlighted in red do not.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png" style="width: 200px; height: 150px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The grid contains six islands, each with a total value that is divisible by 3.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,136 @@
<p data-end="502" data-start="75">You are given a directed acyclic graph of <code>n</code>nodes numbered from 0to<code>n&minus;1</code>. This is represented by a 2D array <code data-end="201" data-start="194">edges</code> of length<font face="monospace"> <code>m</code></font>, where <code data-end="255" data-start="227">edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cost<sub>i</sub>]</code> indicates a oneway communication from node<code data-end="304" data-start="300">u<sub>i</sub></code> to node<code data-end="317" data-start="313">v<sub>i</sub></code> with a recovery cost of<code data-end="349" data-start="342">cost<sub>i</sub></code>.</p>
<p data-end="502" data-start="75">Some nodes may be offline. You are given a boolean array <code data-end="416" data-start="408">online</code> where <code data-end="441" data-start="423">online[i] = true</code> means node<code data-end="456" data-start="453">i</code> is online. Nodes 0 and <code>n&minus;1</code> are always online.</p>
<p data-end="547" data-start="504">A path from 0to <code>n&minus;1</code> is <strong data-end="541" data-start="532">valid</strong> if:</p>
<ul>
<li>All intermediate nodes on the path are online.</li>
<li data-end="676" data-start="605">The total recovery cost of all edges on the path does not exceed <code>k</code>.</li>
</ul>
<p data-end="771" data-start="653">For each valid path, define its <strong data-end="694" data-start="685">score</strong> as the minimum edgecost along that path.</p>
<p data-end="913" data-start="847">Return the <strong>maximum</strong> path score (i.e., the largest <strong>minimum</strong>-edge cost) among all valid paths. If no valid path exists, return -1.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1,5],[1,3,10],[0,2,3],[2,3,4]], online = [true,true,true,true], k = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/06/06/graph-10.png" style="width: 239px; height: 267px;" /></p>
<ul data-end="551" data-start="146">
<li data-end="462" data-start="146">
<p data-end="206" data-start="148">The graph has two possible routes from node 0 to node 3:</p>
<ol data-end="462" data-start="209">
<li data-end="315" data-start="209">
<p data-end="228" data-start="212">Path <code>0 &rarr; 1 &rarr; 3</code></p>
<ul data-end="315" data-start="234">
<li data-end="315" data-start="234">
<p data-end="315" data-start="236">Total cost = <code>5 + 10 = 15</code>, which exceeds k (<code>15 &gt; 10</code>), so this path is invalid.</p>
</li>
</ul>
</li>
<li data-end="462" data-start="318">
<p data-end="337" data-start="321">Path <code>0 &rarr; 2 &rarr; 3</code></p>
<ul data-end="462" data-start="343">
<li data-end="397" data-start="343">
<p data-end="397" data-start="345">Total cost = <code>3 + 4 = 7 &lt;= k</code>, so this path is valid.</p>
</li>
<li data-end="462" data-start="403">
<p data-end="462" data-start="405">The minimum edgecost along this path is <code>min(3, 4) = 3</code>.</p>
</li>
</ul>
</li>
</ol>
</li>
<li data-end="551" data-start="463">
<p data-end="551" data-start="465">There are no other valid paths. Hence, the maximum among all valid pathscores is 3.</p>
</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1,7],[1,4,5],[0,2,6],[2,3,6],[3,4,2],[2,4,6]], online = [true,true,true,false,true], k = 12</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/06/06/graph-11.png" style="width: 343px; height: 194px;" /></p>
<ul>
<li data-end="790" data-start="726">
<p data-end="790" data-start="728">Node 3 is offline, so any path passing through 3 is invalid.</p>
</li>
<li data-end="1231" data-start="791">
<p data-end="837" data-start="793">Consider the remaining routes from 0 to 4:</p>
<ol data-end="1231" data-start="840">
<li data-end="985" data-start="840">
<p data-end="859" data-start="843">Path <code>0 &rarr; 1 &rarr; 4</code></p>
<ul data-end="985" data-start="865">
<li data-end="920" data-start="865">
<p data-end="920" data-start="867">Total cost = <code>7 + 5 = 12 &lt;= k</code>, so this path is valid.</p>
</li>
<li data-end="985" data-start="926">
<p data-end="985" data-start="928">The minimum edgecost along this path is <code>min(7, 5) = 5</code>.</p>
</li>
</ul>
</li>
<li data-end="1083" data-start="988">
<p data-end="1011" data-start="991">Path <code>0 &rarr; 2 &rarr; 3 &rarr; 4</code></p>
<ul data-end="1083" data-start="1017">
<li data-end="1083" data-start="1017">
<p data-end="1083" data-start="1019">Node 3 is offline, so this path is invalid regardless of cost.</p>
</li>
</ul>
</li>
<li data-end="1231" data-start="1086">
<p data-end="1105" data-start="1089">Path <code>0 &rarr; 2 &rarr; 4</code></p>
<ul data-end="1231" data-start="1111">
<li data-end="1166" data-start="1111">
<p data-end="1166" data-start="1113">Total cost = <code>6 + 6 = 12 &lt;= k</code>, so this path is valid.</p>
</li>
<li data-end="1231" data-start="1172">
<p data-end="1231" data-start="1174">The minimum edgecost along this path is <code>min(6, 6) = 6</code>.</p>
</li>
</ul>
</li>
</ol>
</li>
<li data-end="1314" data-is-last-node="" data-start="1232">
<p data-end="1314" data-is-last-node="" data-start="1234">Among the two valid paths, their scores are 5 and 6. Therefore, the answer is 6.</p>
</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="42" data-start="20"><code data-end="40" data-start="20">n == online.length</code></li>
<li data-end="63" data-start="45"><code data-end="61" data-start="45">2 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
<li data-end="102" data-start="66"><code data-end="100" data-start="66">0 &lt;= m == edges.length &lt;= </code><code>min(10<sup>5</sup>, n * (n - 1) / 2)</code></li>
<li data-end="102" data-start="66"><code data-end="127" data-start="105">edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cost<sub>i</sub>]</code></li>
<li data-end="151" data-start="132"><code data-end="149" data-start="132">0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li data-end="166" data-start="154"><code data-end="164" data-start="154">u<sub>i</sub> != v<sub>i</sub></code></li>
<li data-end="191" data-start="169"><code data-end="189" data-start="169">0 &lt;= cost<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li data-end="213" data-start="194"><code data-end="211" data-start="194">0 &lt;= k &lt;= 5 * 10<sup>13</sup></code></li>
<li data-end="309" data-start="216"><code data-end="227" data-start="216">online[i]</code> is either <code data-end="244" data-is-only-node="" data-start="238">true</code> or <code data-end="255" data-start="248">false</code>, and both <code data-end="277" data-start="266">online[0]</code> and <code data-end="295" data-start="282">online[n &minus; 1]</code> are <code data-end="306" data-start="300">true</code>.</li>
<li data-end="362" data-is-last-node="" data-start="312">The given graph is a directed acyclic graph.</li>
</ul>

View File

@@ -0,0 +1,51 @@
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation-array">permutation</span></strong> of the numbers in the range <code>[0..n - 1]</code>.</p>
<p>You may swap elements at indices <code>i</code> and <code>j</code> <strong>only if</strong> <code>nums[i] AND nums[j] == k</code>, where <code>AND</code> denotes the bitwise AND operation and <code>k</code> is a <strong>non-negative</strong> integer.</p>
<p>Return the <strong>maximum</strong> value of <code>k</code> such that the array can be sorted in <strong>non-decreasing</strong> order using any number of such swaps. If <code>nums</code> is already sorted, return 0.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 1</code>. Swapping <code>nums[1] = 3</code> and <code>nums[3] = 1</code> is allowed since <code>nums[1] AND nums[3] == 1</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 2</code>. Swapping <code>nums[2] = 3</code> and <code>nums[3] = 2</code> is allowed since <code>nums[2] AND nums[3] == 2</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Only <code>k = 0</code> allows sorting since no greater <code>k</code> allows the required swaps where <code>nums[i] AND nums[j] == k</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= n - 1</code></li>
<li><code>nums</code> is a permutation of integers from <code>0</code> to <code>n - 1</code>.</li>
</ul>

View File

@@ -0,0 +1,50 @@
<p>You are given a string <code>s</code> consisting of uppercase English letters.</p>
<p>You are allowed to insert <strong>at most one</strong> uppercase English letter at <strong>any</strong> position (including the beginning or end) of the string.</p>
<p>Return the <strong>maximum</strong> number of <code>&quot;LCT&quot;</code> <span data-keyword="subsequence-string-nonempty">subsequences</span> that can be formed in the resulting string after <strong>at most one insertion</strong>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;LMCT&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>We can insert a <code>&quot;L&quot;</code> at the beginning of the string s to make <code>&quot;LLMCT&quot;</code>, which has 2 subsequences, at indices [0, 3, 4] and [1, 3, 4].</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;LCCT&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>We can insert a <code>&quot;L&quot;</code> at the beginning of the string s to make <code>&quot;LLCCT&quot;</code>, which has 4 subsequences, at indices [0, 2, 4], [0, 3, 4], [1, 2, 4] and [1, 3, 4].</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;L&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Since it is not possible to obtain the subsequence <code>&quot;LCT&quot;</code> by inserting a single letter, the result is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consists of uppercase English letters.</li>
</ul>

View File

@@ -0,0 +1,194 @@
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] &lt;= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[1, 2]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:<strong></strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[ ]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == value.length == limit.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= value[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= limit[i] &lt;= n</code></li>
</ul>

View File

@@ -0,0 +1,104 @@
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> &ndash; the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> &ndash; how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> &ndash; the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> &ndash; how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 &rarr; water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 &rarr; land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 &rarr; water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 &rarr; land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 &rarr; land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 &rarr; water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong></strong></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 &lt;= n, m &lt;= 100</code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 &lt;= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] &lt;= 1000</code></li>
</ul>

View File

@@ -0,0 +1,104 @@
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> &ndash; the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> &ndash; how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> &ndash; the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> &ndash; how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 &rarr; water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 &rarr; land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 &rarr; water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 &rarr; land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong></p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 &rarr; land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 &rarr; water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong></strong></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 &lt;= n, m &lt;= 5 * 10<sup>4</sup></code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 &lt;= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,142 @@
<p>Table: <code>stores</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| store_id | int |
| store_name | varchar |
| location | varchar |
+-------------+---------+
store_id is the unique identifier for this table.
Each row contains information about a store and its location.
</pre>
<p>Table: <code>inventory</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| inventory_id| int |
| store_id | int |
| product_name| varchar |
| quantity | int |
| price | decimal |
+-------------+---------+
inventory_id is the unique identifier for this table.
Each row represents the inventory of a specific product at a specific store.
</pre>
<p>Write a solution to find stores that have <strong>inventory imbalance</strong> - stores where the most expensive product has lower stock than the cheapest product.</p>
<ul>
<li>For each store, identify the <strong>most expensive product</strong> (highest price) and its quantity</li>
<li>For each store, identify the <strong>cheapest product</strong> (lowest price) and its quantity</li>
<li>A store has inventory imbalance if the most expensive product&#39;s quantity is <strong>less than</strong> the cheapest product&#39;s quantity</li>
<li>Calculate the <strong>imbalance ratio</strong> as (cheapest_quantity / most_expensive_quantity)</li>
<li><strong>Round</strong> the imbalance ratio to <strong>2</strong> decimal places</li>
<li>Only include stores that have <strong>at least </strong><code>3</code><strong> different products</strong></li>
</ul>
<p>Return <em>the result table ordered by imbalance ratio in <strong>descending</strong> order, then by store name in <strong>ascending</strong> order</em>.</p>
<p>The result format is in the following example.</p>
<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>stores table:</p>
<pre class="example-io">
+----------+----------------+-------------+
| store_id | store_name | location |
+----------+----------------+-------------+
| 1 | Downtown Tech | New York |
| 2 | Suburb Mall | Chicago |
| 3 | City Center | Los Angeles |
| 4 | Corner Shop | Miami |
| 5 | Plaza Store | Seattle |
+----------+----------------+-------------+
</pre>
<p>inventory table:</p>
<pre class="example-io">
+--------------+----------+--------------+----------+--------+
| inventory_id | store_id | product_name | quantity | price |
+--------------+----------+--------------+----------+--------+
| 1 | 1 | Laptop | 5 | 999.99 |
| 2 | 1 | Mouse | 50 | 19.99 |
| 3 | 1 | Keyboard | 25 | 79.99 |
| 4 | 1 | Monitor | 15 | 299.99 |
| 5 | 2 | Phone | 3 | 699.99 |
| 6 | 2 | Charger | 100 | 25.99 |
| 7 | 2 | Case | 75 | 15.99 |
| 8 | 2 | Headphones | 20 | 149.99 |
| 9 | 3 | Tablet | 2 | 499.99 |
| 10 | 3 | Stylus | 80 | 29.99 |
| 11 | 3 | Cover | 60 | 39.99 |
| 12 | 4 | Watch | 10 | 299.99 |
| 13 | 4 | Band | 25 | 49.99 |
| 14 | 5 | Camera | 8 | 599.99 |
| 15 | 5 | Lens | 12 | 199.99 |
+--------------+----------+--------------+----------+--------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+----------+----------------+-------------+------------------+--------------------+------------------+
| store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio |
+----------+----------------+-------------+------------------+--------------------+------------------+
| 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 |
| 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 |
| 2 | Suburb Mall | Chicago | Phone | Case | 25.00 |
+----------+----------------+-------------+------------------+--------------------+------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Downtown Tech (store_id = 1):</strong>
<ul>
<li>Most expensive product: Laptop ($999.99) with quantity 5</li>
<li>Cheapest product: Mouse ($19.99) with quantity 50</li>
<li>Inventory imbalance: 5 &lt; 50 (expensive product has lower stock)</li>
<li>Imbalance ratio: 50 / 5 = 10.00</li>
<li>Has 4 products (&ge; 3), so qualifies</li>
</ul>
</li>
<li><strong>Suburb Mall (store_id = 2):</strong>
<ul>
<li>Most expensive product: Phone ($699.99) with quantity 3</li>
<li>Cheapest product: Case ($15.99) with quantity 75</li>
<li>Inventory imbalance: 3 &lt; 75 (expensive product has lower stock)</li>
<li>Imbalance ratio: 75 / 3 = 25.00</li>
<li>Has 4 products (&ge; 3), so qualifies</li>
</ul>
</li>
<li><strong>City Center (store_id = 3):</strong>
<ul>
<li>Most expensive product: Tablet ($499.99) with quantity 2</li>
<li>Cheapest product: Stylus ($29.99) with quantity 80</li>
<li>Inventory imbalance: 2 &lt; 80 (expensive product has lower stock)</li>
<li>Imbalance ratio: 80 / 2 = 40.00</li>
<li>Has 3 products (&ge; 3), so qualifies</li>
</ul>
</li>
<li><strong>Stores not included:</strong>
<ul>
<li>Corner Shop (store_id = 4): Only has 2 products (Watch, Band) - doesn&#39;t meet minimum 3 products requirement</li>
<li>Plaza Store (store_id = 5): Only has 2 products (Camera, Lens) - doesn&#39;t meet minimum 3 products requirement</li>
</ul>
</li>
</ul>
<p>The Results table is ordered by imbalance ratio in descending order, then by store name in ascending order</p>
</div>

View File

@@ -0,0 +1,113 @@
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an array <code>queries</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, threshold<sub>i</sub>]</code>.</p>
<p>Return an array of integers <code data-end="33" data-start="28">ans</code> where <code data-end="48" data-start="40">ans[i]</code> is equal to the element in the subarray <code data-end="102" data-start="89">nums[l<sub>i</sub>...r<sub>i</sub>]</code> that appears <strong>at least</strong> <code data-end="137" data-start="125">threshold<sub>i</sub></code> times, selecting the element with the <strong>highest</strong> frequency (choosing the <strong>smallest</strong> in case of a tie), or -1 if no such element <em>exists</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,2,1,1], queries = [[0,5,4],[0,3,3],[2,3,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,-1,2]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="left" style="border: 1px solid black;">Query</th>
<th align="left" style="border: 1px solid black;">Sub-array</th>
<th align="left" style="border: 1px solid black;">Threshold</th>
<th align="left" style="border: 1px solid black;">Frequency table</th>
<th align="left" style="border: 1px solid black;">Answer</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" style="border: 1px solid black;">[0, 5, 4]</td>
<td align="left" style="border: 1px solid black;">[1, 1, 2, 2, 1, 1]</td>
<td align="left" style="border: 1px solid black;">4</td>
<td align="left" style="border: 1px solid black;">1 &rarr; 4, 2 &rarr; 2</td>
<td align="left" style="border: 1px solid black;">1</td>
</tr>
<tr>
<td align="left" style="border: 1px solid black;">[0, 3, 3]</td>
<td align="left" style="border: 1px solid black;">[1, 1, 2, 2]</td>
<td align="left" style="border: 1px solid black;">3</td>
<td align="left" style="border: 1px solid black;">1 &rarr; 2, 2 &rarr; 2</td>
<td align="left" style="border: 1px solid black;">-1</td>
</tr>
<tr>
<td align="left" style="border: 1px solid black;">[2, 3, 2]</td>
<td align="left" style="border: 1px solid black;">[2, 2]</td>
<td align="left" style="border: 1px solid black;">2</td>
<td align="left" style="border: 1px solid black;">2 &rarr; 2</td>
<td align="left" style="border: 1px solid black;">2</td>
</tr>
</tbody>
</table>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,3,2,3,2,3], queries = [[0,6,4],[1,5,2],[2,4,1],[3,3,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,3,2]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="left" style="border: 1px solid black;">Query</th>
<th align="left" style="border: 1px solid black;">Sub-array</th>
<th align="left" style="border: 1px solid black;">Threshold</th>
<th align="left" style="border: 1px solid black;">Frequency table</th>
<th align="left" style="border: 1px solid black;">Answer</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" style="border: 1px solid black;">[0, 6, 4]</td>
<td align="left" style="border: 1px solid black;">[3, 2, 3, 2, 3, 2, 3]</td>
<td align="left" style="border: 1px solid black;">4</td>
<td align="left" style="border: 1px solid black;">3 &rarr; 4, 2 &rarr; 3</td>
<td align="left" style="border: 1px solid black;">3</td>
</tr>
<tr>
<td align="left" style="border: 1px solid black;">[1, 5, 2]</td>
<td align="left" style="border: 1px solid black;">[2, 3, 2, 3, 2]</td>
<td align="left" style="border: 1px solid black;">2</td>
<td align="left" style="border: 1px solid black;">2 &rarr; 3, 3 &rarr; 2</td>
<td align="left" style="border: 1px solid black;">2</td>
</tr>
<tr>
<td align="left" style="border: 1px solid black;">[2, 4, 1]</td>
<td align="left" style="border: 1px solid black;">[3, 2, 3]</td>
<td align="left" style="border: 1px solid black;">1</td>
<td align="left" style="border: 1px solid black;">3 &rarr; 2, 2 &rarr; 1</td>
<td align="left" style="border: 1px solid black;">3</td>
</tr>
<tr>
<td align="left" style="border: 1px solid black;">[3, 3, 1]</td>
<td align="left" style="border: 1px solid black;">[2]</td>
<td align="left" style="border: 1px solid black;">1</td>
<td align="left" style="border: 1px solid black;">2 &rarr; 1</td>
<td align="left" style="border: 1px solid black;">2</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="51" data-start="19"><code data-end="49" data-start="19">1 &lt;= nums.length == n &lt;= 10<sup>4</sup></code></li>
<li data-end="82" data-start="54"><code data-end="80" data-start="54">1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li data-end="120" data-start="85"><code data-end="118" data-start="85">1 &lt;= queries.length &lt;= 5 * 10<sup>4</sup></code></li>
<li data-end="195" data-start="123"><code data-end="193" data-is-only-node="" data-start="155">queries[i] = [l<sub>i</sub>, r<sub>i</sub>, threshold<sub>i</sub>]</code></li>
<li data-end="221" data-start="198"><code data-end="219" data-start="198">0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; n</code></li>
<li data-end="259" data-is-last-node="" data-start="224"><code data-end="259" data-is-last-node="" data-start="224">1 &lt;= threshold<sub>i</sub> &lt;= r<sub>i</sub> - l<sub>i</sub> + 1</code></li>
</ul>

View File

@@ -0,0 +1,55 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>Split <code>nums</code> into two arrays <code>A</code> and <code>B</code> using the following rule:</p>
<ul>
<li>Elements at <strong><span data-keyword="prime-number">prime</span></strong> indices in <code>nums</code> must go into array <code>A</code>.</li>
<li>All other elements must go into array <code>B</code>.</li>
</ul>
<p>Return the <strong>absolute</strong> difference between the sums of the two arrays: <code>|sum(A) - sum(B)|</code>.</p>
<p><strong>Note:</strong> An empty array has a sum of 0.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The only prime index in the array is 2, so <code>nums[2] = 4</code> is placed in array <code>A</code>.</li>
<li>The remaining elements, <code>nums[0] = 2</code> and <code>nums[1] = 3</code> are placed in array <code>B</code>.</li>
<li><code>sum(A) = 4</code>, <code>sum(B) = 2 + 3 = 5</code>.</li>
<li>The absolute difference is <code>|4 - 5| = 1</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-1,5,7,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The prime indices in the array are 2 and 3, so <code>nums[2] = 7</code> and <code>nums[3] = 0</code> are placed in array <code>A</code>.</li>
<li>The remaining elements, <code>nums[0] = -1</code> and <code>nums[1] = 5</code> are placed in array <code>B</code>.</li>
<li><code>sum(A) = 7 + 0 = 7</code>, <code>sum(B) = -1 + 5 = 4</code>.</li>
<li>The absolute difference is <code>|7 - 4| = 3</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,51 @@
<p data-end="189" data-start="146">You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of the <code>i<sup>th</sup></code> point on the Cartesian plane.</p>
<p data-end="579" data-start="405">A <strong>horizontal</strong> <strong>trapezoid</strong> is a convex quadrilateral with <strong data-end="496" data-start="475">at least one pair</strong> of horizontal sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have the same slope.</p>
<p data-end="579" data-start="405">Return the <em data-end="330" data-start="297"> number of unique </em><strong><em>horizontal</em> <em>trapezoids</em></strong> that can be formed by choosing any four distinct points from <code>points</code>.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">points = [[1,0],[2,0],[3,0],[2,2],[3,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-6.png" style="width: 250px; height: 250px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-7.png" style="width: 250px; height: 250px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-8.png" style="width: 250px; height: 250px;" /></p>
<p>There are three distinct ways to pick four points that form a horizontal trapezoid:</p>
<ul>
<li data-end="247" data-start="193">Using points <code data-end="213" data-start="206">[1,0]</code>, <code data-end="222" data-start="215">[2,0]</code>, <code data-end="231" data-start="224">[3,2]</code>, and <code data-end="244" data-start="237">[2,2]</code>.</li>
<li data-end="305" data-start="251">Using points <code data-end="271" data-start="264">[2,0]</code>, <code data-end="280" data-start="273">[3,0]</code>, <code data-end="289" data-start="282">[3,2]</code>, and <code data-end="302" data-start="295">[2,2]</code>.</li>
<li data-end="361" data-start="309">Using points <code data-end="329" data-start="322">[1,0]</code>, <code data-end="338" data-start="331">[3,0]</code>, <code data-end="347" data-start="340">[3,2]</code>, and <code data-end="360" data-start="353">[2,2]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">points = [[0,0],[1,0],[0,1],[2,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-5.png" style="width: 250px; height: 250px;" /></p>
<p>There is only one horizontal trapezoid that can be formed.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>4 &lt;= points.length &lt;= 10<sup>5</sup></code></li>
<li><code>&ndash;10<sup>8</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>8</sup></code></li>
<li>All points are pairwise distinct.</li>
</ul>

View File

@@ -0,0 +1,48 @@
<p data-end="189" data-start="146">You are given a 2D integer array <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of the <code>i<sup>th</sup></code> point on the Cartesian plane.</p>
<p data-end="189" data-start="146">Return <em data-end="330" data-start="297">the number of unique </em><em>trapezoids</em> that can be formed by choosing any four distinct points from <code>points</code>.</p>
<p data-end="579" data-start="405">A<b> </b><strong>trapezoid</strong> is a convex quadrilateral with <strong data-end="496" data-start="475">at least one pair</strong> of parallel sides. Two lines are parallel if and only if they have the same slope.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">points = [[-3,2],[3,0],[2,3],[3,2],[2,-3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-4.png" style="width: 250px; height: 250px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-3.png" style="width: 250px; height: 250px;" /></p>
<p>There are two distinct ways to pick four points that form a trapezoid:</p>
<ul>
<li>The points <code>[-3,2], [2,3], [3,2], [2,-3]</code> form one trapezoid.</li>
<li>The points <code>[2,3], [3,2], [3,0], [2,-3]</code> form another trapezoid.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">points = [[0,0],[1,0],[0,1],[2,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-5.png" style="width: 250px; height: 250px;" /></p>
<p>There is only one trapezoid which can be formed.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>4 &lt;= points.length &lt;= 500</code></li>
<li><code>&ndash;1000 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 1000</code></li>
<li>All points are pairwise distinct.</li>
</ul>

View File

@@ -0,0 +1,73 @@
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p>
<p>You start at index 0, and your goal is to reach index <code>n - 1</code>.</p>
<p>From any index <code>i</code>, you may perform one of the following operations:</p>
<ul>
<li><strong>Adjacent Step</strong>: Jump to index <code>i + 1</code> or <code>i - 1</code>, if the index is within bounds.</li>
<li><strong>Prime Teleportation</strong>: If <code>nums[i]</code> is a <span data-keyword="prime-number">prime number</span> <code>p</code>, you may instantly jump to any index <code>j != i</code> such that <code>nums[j] % p == 0</code>.</li>
</ul>
<p>Return the <strong>minimum</strong> number of jumps required to reach index <code>n - 1</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal sequence of jumps is:</p>
<ul>
<li>Start at index <code>i = 0</code>. Take an adjacent step to index 1.</li>
<li>At index <code>i = 1</code>, <code>nums[1] = 2</code> is a prime number. Therefore, we teleport to index <code>i = 3</code> as <code>nums[3] = 6</code> is divisible by 2.</li>
</ul>
<p>Thus, the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal sequence of jumps is:</p>
<ul>
<li>Start at index <code>i = 0</code>. Take an adjacent step to index <code>i = 1</code>.</li>
<li>At index <code>i = 1</code>, <code>nums[1] = 3</code> is a prime number. Therefore, we teleport to index <code>i = 4</code> since <code>nums[4] = 9</code> is divisible by 3.</li>
</ul>
<p>Thus, the answer is 2.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,6,5,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since no teleportation is possible, we move through <code>0 &rarr; 1 &rarr; 2 &rarr; 3</code>. Thus, the answer is 3.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>