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-05-15 01:05:54 +08:00
parent d105a455da
commit 59597532bc
65 changed files with 17476 additions and 9757 deletions

View File

@@ -0,0 +1,91 @@
<p>You are given an integer <code>n</code> and a <strong>Directed Acyclic Graph (DAG)</strong> with <code>n</code> nodes labeled from 0 to <code>n - 1</code>. This is represented by a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates a directed edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
<p>You are also given two integers, <code>k</code> and <code>t</code>.</p>
<p>Your task is to determine the <strong>maximum</strong> possible sum of edge weights for any path in the graph such that:</p>
<ul>
<li>The path contains <strong>exactly</strong> <code>k</code> edges.</li>
<li>The total sum of edge weights in the path is <strong>strictly</strong> less than <code>t</code>.</li>
</ul>
<p>Return the <strong>maximum</strong> possible sum of weights for such a path. If no such path exists, return <code>-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">n = 3, edges = [[0,1,1],[1,2,2]], k = 2, t = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061326.png" style="width: 180px; height: 162px;" /></p>
<ul>
<li>The only path with <code>k = 2</code> edges is <code>0 -&gt; 1 -&gt; 2</code> with weight <code>1 + 2 = 3 &lt; t</code>.</li>
<li>Thus, the maximum possible sum of weights less than <code>t</code> is 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1,2],[0,2,3]], k = 1, t = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061406.png" style="width: 180px; height: 164px;" /></p>
<ul>
<li>There are two paths with <code>k = 1</code> edge:
<ul>
<li><code>0 -&gt; 1</code> with weight <code>2 &lt; t</code>.</li>
<li><code>0 -&gt; 2</code> with weight <code>3 = t</code>, which is not strictly less than <code>t</code>.</li>
</ul>
</li>
<li>Thus, the maximum possible sum of weights less than <code>t</code> 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">n = 3, edges = [[0,1,6],[1,2,8]], k = 1, t = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061442.png" style="width: 180px; height: 154px;" /></p>
<ul>
<li>There are two paths with k = 1 edge:
<ul>
<li><code>0 -&gt; 1</code> with weight <code>6 = t</code>, which is not strictly less than <code>t</code>.</li>
<li><code>1 -&gt; 2</code> with weight <code>8 &gt; t</code>, which is not strictly less than <code>t</code>.</li>
</ul>
</li>
<li>Since there is no path with sum of weights strictly less than <code>t</code>, the answer is -1.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 300</code></li>
<li><code>0 &lt;= edges.length &lt;= 300</code></li>
<li><code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 10</code></li>
<li><code>0 &lt;= k &lt;= 300</code></li>
<li><code>1 &lt;= t &lt;= 600</code></li>
<li>The input graph is <strong>guaranteed</strong> to be a <strong>DAG</strong>.</li>
<li>There are no duplicate edges.</li>
</ul>

View File

@@ -0,0 +1,64 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p>
<p>Your task is to delete some (possibly none) of the characters in the string so that the number of <strong>distinct</strong> characters in the resulting string is <strong>at most</strong> <code>k</code>.</p>
<p>Return the <strong>minimum</strong> number of deletions required to achieve this.</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;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>s</code> has three distinct characters: <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code> and <code>&#39;c&#39;</code>, each with a frequency of 1.</li>
<li>Since we can have at most <code>k = 2</code> distinct characters, remove all occurrences of any one character from the string.</li>
<li>For example, removing all occurrences of <code>&#39;c&#39;</code> results in at most <code>k</code> distinct characters. 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">s = &quot;aabb&quot;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>s</code> has two distinct characters (<code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>) with frequencies of 2 and 2, respectively.</li>
<li>Since we can have at most <code>k = 2</code> distinct characters, no deletions are required. Thus, the answer is 0.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;yyyzz&quot;, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>s</code> has two distinct characters (<code>&#39;y&#39;</code> and <code>&#39;z&#39;</code>) with frequencies of 3 and 2, respectively.</li>
<li>Since we can have at most <code>k = 1</code> distinct character, remove all occurrences of any one character from the string.</li>
<li>Removing all <code>&#39;z&#39;</code> results in at most <code>k</code> distinct characters. Thus, the answer is 2.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 16</code></li>
<li><code>1 &lt;= k &lt;= 16</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
<p> </p>

View File

@@ -0,0 +1,61 @@
<p>You are given a positive integer <code>n</code>.</p>
<p>Return the <strong>maximum</strong> product of any two digits in <code>n</code>.</p>
<p><strong>Note:</strong> You may use the <strong>same</strong> digit twice if it appears more than once in <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 = 31</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The digits of <code>n</code> are <code>[3, 1]</code>.</li>
<li>The possible products of any two digits are: <code>3 * 1 = 3</code>.</li>
<li>The maximum product is 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 22</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The digits of <code>n</code> are <code>[2, 2]</code>.</li>
<li>The possible products of any two digits are: <code>2 * 2 = 4</code>.</li>
<li>The maximum product is 4.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 124</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The digits of <code>n</code> are <code>[1, 2, 4]</code>.</li>
<li>The possible products of any two digits are: <code>1 * 2 = 2</code>, <code>1 * 4 = 4</code>, <code>2 * 4 = 8</code>.</li>
<li>The maximum product is 8.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>10 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,115 @@
<p data-end="378" data-start="31">You are given an array of positive integers <code data-end="85" data-start="79">nums</code> and a positive integer <code data-end="112" data-start="109">k</code>.</p>
<p data-end="378" data-start="31">A <span data-keyword="permutation-array">permutation</span> of <code data-end="137" data-start="131">nums</code> is said to form a <strong data-end="183" data-start="156">divisible concatenation</strong> if, when you <em>concatenate</em> <em>the decimal representations</em> of the numbers in the order specified by the permutation, the resulting number is <strong>divisible by</strong> <code data-end="359" data-start="356">k</code>.</p>
<p data-end="561" data-start="380">Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> permutation (when considered as a list of integers) that forms a <strong>divisible concatenation</strong>. If no such permutation exists, return an empty list.</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 = [3,12,45], k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,12,45]</span></p>
<p><strong>Explanation:</strong></p>
<table data-end="896" data-start="441" node="[object Object]" style="border: 1px solid black;">
<thead data-end="497" data-start="441">
<tr data-end="497" data-start="441">
<th data-end="458" data-start="441" style="border: 1px solid black;">Permutation</th>
<th data-end="479" data-start="458" style="border: 1px solid black;">Concatenated Value</th>
<th data-end="497" data-start="479" style="border: 1px solid black;">Divisible by 5</th>
</tr>
</thead>
<tbody data-end="896" data-start="555">
<tr data-end="611" data-start="555">
<td style="border: 1px solid black;">[3, 12, 45]</td>
<td style="border: 1px solid black;">31245</td>
<td style="border: 1px solid black;">Yes</td>
</tr>
<tr data-end="668" data-start="612">
<td style="border: 1px solid black;">[3, 45, 12]</td>
<td style="border: 1px solid black;">34512</td>
<td style="border: 1px solid black;">No</td>
</tr>
<tr data-end="725" data-start="669">
<td style="border: 1px solid black;">[12, 3, 45]</td>
<td style="border: 1px solid black;">12345</td>
<td style="border: 1px solid black;">Yes</td>
</tr>
<tr data-end="782" data-start="726">
<td style="border: 1px solid black;">[12, 45, 3]</td>
<td style="border: 1px solid black;">12453</td>
<td style="border: 1px solid black;">No</td>
</tr>
<tr data-end="839" data-start="783">
<td style="border: 1px solid black;">[45, 3, 12]</td>
<td style="border: 1px solid black;">45312</td>
<td style="border: 1px solid black;">No</td>
</tr>
<tr data-end="896" data-start="840">
<td style="border: 1px solid black;">[45, 12, 3]</td>
<td style="border: 1px solid black;">45123</td>
<td style="border: 1px solid black;">No</td>
</tr>
</tbody>
</table>
<p data-end="1618" data-start="1525">The lexicographically smallest permutation that forms a divisible concatenation is <code>[3,12,45]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [10,5], k = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">[5,10]</span></p>
<p><strong>Explanation:</strong></p>
<table data-end="1421" data-start="1200" node="[object Object]" style="border: 1px solid black;">
<thead data-end="1255" data-start="1200">
<tr data-end="1255" data-start="1200">
<th data-end="1216" data-start="1200" style="border: 1px solid black;">Permutation</th>
<th data-end="1237" data-start="1216" style="border: 1px solid black;">Concatenated Value</th>
<th data-end="1255" data-start="1237" style="border: 1px solid black;">Divisible by 10</th>
</tr>
</thead>
<tbody data-end="1421" data-start="1312">
<tr data-end="1366" data-start="1312">
<td style="border: 1px solid black;">[5, 10]</td>
<td style="border: 1px solid black;">510</td>
<td style="border: 1px solid black;">Yes</td>
</tr>
<tr data-end="1421" data-start="1367">
<td style="border: 1px solid black;">[10, 5]</td>
<td style="border: 1px solid black;">105</td>
<td style="border: 1px solid black;">No</td>
</tr>
</tbody>
</table>
<p data-end="2011" data-start="1921">The lexicographically smallest permutation that forms a divisible concatenation is <code>[5,10]</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,3], k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
<p><strong>Explanation:</strong></p>
<p>Since no permutation of <code data-end="177" data-start="171">nums</code> forms a valid divisible concatenation, return an empty list.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 13</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,50 @@
<p>There are <code>n</code> types of units indexed from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>conversions</code> of length <code>n - 1</code>, where <code>conversions[i] = [sourceUnit<sub>i</sub>, targetUnit<sub>i</sub>, conversionFactor<sub>i</sub>]</code>. This indicates that a single unit of type <code>sourceUnit<sub>i</sub></code> is equivalent to <code>conversionFactor<sub>i</sub></code> units of type <code>targetUnit<sub>i</sub></code>.</p>
<p>Return an array <code>baseUnitConversion</code> of length <code>n</code>, where <code>baseUnitConversion[i]</code> is the number of units of type <code>i</code> equivalent to a single unit of type 0. Since the answer may be large, return each <code>baseUnitConversion[i]</code> <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">conversions = [[0,1,2],[1,2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Convert a single unit of type 0 into 2 units of type 1 using <code>conversions[0]</code>.</li>
<li>Convert a single unit of type 0 into 6 units of type 2 using <code>conversions[0]</code>, then <code>conversions[1]</code>.</li>
</ul>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/12/example1.png" style="width: 545px; height: 118px;" /></div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">conversions = [[0,1,2],[0,2,3],[1,3,4],[1,4,5],[2,5,2],[4,6,3],[5,7,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3,8,10,6,30,24]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Convert a single unit of type 0 into 2 units of type 1 using <code>conversions[0]</code>.</li>
<li>Convert a single unit of type 0 into 3 units of type 2 using <code>conversions[1]</code>.</li>
<li>Convert a single unit of type 0 into 8 units of type 3 using <code>conversions[0]</code>, then <code>conversions[2]</code>.</li>
<li>Convert a single unit of type 0 into 10 units of type 4 using <code>conversions[0]</code>, then <code>conversions[3]</code>.</li>
<li>Convert a single unit of type 0 into 6 units of type 5 using <code>conversions[1]</code>, then <code>conversions[4]</code>.</li>
<li>Convert a single unit of type 0 into 30 units of type 6 using <code>conversions[0]</code>, <code>conversions[3]</code>, then <code>conversions[5]</code>.</li>
<li>Convert a single unit of type 0 into 24 units of type 7 using <code>conversions[1]</code>, <code>conversions[4]</code>, then <code>conversions[6]</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>conversions.length == n - 1</code></li>
<li><code>0 &lt;= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> &lt; n</code></li>
<li><code>1 &lt;= conversionFactor<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li>It is guaranteed that unit 0 can be converted into any other unit through a <strong>unique</strong> combination of conversions without using any conversions in the opposite direction.</li>
</ul>

View File

@@ -0,0 +1,134 @@
<p data-end="452" data-start="24">You are given a straight road of length <code>l</code> km, an integer <code>n</code>, an integer <code>k</code><strong data-end="83" data-start="78">, </strong>and <strong>two</strong> integer arrays, <code>position</code> and <code>time</code>, each of length <code>n</code>.</p>
<p data-end="452" data-start="24">The array <code>position</code> lists the positions (in km) of signs in <strong>strictly</strong> increasing order (with <code>position[0] = 0</code> and <code>position[n - 1] = l</code>).</p>
<p data-end="452" data-start="24">Each <code>time[i]</code> represents the time (in minutes) required to travel 1 km between <code>position[i]</code> and <code>position[i + 1]</code>.</p>
<p data-end="593" data-start="454">You <strong>must</strong> perform <strong>exactly</strong> <code>k</code> merge operations. In one merge, you can choose any <strong>two</strong> adjacent signs at indices <code>i</code> and <code>i + 1</code> (with <code>i &gt; 0</code> and <code>i + 1 &lt; n</code>) and:</p>
<ul data-end="701" data-start="595">
<li data-end="624" data-start="595">Update the sign at index <code>i + 1</code> so that its time becomes <code>time[i] + time[i + 1]</code>.</li>
<li data-end="624" data-start="595">Remove the sign at index <code>i</code>.</li>
</ul>
<p data-end="846" data-start="703">Return the <strong>minimum</strong> <strong>total</strong> <strong>travel time</strong> (in minutes) to travel from 0 to <code>l</code> after <strong>exactly</strong> <code>k</code> merges.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">l = 10, n = 4, k = 1, position = [0,3,8,10], time = [5,8,3,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">62</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="121" data-start="11">
<p data-end="121" data-start="13">Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to <code>8 + 3 = 11</code>.</p>
</li>
<li data-end="144" data-start="15">After the merge:
<ul>
<li data-end="214" data-start="145"><code>position</code> array: <code>[0, 8, 10]</code></li>
<li data-end="214" data-start="145"><code>time</code> array: <code>[5, 11, 6]</code></li>
<li data-end="214" data-start="145" style="opacity: 0"> </li>
</ul>
</li>
<li data-end="214" data-start="145">
<table data-end="386" data-start="231" style="border: 1px solid black;">
<thead data-end="269" data-start="231">
<tr data-end="269" data-start="231">
<th data-end="241" data-start="231" style="border: 1px solid black;">Segment</th>
<th data-end="252" data-start="241" style="border: 1px solid black;">Distance (km)</th>
<th data-end="260" data-start="252" style="border: 1px solid black;">Time per km (min)</th>
<th data-end="269" data-start="260" style="border: 1px solid black;">Segment Travel Time (min)</th>
</tr>
</thead>
<tbody data-end="386" data-start="309">
<tr data-end="347" data-start="309">
<td style="border: 1px solid black;">0 &rarr; 8</td>
<td style="border: 1px solid black;">8</td>
<td style="border: 1px solid black;">5</td>
<td style="border: 1px solid black;">8 &times; 5 = 40</td>
</tr>
<tr data-end="386" data-start="348">
<td style="border: 1px solid black;">8 &rarr; 10</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">11</td>
<td style="border: 1px solid black;">2 &times; 11 = 22</td>
</tr>
</tbody>
</table>
</li>
<li data-end="214" data-start="145">Total Travel Time: <code>40 + 22 = 62</code>, which is the minimum possible time after exactly 1 merge.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">l = 5, n = 5, k = 1, position = [0,1,2,3,5], time = [8,3,9,3,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">34</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="567" data-start="438">Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to <code>3 + 9 = 12</code>.</li>
<li data-end="755" data-start="568">After the merge:
<ul>
<li data-end="755" data-start="568"><code>position</code> array: <code>[0, 2, 3, 5]</code></li>
<li data-end="755" data-start="568"><code>time</code> array: <code>[8, 12, 3, 3]</code></li>
<li data-end="755" data-start="568" style="opacity: 0"> </li>
</ul>
</li>
<li data-end="755" data-start="568">
<table data-end="966" data-start="772" style="border: 1px solid black;">
<thead data-end="810" data-start="772">
<tr data-end="810" data-start="772">
<th data-end="782" data-start="772" style="border: 1px solid black;">Segment</th>
<th data-end="793" data-start="782" style="border: 1px solid black;">Distance (km)</th>
<th data-end="801" data-start="793" style="border: 1px solid black;">Time per km (min)</th>
<th data-end="810" data-start="801" style="border: 1px solid black;">Segment Travel Time (min)</th>
</tr>
</thead>
<tbody data-end="966" data-start="850">
<tr data-end="888" data-start="850">
<td style="border: 1px solid black;">0 &rarr; 2</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">8</td>
<td style="border: 1px solid black;">2 &times; 8 = 16</td>
</tr>
<tr data-end="927" data-start="889">
<td style="border: 1px solid black;">2 &rarr; 3</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">12</td>
<td style="border: 1px solid black;">1 &times; 12 = 12</td>
</tr>
<tr data-end="966" data-start="928">
<td style="border: 1px solid black;">3 &rarr; 5</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">2 &times; 3 = 6</td>
</tr>
</tbody>
</table>
</li>
<li data-end="755" data-start="568">Total Travel Time: <code>16 + 12 + 6 = 34</code><b>, </b>which is the minimum possible time after exactly 1 merge.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="35" data-start="15"><code>1 &lt;= l &lt;= 10<sup>5</sup></code></li>
<li data-end="52" data-start="36"><code>2 &lt;= n &lt;= min(l + 1, 50)</code></li>
<li data-end="81" data-start="53"><code>0 &lt;= k &lt;= min(n - 2, 10)</code></li>
<li data-end="81" data-start="53"><code>position.length == n</code></li>
<li data-end="81" data-start="53"><code>position[0] = 0</code> and <code>position[n - 1] = l</code></li>
<li data-end="200" data-start="80"><code>position</code> is sorted in strictly increasing order.</li>
<li data-end="81" data-start="53"><code>time.length == n</code></li>
<li data-end="81" data-start="53"><code>1 &lt;= time[i] &lt;= 100</code></li>
<li data-end="81" data-start="53"><code>1 &lt;= sum(time) &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,48 @@
<p>You are given an <strong>und</strong><strong>irected</strong> graph of <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. Each node is connected to <strong>at most</strong> 2 other nodes.</p>
<p>The graph consists of <code>m</code> edges, represented by a 2D array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
<p data-end="502" data-start="345">You have to assign a <strong>unique</strong> value from <code data-end="391" data-start="388">1</code> to <code data-end="398" data-start="395">n</code> to each node. The value of an edge will be the <strong>product</strong> of the values assigned to the two nodes it connects.</p>
<p data-end="502" data-start="345">Your score is the sum of the values of all edges in the graph.</p>
<p>Return the <strong>maximum</strong> score you can achieve.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/23/graphproblemex1drawio.png" style="width: 400px; height: 157px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">130</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above illustrates an optimal assignment of values to nodes. The sum of the values of the edges is: <code>(7 * 6) + (7 * 5) + (6 * 5) + (1 * 3) + (3 * 4) + (4 * 2) = 130</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/23/graphproblemex2drawio.png" style="width: 220px; height: 255px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 6, edges = [[0,3],[4,5],[2,0],[1,3],[2,4],[1,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">82</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above illustrates an optimal assignment of values to nodes. The sum of the values of the edges is: <code>(1 * 2) + (2 * 4) + (4 * 6) + (6 * 5) + (5 * 3) + (3 * 1) = 82</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>m == edges.length</code></li>
<li><code>1 &lt;= m &lt;= n</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li>There are no repeated edges.</li>
<li>Each node is connected to at most 2 other nodes.</li>
</ul>

View File

@@ -0,0 +1,79 @@
<p>You are given a non-negative integer <code><font face="monospace">n</font></code> representing a <code>2<sup>n</sup> x 2<sup>n</sup></code> grid. You must fill the grid with integers from 0 to <code>2<sup>2n</sup> - 1</code> to make it <strong>special</strong>. A grid is <strong>special</strong> if it satisfies <strong>all</strong> the following conditions:</p>
<ul>
<li>All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant.</li>
<li>All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant.</li>
<li>All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant.</li>
<li>Each of its quadrants is also a special grid.</li>
</ul>
<p>Return the <strong>special</strong> <code>2<sup>n</sup> x 2<sup>n</sup></code> grid.</p>
<p><strong>Note</strong>: Any 1x1 grid is special.</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 = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">[[0]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The only number that can be placed is 0, and there is only one possible position in the grid.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,0],[2,1]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The numbers in each quadrant are:</p>
<ul>
<li>Top-right: 0</li>
<li>Bottom-right: 1</li>
<li>Bottom-left: 2</li>
<li>Top-left: 3</li>
</ul>
<p>Since <code>0 &lt; 1 &lt; 2 &lt; 3</code>, this satisfies the given constraints.</p>
</div>
<p><strong class="example">Example 3:</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">[[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/05/4123example3p1drawio.png" style="width: 161px; height: 161px;" /></p>
<p>The numbers in each quadrant are:</p>
<ul>
<li>Top-right: 3, 0, 2, 1</li>
<li>Bottom-right: 7, 4, 6, 5</li>
<li>Bottom-left: 11, 8, 10, 9</li>
<li>Top-left: 15, 12, 14, 13</li>
<li><code>max(3, 0, 2, 1) &lt; min(7, 4, 6, 5)</code></li>
<li><code>max(7, 4, 6, 5) &lt; min(11, 8, 10, 9)</code></li>
<li><code>max(11, 8, 10, 9) &lt; min(15, 12, 14, 13)</code></li>
</ul>
<p>This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= n &lt;= 10</code></li>
</ul>

View File

@@ -0,0 +1,92 @@
<p data-end="551" data-start="302">You are given an undirected tree rooted at node <code>0</code>, with <code>n</code> nodes numbered from 0 to <code>n - 1</code>. The tree is represented by a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p>
<p data-end="670" data-start="553">You are also given an integer array <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the value at node <code>i</code>, and an integer <code>k</code>.</p>
<p data-end="763" data-start="672">You may perform <strong>inversion operations</strong> on a subset of nodes subject to the following rules:</p>
<ul data-end="1247" data-start="765">
<li data-end="890" data-start="765">
<p data-end="799" data-start="767"><strong data-end="799" data-start="767">Subtree Inversion Operation:</strong></p>
<ul data-end="890" data-start="802">
<li data-end="887" data-start="802">
<p data-end="887" data-start="804">When you invert a node, every value in the <span data-keyword="subtree-of-node">subtree</span> rooted at that node is multiplied by -1.</p>
</li>
</ul>
</li>
<li data-end="1247" data-start="891">
<p data-end="931" data-start="893"><strong data-end="931" data-start="893">Distance Constraint on Inversions:</strong></p>
<ul data-end="1247" data-start="934">
<li data-end="1020" data-start="934">
<p data-end="1020" data-start="936">You may only invert a node if it is &quot;sufficiently far&quot; from any other inverted node.</p>
</li>
<li data-end="1247" data-start="1023">
<p data-end="1247" data-start="1025">Specifically, if you invert two nodes <code>a</code> and <code>b</code> such that one is an ancestor of the other (i.e., if <code>LCA(a, b) = a</code> or <code>LCA(a, b) = b</code>), then the distance (the number of edges on the unique path between them) must be at least <code>k</code>.</p>
</li>
</ul>
</li>
</ul>
<p data-end="1358" data-start="1249">Return the <strong>maximum</strong> possible <strong>sum</strong> of the tree&#39;s node values after applying <strong>inversion operations</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">edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], nums = [4,-8,-6,3,7,-2,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/29/tree1-3.jpg" style="width: 311px; height: 202px;" /></p>
<ul>
<li>Apply inversion operations at nodes 0, 3, 4 and 6.</li>
<li>The final <code data-end="1726" data-start="1720">nums</code> array is <code data-end="1760" data-start="1736">[-4, 8, 6, 3, 7, 2, 5]</code>, and the total sum is 27.</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],[1,2],[2,3],[3,4]], nums = [-1,3,-2,4,-5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/29/tree2-1.jpg" style="width: 371px; height: 71px;" /></p>
<ul>
<li>Apply the inversion operation at node 4.</li>
<li data-end="2632" data-start="2483">The final <code data-end="2569" data-start="2563">nums</code> array becomes <code data-end="2603" data-start="2584">[-1, 3, -2, 4, 5]</code>, and the total sum is 9.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[0,2]], nums = [0,-1,-2], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Apply inversion operations at nodes 1 and 2.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>nums.length == n</code></li>
<li><code>-5 * 10<sup>4</sup> &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= k &lt;= 50</code></li>
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
</ul>

View File

@@ -0,0 +1,64 @@
<p>You are given an array <code>nums</code> of size <code>n</code>, consisting of <strong>non-negative</strong> integers. Your task is to apply some (possibly zero) operations on the array so that <strong>all</strong> elements become 0.</p>
<p>In one operation, you can select a <span data-keyword="subarray">subarray</span> <code>[i, j]</code> (where <code>0 &lt;= i &lt;= j &lt; n</code>) and set all occurrences of the <strong>minimum</strong> <strong>non-negative</strong> integer in that subarray to 0.</p>
<p>Return the <strong>minimum</strong> number of operations required to make all elements in the array 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,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Select the subarray <code>[1,1]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0]</code>.</li>
<li>Thus, the minimum number of operations required 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 = [3,1,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Select subarray <code>[1,3]</code> (which is <code>[1,2,1]</code>), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in <code>[3,0,2,0]</code>.</li>
<li>Select subarray <code>[2,2]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[3,0,0,0]</code>.</li>
<li>Select subarray <code>[0,0]</code> (which is <code>[3]</code>), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in <code>[0,0,0,0]</code>.</li>
<li>Thus, the minimum number of operations required is 3.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,2,1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Select subarray <code>[0,5]</code> (which is <code>[1,2,1,2,1,2]</code>), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in <code>[0,2,0,2,0,2]</code>.</li>
<li>Select subarray <code>[1,1]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0,0,2,0,2]</code>.</li>
<li>Select subarray <code>[3,3]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0,0,0,0,2]</code>.</li>
<li>Select subarray <code>[5,5]</code> (which is <code>[2]</code>), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in <code>[0,0,0,0,0,0]</code>.</li>
<li>Thus, the minimum number of operations required is 4.</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>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,46 @@
<p>You are given a 2D string array <code>responses</code> where each <code>responses[i]</code> is an array of strings representing survey responses from the <code>i<sup>th</sup></code> day.</p>
<p>Return the <strong>most common</strong> response across all days after removing <strong>duplicate</strong> responses within each <code>responses[i]</code>. If there is a tie, return the <em><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></em> response.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">responses = [[&quot;good&quot;,&quot;ok&quot;,&quot;good&quot;,&quot;ok&quot;],[&quot;ok&quot;,&quot;bad&quot;,&quot;good&quot;,&quot;ok&quot;,&quot;ok&quot;],[&quot;good&quot;],[&quot;bad&quot;]]</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;good&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After removing duplicates within each list, <code>responses = [[&quot;good&quot;, &quot;ok&quot;], [&quot;ok&quot;, &quot;bad&quot;, &quot;good&quot;], [&quot;good&quot;], [&quot;bad&quot;]]</code>.</li>
<li><code>&quot;good&quot;</code> appears 3 times, <code>&quot;ok&quot;</code> appears 2 times, and <code>&quot;bad&quot;</code> appears 2 times.</li>
<li>Return <code>&quot;good&quot;</code> because it has the highest frequency.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">responses = [[&quot;good&quot;,&quot;ok&quot;,&quot;good&quot;],[&quot;ok&quot;,&quot;bad&quot;],[&quot;bad&quot;,&quot;notsure&quot;],[&quot;great&quot;,&quot;good&quot;]]</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;bad&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After removing duplicates within each list we have <code>responses = [[&quot;good&quot;, &quot;ok&quot;], [&quot;ok&quot;, &quot;bad&quot;], [&quot;bad&quot;, &quot;notsure&quot;], [&quot;great&quot;, &quot;good&quot;]]</code>.</li>
<li><code>&quot;bad&quot;</code>, <code>&quot;good&quot;</code>, and <code>&quot;ok&quot;</code> each occur 2 times.</li>
<li>The output is <code>&quot;bad&quot;</code> because it is the lexicographically smallest amongst the words with the highest frequency.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= responses.length &lt;= 1000</code></li>
<li><code>1 &lt;= responses[i].length &lt;= 1000</code></li>
<li><code>1 &lt;= responses[i][j].length &lt;= 10</code></li>
<li><code>responses[i][j]</code> consists of only lowercase English letters</li>
</ul>

View File

@@ -0,0 +1,53 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters (<code>&#39;a&#39;</code> to <code>&#39;z&#39;</code>). </p>
<p>Your task is to:</p>
<ul>
<li>Find the vowel (one of <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, or <code>&#39;u&#39;</code>) with the <strong>maximum</strong> frequency.</li>
<li>Find the consonant (all other letters excluding vowels) with the <strong>maximum</strong> frequency.</li>
</ul>
<p>Return the sum of the two frequencies.</p>
<p><strong>Note</strong>: If multiple vowels or consonants have the same maximum frequency, you may choose any one of them. If there are no vowels or no consonants in the string, consider their frequency as 0.</p>
The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.
<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;successes&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The vowels are: <code>&#39;u&#39;</code> (frequency 1), <code>&#39;e&#39;</code> (frequency 2). The maximum frequency is 2.</li>
<li>The consonants are: <code>&#39;s&#39;</code> (frequency 4), <code>&#39;c&#39;</code> (frequency 2). The maximum frequency is 4.</li>
<li>The output is <code>2 + 4 = 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">s = &quot;aeiaeia&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The vowels are: <code>&#39;a&#39;</code> (frequency 3), <code>&#39;e&#39;</code> ( frequency 2), <code>&#39;i&#39;</code> (frequency 2). The maximum frequency is 3.</li>
<li>There are no consonants in <code>s</code>. Hence, maximum consonant frequency = 0.</li>
<li>The output is <code>3 + 0 = 3</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> consists of lowercase English letters only.</li>
</ul>

View File

@@ -0,0 +1,119 @@
<p>You are given a <strong>Directed Acyclic Graph (DAG)</strong> with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, represented by a 2D array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates a directed edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code>. Each node has an associated <strong>score</strong> given in an array <code>score</code>, where <code>score[i]</code> represents the score of node <code>i</code>.</p>
<p>You must process the nodes in a <strong>valid topological order</strong>. Each node is assigned a <strong>1-based position</strong> in the processing order.</p>
<p>The <strong>profit</strong> is calculated by summing up the product of each node&#39;s score and its position in the ordering.</p>
<p>Return the <strong>maximum </strong>possible profit achievable with an optimal topological order.</p>
<p>A <strong>topological order</strong> of a DAG is a linear ordering of its nodes such that for every directed edge <code>u &rarr; v</code>, node <code>u</code> comes before <code>v</code> in the ordering.</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, edges = [[0,1]], score = [2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/10/screenshot-2025-03-11-at-021131.png" style="width: 200px; height: 89px;" /></p>
<p>Node 1 depends on node 0, so a valid order is <code>[0, 1]</code>.</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Node</th>
<th style="border: 1px solid black;">Processing Order</th>
<th style="border: 1px solid black;">Score</th>
<th style="border: 1px solid black;">Multiplier</th>
<th style="border: 1px solid black;">Profit Calculation</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">1st</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">2 &times; 1 = 2</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">2nd</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">3 &times; 2 = 6</td>
</tr>
</tbody>
</table>
<p>The maximum total profit achievable over all valid topological orders is <code>2 + 6 = 8</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[0,1],[0,2]], score = [1,6,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">25</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/10/screenshot-2025-03-11-at-023558.png" style="width: 200px; height: 124px;" /></p>
<p>Nodes 1 and 2 depend on node 0, so the most optimal valid order is <code>[0, 2, 1]</code>.</p>
<table data-end="1197" data-start="851" node="[object Object]" style="border: 1px solid black;">
<thead data-end="920" data-start="851">
<tr data-end="920" data-start="851">
<th data-end="858" data-start="851" style="border: 1px solid black;">Node</th>
<th data-end="877" data-start="858" style="border: 1px solid black;">Processing Order</th>
<th data-end="885" data-start="877" style="border: 1px solid black;">Score</th>
<th data-end="898" data-start="885" style="border: 1px solid black;">Multiplier</th>
<th data-end="920" data-start="898" style="border: 1px solid black;">Profit Calculation</th>
</tr>
</thead>
<tbody data-end="1197" data-start="991">
<tr data-end="1059" data-start="991">
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">1st</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1 &times; 1 = 1</td>
</tr>
<tr data-end="1128" data-start="1060">
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2nd</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">3 &times; 2 = 6</td>
</tr>
<tr data-end="1197" data-start="1129">
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">3rd</td>
<td style="border: 1px solid black;">6</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">6 &times; 3 = 18</td>
</tr>
</tbody>
</table>
<p>The maximum total profit achievable over all valid topological orders is <code>1 + 6 + 18 = 25</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == score.length &lt;= 22</code></li>
<li><code>1 &lt;= score[i] &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a directed edge from <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code>.</li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>The input graph is <strong>guaranteed</strong> to be a <strong>DAG</strong>.</li>
<li>There are no duplicate edges.</li>
</ul>

View File

@@ -0,0 +1,45 @@
<p>You are given an <code>m x n</code> matrix <code>grid</code> of positive integers. Your task is to determine if it is possible to make <strong>either one horizontal or one vertical cut</strong> on the grid such that:</p>
<ul>
<li>Each of the two resulting sections formed by the cut is <strong>non-empty</strong>.</li>
<li>The sum of the elements in both sections is <strong>equal</strong>.</li>
</ul>
<p>Return <code>true</code> if such a partition exists; 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">grid = [[1,4],[2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/30/lc.png" style="width: 200px;" /><img alt="" src="https://assets.leetcode.com/uploads/2025/03/30/lc.jpeg" style="width: 200px; height: 200px;" /></p>
<p>A horizontal cut between row 0 and row 1 results in two non-empty sections, each with a sum of 5. Thus, the answer is <code>true</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,3],[2,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>No horizontal or vertical cut results in two non-empty sections with equal sums. Thus, the answer is <code>false</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= n == grid[i].length &lt;= 10<sup>5</sup></code></li>
<li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,84 @@
<p>You are given an <code>m x n</code> matrix <code>grid</code> of positive integers. Your task is to determine if it is possible to make <strong>either one horizontal or one vertical cut</strong> on the grid such that:</p>
<ul>
<li>Each of the two resulting sections formed by the cut is <strong>non-empty</strong>.</li>
<li>The sum of elements in both sections is <b>equal</b>, or can be made equal by discounting <strong>at most</strong> one single cell in total (from either section).</li>
<li>If a cell is discounted, the rest of the section must <strong>remain connected</strong>.</li>
</ul>
<p>Return <code>true</code> if such a partition exists; otherwise, return <code>false</code>.</p>
<p><strong>Note:</strong> A section is <strong>connected</strong> if every cell in it can be reached from any other cell by moving up, down, left, or right through other cells in the section.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,4],[2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/30/lc.jpeg" style="height: 180px; width: 180px;" /></p>
<ul>
<li>A horizontal cut after the first row gives sums <code>1 + 4 = 5</code> and <code>2 + 3 = 5</code>, which are equal. Thus, the answer is <code>true</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,2],[3,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/01/chatgpt-image-apr-1-2025-at-05_28_12-pm.png" style="height: 180px; width: 180px;" /></p>
<ul>
<li>A vertical cut after the first column gives sums <code>1 + 3 = 4</code> and <code>2 + 4 = 6</code>.</li>
<li>By discounting 2 from the right section (<code>6 - 2 = 4</code>), both sections have equal sums and remain connected. Thus, the answer is <code>true</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,2,4],[2,3,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2025/04/01/chatgpt-image-apr-2-2025-at-02_50_29-am.png" style="height: 180px; width: 180px;" /></strong></p>
<ul>
<li>A horizontal cut after the first row gives <code>1 + 2 + 4 = 7</code> and <code>2 + 3 + 5 = 10</code>.</li>
<li>By discounting 3 from the bottom section (<code>10 - 3 = 7</code>), both sections have equal sums, but they do not remain connected as it splits the bottom section into two parts (<code>[2]</code> and <code>[5]</code>). Thus, the answer is <code>false</code>.</li>
</ul>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[4,1,8],[3,2,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>No valid cut exists, so the answer is <code>false</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= n == grid[i].length &lt;= 10<sup>5</sup></code></li>
<li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,58 @@
<p>You are given an <code>m x n</code> matrix <code>grid</code> consisting of characters and a string <code>pattern</code>.</p>
<p>A <strong data-end="264" data-start="240">horizontal substring</strong> is a contiguous sequence of characters read from left to right. If the end of a row is reached before the substring is complete, it wraps to the first column of the next row and continues as needed. You do <strong>not</strong> wrap from the bottom row back to the top.</p>
<p>A <strong data-end="484" data-start="462">vertical substring</strong> is a contiguous sequence of characters read from top to bottom. If the bottom of a column is reached before the substring is complete, it wraps to the first row of the next column and continues as needed. You do <strong>not</strong> wrap from the last column back to the first.</p>
<p>Count the number of cells in the matrix that satisfy the following condition:</p>
<ul>
<li>The cell must be part of <strong>at least</strong> one horizontal substring and <strong>at least</strong> one vertical substring, where <strong>both</strong> substrings are equal to the given <code>pattern</code>.</li>
</ul>
<p>Return the count of these cells.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/03/gridtwosubstringsdrawio.png" style="width: 150px; height: 187px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[&quot;a&quot;,&quot;a&quot;,&quot;c&quot;,&quot;c&quot;],[&quot;b&quot;,&quot;b&quot;,&quot;b&quot;,&quot;c&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;c&quot;,&quot;a&quot;,&quot;a&quot;,&quot;c&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;]], pattern = &quot;abaca&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The pattern <code>&quot;abaca&quot;</code> appears once as a horizontal substring (colored blue) and once as a vertical substring (colored red), intersecting at one cell (colored purple).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/03/gridexample2fixeddrawio.png" style="width: 150px; height: 150px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[&quot;c&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;b&quot;,&quot;b&quot;,&quot;a&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;]], pattern = &quot;aba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The cells colored above are all part of at least one horizontal and one vertical substring matching the pattern <code>&quot;aba&quot;</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[&quot;a&quot;]], pattern = &quot;a&quot;</span></p>
<p><strong>Output:</strong> 1</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>1 &lt;= pattern.length &lt;= m * n</code></li>
<li><code>grid</code> and <code>pattern</code> consist of only lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,83 @@
<p>You are given a positive integer <code>n</code>, representing an <code>n x n</code> city. You are also given a 2D grid <code>buildings</code>, where <code>buildings[i] = [x, y]</code> denotes a <strong>unique</strong> building located at coordinates <code>[x, y]</code>.</p>
<p>A building is <strong>covered</strong> if there is at least one building in all <strong>four</strong> directions: left, right, above, and below.</p>
<p>Return the number of <strong>covered</strong> buildings.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/04/telegram-cloud-photo-size-5-6212982906394101085-m.jpg" style="width: 200px; height: 204px;" /></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Only building <code>[2,2]</code> is covered as it has at least one building:
<ul>
<li>above (<code>[1,2]</code>)</li>
<li>below (<code>[3,2]</code>)</li>
<li>left (<code>[2,1]</code>)</li>
<li>right (<code>[2,3]</code>)</li>
</ul>
</li>
<li>Thus, the count of covered buildings is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/04/telegram-cloud-photo-size-5-6212982906394101086-m.jpg" style="width: 200px; height: 204px;" /></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>No building has at least one building in all four directions.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/03/16/telegram-cloud-photo-size-5-6248862251436067566-x.jpg" style="width: 202px; height: 205px;" /></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, buildings = [[1,3],[3,2],[3,3],[3,5],[5,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Only building <code>[3,3]</code> is covered as it has at least one building:
<ul>
<li>above (<code>[1,3]</code>)</li>
<li>below (<code>[5,3]</code>)</li>
<li>left (<code>[3,2]</code>)</li>
<li>right (<code>[3,5]</code>)</li>
</ul>
</li>
<li>Thus, the count of covered buildings is 1.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= buildings.length &lt;= 10<sup>5</sup> </code></li>
<li><code>buildings[i] = [x, y]</code></li>
<li><code>1 &lt;= x, y &lt;= n</code></li>
<li>All coordinates of <code>buildings</code> are <strong>unique</strong>.</li>
</ul>

View File

@@ -0,0 +1,61 @@
<p>You are given an integer <code>n</code> representing the number of nodes in a graph, labeled from 0 to <code>n - 1</code>.</p>
<p>You are also given an integer array <code>nums</code> of length <code>n</code> sorted in <strong>non-decreasing</strong> order, and an integer <code>maxDiff</code>.</p>
<p>An <strong>undirected </strong>edge exists between nodes <code>i</code> and <code>j</code> if the <strong>absolute</strong> difference between <code>nums[i]</code> and <code>nums[j]</code> is <strong>at most</strong> <code>maxDiff</code> (i.e., <code>|nums[i] - nums[j]| &lt;= maxDiff</code>).</p>
<p>You are also given a 2D integer array <code>queries</code>. For each <code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>, determine whether there exists a path between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p>
<p>Return a boolean array <code>answer</code>, where <code>answer[i]</code> is <code>true</code> if there exists a path between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the <code>i<sup>th</sup></code> query and <code>false</code> otherwise.</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, nums = [1,3], maxDiff = 1, queries = [[0,0],[0,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[true,false]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Query <code>[0,0]</code>: Node 0 has a trivial path to itself.</li>
<li>Query <code>[0,1]</code>: There is no edge between Node 0 and Node 1 because <code>|nums[0] - nums[1]| = |1 - 3| = 2</code>, which is greater than <code>maxDiff</code>.</li>
<li>Thus, the final answer after processing all the queries is <code>[true, false]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, nums = [2,5,6,8], maxDiff = 2, queries = [[0,1],[0,2],[1,3],[2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[false,false,true,true]</span></p>
<p><strong>Explanation:</strong></p>
<p>The resulting graph is:</p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/25/screenshot-2025-03-26-at-122249.png" style="width: 300px; height: 170px;" /></p>
<ul>
<li>Query <code>[0,1]</code>: There is no edge between Node 0 and Node 1 because <code>|nums[0] - nums[1]| = |2 - 5| = 3</code>, which is greater than <code>maxDiff</code>.</li>
<li>Query <code>[0,2]</code>: There is no edge between Node 0 and Node 2 because <code>|nums[0] - nums[2]| = |2 - 6| = 4</code>, which is greater than <code>maxDiff</code>.</li>
<li>Query <code>[1,3]</code>: There is a path between Node 1 and Node 3 through Node 2 since <code>|nums[1] - nums[2]| = |5 - 6| = 1</code> and <code>|nums[2] - nums[3]| = |6 - 8| = 2</code>, both of which are within <code>maxDiff</code>.</li>
<li>Query <code>[2,3]</code>: There is an edge between Node 2 and Node 3 because <code>|nums[2] - nums[3]| = |6 - 8| = 2</code>, which is equal to <code>maxDiff</code>.</li>
<li>Thus, the final answer after processing all the queries is <code>[false, false, true, true]</code>.</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>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
<li><code>0 &lt;= maxDiff &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i] == [u<sub>i</sub>, v<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
</ul>

View File

@@ -0,0 +1,126 @@
<p>You are given an integer <code>n</code> representing the number of nodes in a graph, labeled from 0 to <code>n - 1</code>.</p>
<p>You are also given an integer array <code>nums</code> of length <code>n</code> and an integer <code>maxDiff</code>.</p>
<p>An <strong>undirected </strong>edge exists between nodes <code>i</code> and <code>j</code> if the <strong>absolute</strong> difference between <code>nums[i]</code> and <code>nums[j]</code> is <strong>at most</strong> <code>maxDiff</code> (i.e., <code>|nums[i] - nums[j]| &lt;= maxDiff</code>).</p>
<p>You are also given a 2D integer array <code>queries</code>. For each <code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>, find the <strong>minimum</strong> distance between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code><sub>.</sub> If no path exists between the two nodes, return -1 for that query.</p>
<p>Return an array <code>answer</code>, where <code>answer[i]</code> is the result of the <code>i<sup>th</sup></code> query.</p>
<p><strong>Note:</strong> The edges between the nodes are unweighted.</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 = 5, nums = [1,8,3,4,2], maxDiff = 3, queries = [[0,3],[2,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,1]</span></p>
<p><strong>Explanation:</strong></p>
<p>The resulting graph is:</p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/25/4149example1drawio.png" style="width: 281px; height: 161px;" /></p>
<table>
<tbody>
<tr>
<th>Query</th>
<th>Shortest Path</th>
<th>Minimum Distance</th>
</tr>
<tr>
<td>[0, 3]</td>
<td>0 &rarr; 3</td>
<td>1</td>
</tr>
<tr>
<td>[2, 4]</td>
<td>2 &rarr; 4</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>Thus, the output is <code>[1, 1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, nums = [5,3,1,9,10], maxDiff = 2, queries = [[0,1],[0,2],[2,3],[4,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,-1,1]</span></p>
<p><strong>Explanation:</strong></p>
<p>The resulting graph is:</p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/03/25/4149example2drawio.png" style="width: 281px; height: 121px;" /></p>
</div>
<table>
<tbody>
<tr>
<th>Query</th>
<th>Shortest Path</th>
<th>Minimum Distance</th>
</tr>
<tr>
<td>[0, 1]</td>
<td>0 &rarr; 1</td>
<td>1</td>
</tr>
<tr>
<td>[0, 2]</td>
<td>0 &rarr; 1 &rarr; 2</td>
<td>2</td>
</tr>
<tr>
<td>[2, 3]</td>
<td>None</td>
<td>-1</td>
</tr>
<tr>
<td>[4, 3]</td>
<td>3 &rarr; 4</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>Thus, the output is <code>[1, 2, -1, 1]</code>.</p>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, nums = [3,6,1], maxDiff = 1, queries = [[0,0],[0,1],[1,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,-1,-1]</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no edges between any two nodes because:</p>
<ul>
<li>Nodes 0 and 1: <code>|nums[0] - nums[1]| = |3 - 6| = 3 &gt; 1</code></li>
<li>Nodes 0 and 2: <code>|nums[0] - nums[2]| = |3 - 1| = 2 &gt; 1</code></li>
<li>Nodes 1 and 2: <code>|nums[1] - nums[2]| = |6 - 1| = 5 &gt; 1</code></li>
</ul>
<p>Thus, no node can reach any other node, and the output is <code>[0, -1, -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>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= maxDiff &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i] == [u<sub>i</sub>, v<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
</ul>

View File

@@ -0,0 +1,62 @@
<p>You are given two integers, <code>m</code> and <code>k</code>, and an integer array <code>nums</code>.</p>
A sequence of integers <code>seq</code> is called <strong>magical</strong> if:
<ul>
<li><code>seq</code> has a size of <code>m</code>.</li>
<li><code>0 &lt;= seq[i] &lt; nums.length</code></li>
<li>The <strong>binary representation</strong> of <code>2<sup>seq[0]</sup> + 2<sup>seq[1]</sup> + ... + 2<sup>seq[m - 1]</sup></code> has <code>k</code> <strong>set bits</strong>.</li>
</ul>
<p>The <strong>array product</strong> of this sequence is defined as <code>prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]])</code>.</p>
<p>Return the <strong>sum</strong> of the <strong>array products</strong> for all valid <strong>magical</strong> sequences.</p>
<p>Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>A <strong>set bit</strong> refers to a bit in the binary representation of a number that has a value of 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">m = 5, k = 5, nums = [1,10,100,10000,1000000]</span></p>
<p><strong>Output:</strong> <span class="example-io">991600007</span></p>
<p><strong>Explanation:</strong></p>
<p>All permutations of <code>[0, 1, 2, 3, 4]</code> are magical sequences, each with an array product of 10<sup>13</sup>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">m = 2, k = 2, nums = [5,4,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">170</span></p>
<p><strong>Explanation:</strong></p>
<p>The magical sequences are <code>[0, 1]</code>, <code>[0, 2]</code>, <code>[0, 3]</code>, <code>[0, 4]</code>, <code>[1, 0]</code>, <code>[1, 2]</code>, <code>[1, 3]</code>, <code>[1, 4]</code>, <code>[2, 0]</code>, <code>[2, 1]</code>, <code>[2, 3]</code>, <code>[2, 4]</code>, <code>[3, 0]</code>, <code>[3, 1]</code>, <code>[3, 2]</code>, <code>[3, 4]</code>, <code>[4, 0]</code>, <code>[4, 1]</code>, <code>[4, 2]</code>, and <code>[4, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">m = 1, k = 1, nums = [28]</span></p>
<p><strong>Output:</strong> <span class="example-io">28</span></p>
<p><strong>Explanation:</strong></p>
<p>The only magical sequence is <code>[0]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= m &lt;= 30</code></li>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>8</sup></code></li>
</ul>