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-07-17 00:14:36 +08:00
parent 5808ae7d32
commit dee13a03bd
61 changed files with 17286 additions and 9663 deletions

View File

@@ -0,0 +1,91 @@
<p>You are given two integers <code>m</code> and <code>n</code> representing the number of rows and columns of a grid, respectively.</p>
<p>The cost to enter cell <code>(i, j)</code> is defined as <code>(i + 1) * (j + 1)</code>.</p>
<p>You are also given a 2D integer array <code>waitCost</code> where <code>waitCost[i][j]</code> defines the cost to <strong>wait</strong> on that cell.</p>
<p>The path will always begin by entering cell <code>(0, 0)</code> on move 1 and paying the entrance cost.</p>
<p>At each step, you follow an alternating pattern:</p>
<ul>
<li>On <strong>odd-numbered</strong> seconds, you must move <strong>right</strong> or <strong>down</strong> to an <strong>adjacent</strong> cell, paying its entry cost.</li>
<li>On <strong>even-numbered</strong> seconds, you must <strong>wait</strong> in place for <strong>exactly</strong> one second and pay <code>waitCost[i][j]</code> during that second.</li>
</ul>
<p>Return the <strong>minimum</strong> total cost required to reach <code>(m - 1, 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">m = 1, n = 2, waitCost = [[1,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal path is:</p>
<ul>
<li>Start at cell <code>(0, 0)</code> at second 1 with entry cost <code>(0 + 1) * (0 + 1) = 1</code>.</li>
<li><strong>Second 1</strong>: Move right to cell <code>(0, 1)</code> with entry cost <code>(0 + 1) * (1 + 1) = 2</code>.</li>
</ul>
<p>Thus, the total cost is <code>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">m = 2, n = 2, waitCost = [[3,5],[2,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal path is:</p>
<ul>
<li>Start at cell <code>(0, 0)</code> at second 1 with entry cost <code>(0 + 1) * (0 + 1) = 1</code>.</li>
<li><strong>Second 1</strong>: Move down to cell <code>(1, 0)</code> with entry cost <code>(1 + 1) * (0 + 1) = 2</code>.</li>
<li><strong>Second 2</strong>: Wait at cell <code>(1, 0)</code>, paying <code>waitCost[1][0] = 2</code>.</li>
<li><strong>Second 3</strong>: Move right to cell <code>(1, 1)</code> with entry cost <code>(1 + 1) * (1 + 1) = 4</code>.</li>
</ul>
<p>Thus, the total cost is <code>1 + 2 + 2 + 4 = 9</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">m = 2, n = 3, waitCost = [[6,1,4],[3,2,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal path is:</p>
<ul>
<li>Start at cell <code>(0, 0)</code> at second 1 with entry cost <code>(0 + 1) * (0 + 1) = 1</code>.</li>
<li><strong>Second 1</strong>: Move right to cell <code>(0, 1)</code> with entry cost <code>(0 + 1) * (1 + 1) = 2</code>.</li>
<li><strong>Second 2</strong>: Wait at cell <code>(0, 1)</code>, paying <code>waitCost[0][1] = 1</code>.</li>
<li><strong>Second 3</strong>: Move down to cell <code>(1, 1)</code> with entry cost <code>(1 + 1) * (1 + 1) = 4</code>.</li>
<li><strong>Second 4</strong>: Wait at cell <code>(1, 1)</code>, paying <code>waitCost[1][1] = 2</code>.</li>
<li><strong>Second 5</strong>: Move right to cell <code>(1, 2)</code> with entry cost <code>(1 + 1) * (2 + 1) = 6</code>.</li>
</ul>
<p>Thus, the total cost is <code>1 + 2 + 1 + 4 + 2 + 6 = 16</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
<li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>waitCost.length == m</code></li>
<li><code>waitCost[0].length == n</code></li>
<li><code>0 &lt;= waitCost[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,62 @@
<p>You are given three arrays of length <code>n</code> that describe the properties of <code>n</code> coupons: <code>code</code>, <code>businessLine</code>, and <code>isActive</code>. The <code>i<sup>th</sup> </code>coupon has:</p>
<ul>
<li><code>code[i]</code>: a <strong>string</strong> representing the coupon identifier.</li>
<li><code>businessLine[i]</code>: a <strong>string</strong> denoting the business category of the coupon.</li>
<li><code>isActive[i]</code>: a <strong>boolean</strong> indicating whether the coupon is currently active.</li>
</ul>
<p>A coupon is considered <strong>valid</strong> if all of the following conditions hold:</p>
<ol>
<li><code>code[i]</code> is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) and underscores (<code>_</code>).</li>
<li><code>businessLine[i]</code> is one of the following four categories: <code>&quot;electronics&quot;</code>, <code>&quot;grocery&quot;</code>, <code>&quot;pharmacy&quot;</code>, <code>&quot;restaurant&quot;</code>.</li>
<li><code>isActive[i]</code> is <strong>true</strong>.</li>
</ol>
<p>Return an array of the <strong>codes</strong> of all valid coupons, <strong>sorted</strong> first by their <strong>businessLine</strong> in the order: <code>&quot;electronics&quot;</code>, <code>&quot;grocery&quot;</code>, <code>&quot;pharmacy&quot;, &quot;restaurant&quot;</code>, and then by <strong>code</strong> in lexicographical (ascending) order within each category.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">code = [&quot;SAVE20&quot;,&quot;&quot;,&quot;PHARMA5&quot;,&quot;SAVE@20&quot;], businessLine = [&quot;restaurant&quot;,&quot;grocery&quot;,&quot;pharmacy&quot;,&quot;restaurant&quot;], isActive = [true,true,true,true]</span></p>
<p><strong>Output:</strong> <span class="example-io">[&quot;PHARMA5&quot;,&quot;SAVE20&quot;]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>First coupon is valid.</li>
<li>Second coupon has empty code (invalid).</li>
<li>Third coupon is valid.</li>
<li>Fourth coupon has special character <code>@</code> (invalid).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">code = [&quot;GROCERY15&quot;,&quot;ELECTRONICS_50&quot;,&quot;DISCOUNT10&quot;], businessLine = [&quot;grocery&quot;,&quot;electronics&quot;,&quot;invalid&quot;], isActive = [false,true,true]</span></p>
<p><strong>Output:</strong> <span class="example-io">[&quot;ELECTRONICS_50&quot;]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>First coupon is inactive (invalid).</li>
<li>Second coupon is valid.</li>
<li>Third coupon has invalid business line (invalid).</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == code.length == businessLine.length == isActive.length</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>0 &lt;= code[i].length, businessLine[i].length &lt;= 100</code></li>
<li><code>code[i]</code> and <code>businessLine[i]</code> consist of printable ASCII characters.</li>
<li><code>isActive[i]</code> is either <code>true</code> or <code>false</code>.</li>
</ul>

View File

@@ -0,0 +1,165 @@
<p>Given a string <code>s</code>, partition it into <strong>unique segments</strong> according to the following procedure:</p>
<ul>
<li>Start building a segment beginning at index 0.</li>
<li>Continue extending the current segment character by character until the current segment has not been seen before.</li>
<li>Once the segment is unique, add it to your list of segments, mark it as seen, and begin a new segment from the next index.</li>
<li>Repeat until you reach the end of <code>s</code>.</li>
</ul>
<p>Return an array of strings <code>segments</code>, where <code>segments[i]</code> is the <code>i<sup>th</sup></code> segment created.</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;abbccccd&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">[&quot;a&quot;,&quot;b&quot;,&quot;bc&quot;,&quot;c&quot;,&quot;cc&quot;,&quot;d&quot;]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th style="border: 1px solid black;">Index</th>
<th style="border: 1px solid black;">Segment After Adding</th>
<th style="border: 1px solid black;">Seen Segments</th>
<th style="border: 1px solid black;">Current Segment Seen Before?</th>
<th style="border: 1px solid black;">New Segment</th>
<th style="border: 1px solid black;">Updated Seen Segments</th>
</tr>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">&quot;a&quot;</td>
<td style="border: 1px solid black;">[]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">&quot;b&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">&quot;b&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;">&quot;b&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">&quot;bc&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;">&quot;c&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">5</td>
<td style="border: 1px solid black;">&quot;c&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;">&quot;c&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">6</td>
<td style="border: 1px solid black;">&quot;cc&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;, &quot;cc&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">7</td>
<td style="border: 1px solid black;">&quot;d&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;, &quot;cc&quot;]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;, &quot;cc&quot;, &quot;d&quot;]</td>
</tr>
</tbody>
</table>
<p>Hence, the final output is <code>[&quot;a&quot;, &quot;b&quot;, &quot;bc&quot;, &quot;c&quot;, &quot;cc&quot;, &quot;d&quot;]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aaaa&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">[&quot;a&quot;,&quot;aa&quot;]</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th style="border: 1px solid black;">Index</th>
<th style="border: 1px solid black;">Segment After Adding</th>
<th style="border: 1px solid black;">Seen Segments</th>
<th style="border: 1px solid black;">Current Segment Seen Before?</th>
<th style="border: 1px solid black;">New Segment</th>
<th style="border: 1px solid black;">Updated Seen Segments</th>
</tr>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">&quot;a&quot;</td>
<td style="border: 1px solid black;">[]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">&quot;a&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;">&quot;a&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">&quot;aa&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">&quot;&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;aa&quot;]</td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">&quot;a&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;aa&quot;]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;">&quot;a&quot;</td>
<td style="border: 1px solid black;">[&quot;a&quot;, &quot;aa&quot;]</td>
</tr>
</tbody>
</table>
<p>Hence, the final output is <code>[&quot;a&quot;, &quot;aa&quot;]</code>.</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> contains only lowercase English letters. </li>
</ul>

View File

@@ -0,0 +1,73 @@
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Your task is to partition <code>nums</code> into <code>k</code><strong> </strong>non-empty <strong><span data-keyword="subarray-nonempty">subarrays</span></strong>. For each subarray, compute the bitwise <strong>XOR</strong> of all its elements.</p>
<p>Return the <strong>minimum</strong> possible value of the <strong>maximum XOR</strong> among these <code>k</code> subarrays.</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,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal partition is <code>[1]</code> and <code>[2, 3]</code>.</p>
<ul>
<li>XOR of the first subarray is <code>1</code>.</li>
<li>XOR of the second subarray is <code>2 XOR 3 = 1</code>.</li>
</ul>
<p>The maximum XOR among the subarrays is 1, which is the minimum possible.</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,3,2], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal partition is <code>[2]</code>, <code>[3, 3]</code>, and <code>[2]</code>.</p>
<ul>
<li>XOR of the first subarray is <code>2</code>.</li>
<li>XOR of the second subarray is <code>3 XOR 3 = 0</code>.</li>
<li>XOR of the third subarray is <code>2</code>.</li>
</ul>
<p>The maximum XOR among the subarrays is 2, which is the minimum possible.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,3,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal partition is <code>[1, 1]</code> and <code>[2, 3, 1]</code>.</p>
<ul>
<li>XOR of the first subarray is <code>1 XOR 1 = 0</code>.</li>
<li>XOR of the second subarray is <code>2 XOR 3 XOR 1 = 0</code>.</li>
</ul>
<p>The maximum XOR among the subarrays is 0, which is the minimum possible.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 250</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= n</code></li>
</ul>

View File

@@ -0,0 +1,74 @@
<p>You are given four integers <code>sx</code>, <code>sy</code>, <code>tx</code>, and <code>ty</code>, representing two points <code>(sx, sy)</code> and <code>(tx, ty)</code> on an infinitely large 2D grid.</p>
<p>You start at <code>(sx, sy)</code>.</p>
<p>At any point <code>(x, y)</code>, define <code>m = max(x, y)</code>. You can either:</p>
<ul>
<li>Move to <code>(x + m, y)</code>, or</li>
<li>Move to <code>(x, y + m)</code>.</li>
</ul>
<p>Return the <strong>minimum</strong> number of moves required to reach <code>(tx, ty)</code>. If it is impossible to reach the target, 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">sx = 1, sy = 2, tx = 5, ty = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal path is:</p>
<ul>
<li>Move 1: <code>max(1, 2) = 2</code>. Increase the y-coordinate by 2, moving from <code>(1, 2)</code> to <code>(1, 2 + 2) = (1, 4)</code>.</li>
<li>Move 2: <code>max(1, 4) = 4</code>. Increase the x-coordinate by 4, moving from <code>(1, 4)</code> to <code>(1 + 4, 4) = (5, 4)</code>.</li>
</ul>
<p>Thus, the minimum number of moves to reach <code>(5, 4)</code> is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">sx = 0, sy = 1, tx = 2, ty = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal path is:</p>
<ul>
<li>Move 1: <code>max(0, 1) = 1</code>. Increase the x-coordinate by 1, moving from <code>(0, 1)</code> to <code>(0 + 1, 1) = (1, 1)</code>.</li>
<li>Move 2: <code>max(1, 1) = 1</code>. Increase the x-coordinate by 1, moving from <code>(1, 1)</code> to <code>(1 + 1, 1) = (2, 1)</code>.</li>
<li>Move 3: <code>max(2, 1) = 2</code>. Increase the y-coordinate by 2, moving from <code>(2, 1)</code> to <code>(2, 1 + 2) = (2, 3)</code>.</li>
</ul>
<p>Thus, the minimum number of moves to reach <code>(2, 3)</code> is 3.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">sx = 1, sy = 1, tx = 2, ty = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>It is impossible to reach <code>(2, 2)</code> from <code>(1, 1)</code> using the allowed moves. Thus, the answer is -1.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= sx &lt;= tx &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= sy &lt;= ty &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,76 @@
<p>You are given an integer <code>n</code> and an undirected graph 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>, time<sub>i</sub>]</code> indicates an undirected edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> that can be removed at <code>time<sub>i</sub></code>.</p>
<p>You are also given an integer <code>k</code>.</p>
<p>Initially, the graph may be connected or disconnected. Your task is to find the <strong>minimum</strong> time <code>t</code> such that after removing all edges with <code>time &lt;= t</code>, the graph contains <strong>at least</strong> <code>k</code> connected components.</p>
<p>Return the <strong>minimum</strong> time <code>t</code>.</p>
<p>A <strong>connected component</strong> is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.</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,3]], k = 2</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/05/31/screenshot-2025-06-01-at-022724.png" style="width: 230px; height: 85px;" /></p>
<ul>
<li>Initially, there is one connected component <code>{0, 1}</code>.</li>
<li>At <code>time = 1</code> or <code>2</code>, the graph remains unchanged.</li>
<li>At <code>time = 3</code>, edge <code>[0, 1]</code> is removed, resulting in <code>k = 2</code> connected components <code>{0}</code>, <code>{1}</code>. Thus, the answer 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],[1,2,4]], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/05/31/screenshot-2025-06-01-at-022812.png" style="width: 180px; height: 164px;" /></p>
<ul>
<li>Initially, there is one connected component <code>{0, 1, 2}</code>.</li>
<li>At <code>time = 2</code>, edge <code>[0, 1]</code> is removed, resulting in two connected components <code>{0}</code>, <code>{1, 2}</code>.</li>
<li>At <code>time = 4</code>, edge <code>[1, 2]</code> is removed, resulting in <code>k = 3</code> connected components <code>{0}</code>, <code>{1}</code>, <code>{2}</code>. Thus, the answer 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 = 3, edges = [[0,2,5]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/05/31/screenshot-2025-06-01-at-022930.png" style="width: 180px; height: 155px;" /></p>
<ul>
<li>Since there are already <code>k = 2</code> disconnected components <code>{1}</code>, <code>{0, 2}</code>, no edge removal is needed. Thus, the answer is 0.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
<li><code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, time<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;= time<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= n</code></li>
<li>There are no duplicate edges.</li>
</ul>

View File

@@ -0,0 +1,47 @@
<p>You are given an integer <code>n</code>.</p>
<p>Return the concatenation of the <strong>hexadecimal</strong> representation of <code>n<sup>2</sup></code> and the <strong>hexatrigesimal</strong> representation of <code>n<sup>3</sup></code>.</p>
<p>A <strong>hexadecimal</strong> number is defined as a base-16 numeral system that uses the digits <code>0 &ndash; 9</code> and the uppercase letters <code>A - F</code> to represent values from 0 to 15.</p>
<p>A <strong>hexatrigesimal</strong> number is defined as a base-36 numeral system that uses the digits <code>0 &ndash; 9</code> and the uppercase letters <code>A - Z</code> to represent values from 0 to 35.</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 = 13</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;A91P1&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>n<sup>2</sup> = 13 * 13 = 169</code>. In hexadecimal, it converts to <code>(10 * 16) + 9 = 169</code>, which corresponds to <code>&quot;A9&quot;</code>.</li>
<li><code>n<sup>3</sup> = 13 * 13 * 13 = 2197</code>. In hexatrigesimal, it converts to <code>(1 * 36<sup>2</sup>) + (25 * 36) + 1 = 2197</code>, which corresponds to <code>&quot;1P1&quot;</code>.</li>
<li>Concatenating both results gives <code>&quot;A9&quot; + &quot;1P1&quot; = &quot;A91P1&quot;</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 = 36</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;5101000&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>n<sup>2</sup> = 36 * 36 = 1296</code>. In hexadecimal, it converts to <code>(5 * 16<sup>2</sup>) + (1 * 16) + 0 = 1296</code>, which corresponds to <code>&quot;510&quot;</code>.</li>
<li><code>n<sup>3</sup> = 36 * 36 * 36 = 46656</code>. In hexatrigesimal, it converts to <code>(1 * 36<sup>3</sup>) + (0 * 36<sup>2</sup>) + (0 * 36) + 0 = 46656</code>, which corresponds to <code>&quot;1000&quot;</code>.</li>
<li>Concatenating both results gives <code>&quot;510&quot; + &quot;1000&quot; = &quot;5101000&quot;</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>

View File

@@ -0,0 +1,79 @@
<p>You are given an integer <code>n</code>, representing <code>n</code> nodes numbered from 0 to <code>n - 1</code> and a list of <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, s<sub>i</sub>, must<sub>i</sub>]</code>:</p>
<ul>
<li><code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> indicates an undirected edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</li>
<li><code>s<sub>i</sub></code> is the strength of the edge.</li>
<li><code>must<sub>i</sub></code> is an integer (0 or 1). If <code>must<sub>i</sub> == 1</code>, the edge <strong>must</strong> be included in the<strong> </strong><strong>spanning tree</strong>. These edges <strong>cannot</strong> be <strong>upgraded</strong>.</li>
</ul>
<p>You are also given an integer <code>k</code>, the <strong>maximum</strong> number of upgrades you can perform. Each upgrade <strong>doubles</strong> the strength of an edge, and each eligible edge (with <code>must<sub>i</sub> == 0</code>) can be upgraded <strong>at most</strong> once.</p>
<p>The <strong>stability</strong> of a spanning tree is defined as the <strong>minimum</strong> strength score among all edges included in it.</p>
<p>Return the <strong>maximum</strong> possible stability of any valid spanning tree. If it is impossible to connect all nodes, return <code>-1</code>.</p>
<p><strong>Note</strong>: A <strong>spanning tree</strong> of a graph with <code>n</code> nodes is a subset of the edges that connects all nodes together (i.e. the graph is <strong>connected</strong>) <em>without</em> forming any cycles, and uses <strong>exactly</strong> <code>n - 1</code> edges.</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,2,1],[1,2,3,0]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Edge <code>[0,1]</code> with strength = 2 must be included in the spanning tree.</li>
<li>Edge <code>[1,2]</code> is optional and can be upgraded from 3 to 6 using one upgrade.</li>
<li>The resulting spanning tree includes these two edges with strengths 2 and 6.</li>
<li>The minimum strength in the spanning tree is 2, which is the maximum possible stability.</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,4,0],[1,2,3,0],[0,2,1,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since all edges are optional and up to <code>k = 2</code> upgrades are allowed.</li>
<li>Upgrade edges <code>[0,1]</code> from 4 to 8 and <code>[1,2]</code> from 3 to 6.</li>
<li>The resulting spanning tree includes these two edges with strengths 8 and 6.</li>
<li>The minimum strength in the tree is 6, which is the maximum possible stability.</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,1,1],[1,2,1,1],[2,0,1,1]], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>All edges are mandatory and form a cycle, which violates the spanning tree property of acyclicity. Thus, the answer 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;= edges.length &lt;= 10<sup>5</sup></code></li>
<li><code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, s<sub>i</sub>, must<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;= s<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
<li><code>must<sub>i</sub></code> is either <code>0</code> or <code>1</code>.</li>
<li><code>0 &lt;= k &lt;= n</code></li>
<li>There are no duplicate edges.</li>
</ul>

View File

@@ -0,0 +1,73 @@
<p>You are given an integer <code>n</code> and an <strong>undirected</strong> graph with <code>n</code> nodes labeled from 0 to <code>n - 1</code> and a 2D array <code>edges</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>You are also given a string <code>label</code> of length <code>n</code>, where <code>label[i]</code> is the character associated with node <code>i</code>.</p>
<p>You may start at any node and move to any adjacent node, visiting each node <strong>at most</strong> once.</p>
<p>Return the <strong>maximum</strong> possible length of a <strong><span data-keyword="palindrome-string">palindrome</span></strong> that can be formed by visiting a set of <strong>unique</strong> nodes along a valid path.</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,2]], label = &quot;aba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Exp</strong><strong>lanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/06/13/screenshot-2025-06-13-at-230714.png" style="width: 250px; height: 85px;" /></p>
<ul>
<li>The longest palindromic path is from node 0 to node 2 via node 1, following the path <code>0 &rarr; 1 &rarr; 2</code> forming string <code>&quot;aba&quot;</code>.</li>
<li>This is a valid palindrome of length 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],[0,2]], label = &quot;abc&quot;</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/06/13/screenshot-2025-06-13-at-230017.png" style="width: 200px; height: 150px;" /></p>
<ul>
<li>No path with more than one node forms a palindrome.</li>
<li>The best option is any single node, giving a palindrome of length 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[0,2],[0,3],[3,1]], label = &quot;bbac&quot;</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/06/13/screenshot-2025-06-13-at-230508.png" style="width: 200px; height: 200px;" /></p>
<ul>
<li>The longest palindromic path is from node 0 to node 1, following the path <code>0 &rarr; 3 &rarr; 1</code>, forming string <code>&quot;bcb&quot;</code>.</li>
<li>This is a valid palindrome of length 3.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 14</code></li>
<li><code>n - 1 &lt;= edges.length &lt;= n * (n - 1) / 2</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 - 1</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li><code>label.length == n</code></li>
<li><code>label</code> consists of lowercase English letters.</li>
<li>There are no duplicate edges.</li>
</ul>

View File

@@ -0,0 +1,128 @@
<p>Table: <code>drivers</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| driver_id | int |
| driver_name | varchar |
+-------------+---------+
driver_id is the unique identifier for this table.
Each row contains information about a driver.
</pre>
<p>Table: <code>trips</code></p>
<pre>
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| trip_id | int |
| driver_id | int |
| trip_date | date |
| distance_km | decimal |
| fuel_consumed | decimal |
+---------------+---------+
trip_id is the unique identifier for this table.
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
</pre>
<p>Write a solution to find drivers whose <strong>fuel efficiency has improved</strong> by <strong>comparing</strong> their average fuel efficiency in the<strong> first half</strong> of the year with the <strong>second half</strong> of the year.</p>
<ul>
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
</ul>
<p>Return <em>the result table ordered by efficiency improvement in <strong>descending</strong> order, then by driver 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>drivers table:</p>
<pre class="example-io">
+-----------+---------------+
| driver_id | driver_name |
+-----------+---------------+
| 1 | Alice Johnson |
| 2 | Bob Smith |
| 3 | Carol Davis |
| 4 | David Wilson |
| 5 | Emma Brown |
+-----------+---------------+
</pre>
<p>trips table:</p>
<pre class="example-io">
+---------+-----------+------------+-------------+---------------+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
+---------+-----------+------------+-------------+---------------+
| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |
| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |
| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |
| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |
| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |
| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |
| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |
| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |
| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |
| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |
| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |
| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |
+---------+-----------+------------+-------------+---------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-----------+---------------+------------------+-------------------+------------------------+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
+-----------+---------------+------------------+-------------------+------------------------+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
+-----------+---------------+------------------+-------------------+------------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Alice Johnson (driver_id = 1):</strong>
<ul>
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
</ul>
</li>
<li><strong>Bob Smith (driver_id = 2):</strong>
<ul>
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
<li>Second half average efficiency: 13.33</li>
<li>Efficiency improvement: 13.33 - 11.24 = 2.10 (rounded to 2 decimal places)</li>
</ul>
</li>
<li><strong>Drivers not included:</strong>
<ul>
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
</ul>
</li>
</ul>
<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
</div>

View File

@@ -0,0 +1,80 @@
<p>You are given an integer array <code>nums</code> and an integer <code>maxC</code>.</p>
<p>A <strong><span data-keyword="subarray">subarray</span></strong> is called <strong>stable</strong> if the <em>highest common factor (HCF)</em> of all its elements is <strong>greater than or equal to</strong> 2.</p>
<p>The <strong>stability factor</strong> of an array is defined as the length of its <strong>longest</strong> stable subarray.</p>
<p>You may modify <strong>at most</strong> <code>maxC</code> elements of the array to any integer.</p>
<p>Return the <strong>minimum</strong> possible stability factor of the array after at most <code>maxC</code> modifications. If no stable subarray remains, return 0.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>highest common factor (HCF)</strong> of an array is the largest integer that evenly divides all the array elements.</li>
<li>A <strong>subarray</strong> of length 1 is stable if its only element is greater than or equal to 2, since <code>HCF([x]) = x</code>.</li>
</ul>
<div class="notranslate" style="all: initial;"> </div>
<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,5,10], maxC = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The stable subarray <code>[5, 10]</code> has <code>HCF = 5</code>, which has a stability factor of 2.</li>
<li>Since <code>maxC = 1</code>, one optimal strategy is to change <code>nums[1]</code> to <code>7</code>, resulting in <code>nums = [3, 7, 10]</code>.</li>
<li>Now, no subarray of length greater than 1 has <code>HCF &gt;= 2</code>. Thus, the minimum possible stability factor 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 = [2,6,8], maxC = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The subarray <code>[2, 6, 8]</code> has <code>HCF = 2</code>, which has a stability factor of 3.</li>
<li>Since <code>maxC = 2</code>, one optimal strategy is to change <code>nums[1]</code> to 3 and <code>nums[2]</code> to 5, resulting in <code>nums = [2, 3, 5]</code>.</li>
<li>Now, no subarray of length greater than 1 has <code>HCF &gt;= 2</code>. Thus, the minimum possible stability factor is 1.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,9,6], maxC = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The stable subarrays are:
<ul>
<li><code>[2, 4]</code> with <code>HCF = 2</code> and stability factor of 2.</li>
<li><code>[9, 6]</code> with <code>HCF = 3</code> and stability factor of 2.</li>
</ul>
</li>
<li>Since <code>maxC = 1</code>, the stability factor of 2 cannot be reduced due to two separate stable subarrays. Thus, the minimum possible stability factor is 2.</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>9</sup></code></li>
<li><code>0 &lt;= maxC &lt;= n</code></li>
</ul>

View File

@@ -0,0 +1,55 @@
<p data-end="331" data-start="85">You are given an undirected connected graph with <code data-end="137" data-start="134">n</code> nodes labeled from 0 to <code data-end="171" data-start="164">n - 1</code> and a 2D integer array <code data-end="202" data-start="195">edges</code> where <code data-end="234" data-start="209">edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> denotes an undirected edge between node <code data-end="279" data-start="275">u<sub>i</sub></code> and node <code data-end="293" data-start="289">v<sub>i</sub></code> with weight <code data-end="310" data-start="306">w<sub>i</sub></code>, and an integer <code data-end="330" data-start="327">k</code>.</p>
<p data-end="461" data-start="333">You are allowed to remove any number of edges from the graph such that the resulting graph has <strong>at most</strong> <code data-end="439" data-start="436">k</code> connected components.</p>
<p data-end="589" data-start="463">The <strong>cost</strong> of a component is defined as the <strong>maximum</strong> edge weight in that component. If a component has no edges, its cost is 0.</p>
<p data-end="760" data-start="661">Return the <strong>minimum</strong> possible value of the <strong>maximum</strong> cost among all components <strong data-end="759" data-start="736">after such removals</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">n = 5, edges = [[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/19/minimizemaximumm.jpg" style="width: 535px; height: 225px;" /></p>
<ul>
<li data-end="1070" data-start="1021">Remove the edge between nodes 3 and 4 (weight 6).</li>
<li data-end="1141" data-start="1073">The resulting components have costs of 0 and 4, so the overall maximum cost is 4.</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, edges = [[0,1,5],[1,2,5],[2,3,5]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/19/minmax2.jpg" style="width: 315px; height: 55px;" /></p>
<ul>
<li data-end="1315" data-start="1251">No edge can be removed, since allowing only one component (<code>k = 1</code>) requires the graph to stay fully connected.</li>
<li data-end="1389" data-start="1318">That single component&rsquo;s cost equals its largest edge weight, which is 5.</li>
</ul>
</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>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
<li><code>edges[i].length == 3</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n</code></li>
<li>The input graph is connected.</li>
</ul>

View File

@@ -0,0 +1,83 @@
<p>You are given an integer <code>n</code> and a <strong>directed</strong> graph 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>, start<sub>i</sub>, end<sub>i</sub>]</code> indicates an edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> that can <strong>only</strong> be used at any integer time <code>t</code> such that <code>start<sub>i</sub> &lt;= t &lt;= end<sub>i</sub></code>.</p>
<p>You start at node 0 at time 0.</p>
<p>In one unit of time, you can either:</p>
<ul>
<li>Wait at your current node without moving, or</li>
<li>Travel along an outgoing edge from your current node if the current time <code>t</code> satisfies <code>start<sub>i</sub> &lt;= t &lt;= end<sub>i</sub></code>.</li>
</ul>
<p>Return the <strong>minimum</strong> time required to reach node <code>n - 1</code>. If it is impossible, 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,0,1],[1,2,2,5]]</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/06/05/screenshot-2025-06-06-at-004535.png" style="width: 150px; height: 141px;" /></p>
<p>The optimal path is:</p>
<ul>
<li>At time <code>t = 0</code>, take the edge <code>(0 &rarr; 1)</code> which is available from 0 to 1. You arrive at node 1 at time <code>t = 1</code>, then wait until <code>t = 2</code>.</li>
<li>At time <code>t = <code>2</code></code>, take the edge <code>(1 &rarr; 2)</code> which is available from 2 to 5. You arrive at node 2 at time 3.</li>
</ul>
<p>Hence, the minimum time to reach node 2 is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, edges = [[0,1,0,3],[1,3,7,8],[0,2,1,5],[2,3,4,7]]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2025/06/05/screenshot-2025-06-06-at-004757.png" style="width: 170px; height: 219px;" /></p>
<p>The optimal path is:</p>
<ul>
<li>Wait at node 0 until time <code>t = 1</code>, then take the edge <code>(0 &rarr; 2)</code> which is available from 1 to 5. You arrive at node 2 at <code>t = 2</code>.</li>
<li>Wait at node 2 until time <code>t = 4</code>, then take the edge <code>(2 &rarr; 3)</code> which is available from 4 to 7. You arrive at node 3 at <code>t = 5</code>.</li>
</ul>
<p>Hence, the minimum time to reach node 3 is 5.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, edges = [[1,0,1,3],[1,2,3,5]]</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/06/05/screenshot-2025-06-06-at-004914.png" style="width: 150px; height: 145px;" /></p>
<ul>
<li>Since there is no outgoing edge from node 0, it is impossible to reach node 2. Hence, the output is -1.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, start<sub>i</sub>, end<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,138 @@
<p>Table: <code>students</code></p>
<pre>
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| student_id | int |
| student_name | varchar |
| major | varchar |
+--------------+---------+
student_id is the unique identifier for this table.
Each row contains information about a student and their academic major.
</pre>
<p>Table: <code>study_sessions</code></p>
<pre>
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| session_id | int |
| student_id | int |
| subject | varchar |
| session_date | date |
| hours_studied | decimal |
+---------------+---------+
session_id is the unique identifier for this table.
Each row represents a study session by a student for a specific subject.
</pre>
<p>Write a solution to find students who follow the <strong>Study Spiral Pattern</strong>&nbsp;- students who consistently study multiple subjects in a rotating cycle.</p>
<ul>
<li>A Study Spiral Pattern means a student studies at least <code>3</code><strong> different subjects</strong> in a repeating sequence</li>
<li>The pattern must repeat for <strong>at least </strong><code>2</code><strong> complete cycles</strong> (minimum <code>6</code> study sessions)</li>
<li>Sessions must be <strong>consecutive dates</strong> with no gaps longer than <code>2</code> days between sessions</li>
<li>Calculate the <strong>cycle length</strong> (number of different subjects in the pattern)</li>
<li>Calculate the <strong>total study hours</strong> across all sessions in the pattern</li>
<li>Only include students with cycle length of <strong>at least </strong><code>3</code><strong> subjects</strong></li>
</ul>
<p>Return <em>the result table ordered by cycle length in <strong>descending</strong> order, then by total study hours in <strong>descending</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>students table:</p>
<pre class="example-io">
+------------+--------------+------------------+
| student_id | student_name | major |
+------------+--------------+------------------+
| 1 | Alice Chen | Computer Science |
| 2 | Bob Johnson | Mathematics |
| 3 | Carol Davis | Physics |
| 4 | David Wilson | Chemistry |
| 5 | Emma Brown | Biology |
+------------+--------------+------------------+
</pre>
<p>study_sessions table:</p>
<pre class="example-io">
+------------+------------+------------+--------------+---------------+
| session_id | student_id | subject | session_date | hours_studied |
+------------+------------+------------+--------------+---------------+
| 1 | 1 | Math | 2023-10-01 | 2.5 |
| 2 | 1 | Physics | 2023-10-02 | 3.0 |
| 3 | 1 | Chemistry | 2023-10-03 | 2.0 |
| 4 | 1 | Math | 2023-10-04 | 2.5 |
| 5 | 1 | Physics | 2023-10-05 | 3.0 |
| 6 | 1 | Chemistry | 2023-10-06 | 2.0 |
| 7 | 2 | Algebra | 2023-10-01 | 4.0 |
| 8 | 2 | Calculus | 2023-10-02 | 3.5 |
| 9 | 2 | Statistics | 2023-10-03 | 2.5 |
| 10 | 2 | Geometry | 2023-10-04 | 3.0 |
| 11 | 2 | Algebra | 2023-10-05 | 4.0 |
| 12 | 2 | Calculus | 2023-10-06 | 3.5 |
| 13 | 2 | Statistics | 2023-10-07 | 2.5 |
| 14 | 2 | Geometry | 2023-10-08 | 3.0 |
| 15 | 3 | Biology | 2023-10-01 | 2.0 |
| 16 | 3 | Chemistry | 2023-10-02 | 2.5 |
| 17 | 3 | Biology | 2023-10-03 | 2.0 |
| 18 | 3 | Chemistry | 2023-10-04 | 2.5 |
| 19 | 4 | Organic | 2023-10-01 | 3.0 |
| 20 | 4 | Physical | 2023-10-05 | 2.5 |
+------------+------------+------------+--------------+---------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+------------+--------------+------------------+--------------+-------------------+
| student_id | student_name | major | cycle_length | total_study_hours |
+------------+--------------+------------------+--------------+-------------------+
| 2 | Bob Johnson | Mathematics | 4 | 26.0 |
| 1 | Alice Chen | Computer Science | 3 | 15.0 |
+------------+--------------+------------------+--------------+-------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Alice Chen (student_id = 1):</strong>
<ul>
<li>Study sequence: Math &rarr; Physics &rarr; Chemistry &rarr; Math &rarr; Physics &rarr; Chemistry</li>
<li>Pattern: 3 subjects (Math, Physics, Chemistry) repeating for 2 complete cycles</li>
<li>Consecutive dates: Oct 1-6 with no gaps &gt; 2 days</li>
<li>Cycle length: 3 subjects</li>
<li>Total hours: 2.5 + 3.0 + 2.0 + 2.5 + 3.0 + 2.0 = 15.0 hours</li>
</ul>
</li>
<li><strong>Bob Johnson (student_id = 2):</strong>
<ul>
<li>Study sequence: Algebra &rarr; Calculus &rarr; Statistics &rarr; Geometry &rarr; Algebra &rarr; Calculus &rarr; Statistics &rarr; Geometry</li>
<li>Pattern: 4 subjects (Algebra, Calculus, Statistics, Geometry) repeating for 2 complete cycles</li>
<li>Consecutive dates: Oct 1-8 with no gaps &gt; 2 days</li>
<li>Cycle length: 4 subjects</li>
<li>Total hours: 4.0 + 3.5 + 2.5 + 3.0 + 4.0 + 3.5 + 2.5 + 3.0 = 26.0&nbsp;hours</li>
</ul>
</li>
<li><strong>Students not included:</strong>
<ul>
<li>Carol Davis (student_id = 3): Only 2 subjects (Biology, Chemistry) - doesn&#39;t meet minimum 3 subjects requirement</li>
<li>David Wilson (student_id = 4): Only 2 study sessions with a 4-day gap - doesn&#39;t meet consecutive dates requirement</li>
<li>Emma Brown (student_id = 5): No study sessions recorded</li>
</ul>
</li>
</ul>
<p>The result table is ordered by cycle_length in descending order, then by total_study_hours in descending order.</p>
</div>

View File

@@ -0,0 +1,126 @@
<p>Table: <code>employees</code></p>
<pre>
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| employee_id | int |
| employee_name | varchar |
| department | varchar |
+---------------+---------+
employee_id is the unique identifier for this table.
Each row contains information about an employee and their department.
</pre>
<p>Table: <code>meetings</code></p>
<pre>
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| meeting_id | int |
| employee_id | int |
| meeting_date | date |
| meeting_type | varchar |
| duration_hours| decimal |
+---------------+---------+
meeting_id is the unique identifier for this table.
Each row represents a meeting attended by an employee. meeting_type can be &#39;Team&#39;, &#39;Client&#39;, or &#39;Training&#39;.
</pre>
<p>Write a solution to find employees who are <strong>meeting-heavy</strong> - employees who spend more than <code>50%</code> of their working time in meetings during any given week.</p>
<ul>
<li>Assume a standard work week is <code>40</code><strong> hours</strong></li>
<li>Calculate <strong>total meeting hours</strong> per employee <strong>per week</strong> (<strong>Monday to Sunday</strong>)</li>
<li>An employee is meeting-heavy if their weekly meeting hours <code>&gt;</code> <code>20</code> hours (<code>50%</code> of <code>40</code> hours)</li>
<li>Count how many weeks each employee was meeting-heavy</li>
<li><strong>Only include</strong> employees who were meeting-heavy for <strong>at least </strong><code>2</code><strong> weeks</strong></li>
</ul>
<p>Return <em>the result table ordered by the number of meeting-heavy weeks in <strong>descending</strong> order, then by employee 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>employees table:</p>
<pre class="example-io">
+-------------+----------------+-------------+
| employee_id | employee_name | department |
+-------------+----------------+-------------+
| 1 | Alice Johnson | Engineering |
| 2 | Bob Smith | Marketing |
| 3 | Carol Davis | Sales |
| 4 | David Wilson | Engineering |
| 5 | Emma Brown | HR |
+-------------+----------------+-------------+
</pre>
<p>meetings table:</p>
<pre class="example-io">
+------------+-------------+--------------+--------------+----------------+
| meeting_id | employee_id | meeting_date | meeting_type | duration_hours |
+------------+-------------+--------------+--------------+----------------+
| 1 | 1 | 2023-06-05 | Team | 8.0 |
| 2 | 1 | 2023-06-06 | Client | 6.0 |
| 3 | 1 | 2023-06-07 | Training | 7.0 |
| 4 | 1 | 2023-06-12 | Team | 12.0 |
| 5 | 1 | 2023-06-13 | Client | 9.0 |
| 6 | 2 | 2023-06-05 | Team | 15.0 |
| 7 | 2 | 2023-06-06 | Client | 8.0 |
| 8 | 2 | 2023-06-12 | Training | 10.0 |
| 9 | 3 | 2023-06-05 | Team | 4.0 |
| 10 | 3 | 2023-06-06 | Client | 3.0 |
| 11 | 4 | 2023-06-05 | Team | 25.0 |
| 12 | 4 | 2023-06-19 | Client | 22.0 |
| 13 | 5 | 2023-06-05 | Training | 2.0 |
+------------+-------------+--------------+--------------+----------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-------------+----------------+-------------+---------------------+
| employee_id | employee_name | department | meeting_heavy_weeks |
+-------------+----------------+-------------+---------------------+
| 1 | Alice Johnson | Engineering | 2 |
| 4 | David Wilson | Engineering | 2 |
+-------------+----------------+-------------+---------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Alice Johnson (employee_id = 1):</strong>
<ul>
<li>Week of June 5-11 (2023-06-05 to 2023-06-11): 8.0 + 6.0 + 7.0 = 21.0 hours (&gt; 20 hours)</li>
<li>Week of June 12-18 (2023-06-12 to 2023-06-18): 12.0 + 9.0 = 21.0 hours (&gt; 20 hours)</li>
<li>Meeting-heavy for 2 weeks</li>
</ul>
</li>
<li><strong>David Wilson (employee_id = 4):</strong>
<ul>
<li>Week of June 5-11: 25.0 hours (&gt; 20 hours)</li>
<li>Week of June 19-25: 22.0 hours (&gt; 20 hours)</li>
<li>Meeting-heavy for 2 weeks</li>
</ul>
</li>
<li><strong>Employees not included:</strong>
<ul>
<li>Bob Smith (employee_id = 2): Week of June 5-11: 15.0 + 8.0 = 23.0 hours (&gt; 20), Week of June 12-18: 10.0 hours (&lt; 20). Only 1 meeting-heavy week</li>
<li>Carol Davis (employee_id = 3): Week of June 5-11: 4.0 + 3.0 = 7.0 hours (&lt; 20). No meeting-heavy weeks</li>
<li>Emma Brown (employee_id = 5): Week of June 5-11: 2.0 hours (&lt; 20). No meeting-heavy weeks</li>
</ul>
</li>
</ul>
<p>The result table is ordered by meeting_heavy_weeks in descending order, then by employee name in ascending order.</p>
</div>

View File

@@ -0,0 +1,119 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters and the special characters: <code>*</code>, <code>#</code>, and <code>%</code>.</p>
<p>Build a new string <code>result</code> by processing <code>s</code> according to the following rules from left to right:</p>
<ul>
<li>If the letter is a <strong>lowercase</strong> English letter append it to <code>result</code>.</li>
<li>A <code>&#39;*&#39;</code> <strong>removes</strong> the last character from <code>result</code>, if it exists.</li>
<li>A <code>&#39;#&#39;</code> <strong>duplicates</strong> the current <code>result</code> and <strong>appends</strong> it to itself.</li>
<li>A <code>&#39;%&#39;</code> <strong>reverses</strong> the current <code>result</code>.</li>
</ul>
<p>Return the final string <code>result</code> after processing all characters in <code>s</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">s = &quot;a#b%*&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;ba&quot;</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>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;a&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;a&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;a&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;aa&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;b&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;b&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;aab&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>&#39;%&#39;</code></td>
<td style="border: 1px solid black;">Reverse <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;baa&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;ba&quot;</code></td>
</tr>
</tbody>
</table>
<p>Thus, the final <code>result</code> is <code>&quot;ba&quot;</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;z*#&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</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>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;z&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;z&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;z&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate the string</td>
<td style="border: 1px solid black;"><code>&quot;&quot;</code></td>
</tr>
</tbody>
</table>
<p>Thus, the final <code>result</code> is <code>&quot;&quot;</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 20</code></li>
<li><code>s</code> consists of only lowercase English letters and special characters <code>*</code>, <code>#</code>, and <code>%</code>.</li>
</ul>

View File

@@ -0,0 +1,184 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters and the special characters: <code>&#39;*&#39;</code>, <code>&#39;#&#39;</code>, and <code>&#39;%&#39;</code>.</p>
<p>You are also given an integer <code>k</code>.</p>
<p>Build a new string <code>result</code> by processing <code>s</code> according to the following rules from left to right:</p>
<ul>
<li>If the letter is a <strong>lowercase</strong> English letter append it to <code>result</code>.</li>
<li>A <code>&#39;*&#39;</code> <strong>removes</strong> the last character from <code>result</code>, if it exists.</li>
<li>A <code>&#39;#&#39;</code> <strong>duplicates</strong> the current <code>result</code> and <strong>appends</strong> it to itself.</li>
<li>A <code>&#39;%&#39;</code> <strong>reverses</strong> the current <code>result</code>.</li>
</ul>
<p>Return the <code>k<sup>th</sup></code> character of the final string <code>result</code>. If <code>k</code> is out of the bounds of <code>result</code>, return <code>&#39;.&#39;</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">s = &quot;a#b%*&quot;, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;a&quot;</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>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;a&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;a&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;a&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;aa&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;b&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;b&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;aab&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>&#39;%&#39;</code></td>
<td style="border: 1px solid black;">Reverse <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;baa&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;ba&quot;</code></td>
</tr>
</tbody>
</table>
<p>The final <code>result</code> is <code>&quot;ba&quot;</code>. The character at index <code>k = 1</code> is <code>&#39;a&#39;</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;cd%#*#&quot;, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;d&quot;</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>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;c&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;c&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;c&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;d&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;d&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;cd&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;%&#39;</code></td>
<td style="border: 1px solid black;">Reverse <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;dc&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;dcdc&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;dcd&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">5</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;dcddcd&quot;</code></td>
</tr>
</tbody>
</table>
<p>The final <code>result</code> is <code>&quot;dcddcd&quot;</code>. The character at index <code>k = 3</code> is <code>&#39;d&#39;</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;z*#&quot;, k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;.&quot;</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>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;z&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;z&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;z&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate the string</td>
<td style="border: 1px solid black;"><code>&quot;&quot;</code></td>
</tr>
</tbody>
</table>
<p>The final <code>result</code> is <code>&quot;&quot;</code>. Since index <code>k = 0</code> is out of bounds, the output is <code>&#39;.&#39;</code>.</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 only lowercase English letters and special characters <code>&#39;*&#39;</code>, <code>&#39;#&#39;</code>, and <code>&#39;%&#39;</code>.</li>
<li><code>0 &lt;= k &lt;= 10<sup>15</sup></code></li>
<li>The length of <code>result</code> after processing <code>s</code> will not exceed <code>10<sup>15</sup></code>.</li>
</ul>

View File

@@ -0,0 +1,74 @@
<p data-end="401" data-start="120">You are given an integer <code data-end="194" data-start="191">c</code> representing <code data-end="211" data-start="208">c</code> power stations, each with a unique identifier <code>id</code> from 1 to <code>c</code> (1based indexing).</p>
<p data-end="401" data-start="120">These stations are interconnected via <code data-end="295" data-start="292">n</code> <strong>bidirectional</strong> cables, represented by a 2D array <code data-end="357" data-start="344">connections</code>, where each element <code data-end="430" data-start="405">connections[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates a connection between station <code>u<sub>i</sub></code> and station <code>v<sub>i</sub></code>. Stations that are directly or indirectly connected form a <strong>power grid</strong>.</p>
<p data-end="626" data-start="586">Initially, <strong>all</strong> stations are online (operational).</p>
<p data-end="720" data-start="628">You are also given a 2D array <code data-end="667" data-start="658">queries</code>, where each query is one of the following <em>two</em> types:</p>
<ul data-end="995" data-start="722">
<li data-end="921" data-start="722">
<p data-end="921" data-start="724"><code data-end="732" data-start="724">[1, x]</code>: A maintenance check is requested for station <code data-end="782" data-start="779">x</code>. If station <code>x</code> is online, it resolves the check by itself. If station <code>x</code> is offline, the check is resolved by the operational station with the smallest <code>id</code> in the same <strong>power grid</strong> as <code>x</code>. If <strong>no</strong> <strong>operational</strong> station <em>exists</em> in that grid, return -1.</p>
</li>
<li data-end="995" data-start="923">
<p data-end="995" data-start="925"><code data-end="933" data-start="925">[2, x]</code>: Station <code data-end="946" data-start="943">x</code> goes offline (i.e., it becomes non-operational).</p>
</li>
</ul>
<p data-end="1106" data-start="997">Return an array of integers representing the results of each query of type <code data-end="1080" data-start="1072">[1, x]</code> in the <strong>order</strong> they appear.</p>
<p data-end="1106" data-start="997"><strong>Note:</strong> The power grid preserves its structure; an offline (nonoperational) node remains part of its grid and taking it offline does not alter connectivity.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">c = 5, connections = [[1,2],[2,3],[3,4],[4,5]], queries = [[1,3],[2,1],[1,1],[2,2],[1,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/04/15/powergrid.jpg" style="width: 361px; height: 42px;" /></p>
<ul>
<li data-end="223" data-start="143">Initially, all stations <code>{1, 2, 3, 4, 5}</code> are online and form a single power grid.</li>
<li data-end="322" data-start="226">Query <code>[1,3]</code>: Station 3 is online, so the maintenance check is resolved by station 3.</li>
<li data-end="402" data-start="325">Query <code>[2,1]</code>: Station 1 goes offline. The remaining online stations are <code>{2, 3, 4, 5}</code>.</li>
<li data-end="557" data-start="405">Query <code>[1,1]</code>: Station 1 is offline, so the check is resolved by the operational station with the smallest <code>id</code> among <code>{2, 3, 4, 5}</code>, which is station 2.</li>
<li data-end="641" data-start="560">Query <code>[2,2]</code>: Station 2 goes offline. The remaining online stations are <code>{3, 4, 5}</code>.</li>
<li data-end="800" data-start="644">Query <code>[1,2]</code>: Station 2 is offline, so the check is resolved by the operational station with the smallest <code>id</code> among <code>{3, 4, 5}</code>, which is station 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">c = 3, connections = [], queries = [[1,1],[2,1],[1,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,-1]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="976" data-start="909">There are no connections, so each station is its own isolated grid.</li>
<li data-end="1096" data-start="979">Query <code>[1,1]</code>: Station 1 is online in its isolated grid, so the maintenance check is resolved by station 1.</li>
<li data-end="1135" data-start="1099">Query <code>[2,1]</code>: Station 1 goes offline.</li>
<li data-end="1237" data-start="1138">Query <code>[1,1]</code>: Station 1 is offline and there are no other stations in its grid, so the result is -1.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="155" data-start="139"><code>1 &lt;= c &lt;= 10<sup>5</sup></code></li>
<li data-end="213" data-start="158"><code>0 &lt;= n == connections.length &lt;= min(10<sup>5</sup>, c * (c - 1) / 2)</code></li>
<li data-end="244" data-start="216"><code>connections[i].length == 2</code></li>
<li data-end="295" data-start="247"><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= c</code></li>
<li data-end="338" data-start="298"><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li data-end="374" data-start="341"><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li>
<li data-end="401" data-start="377"><code>queries[i].length == 2</code></li>
<li data-end="436" data-start="404"><code>queries[i][0]</code> is either 1 or 2.</li>
<li data-end="462" data-start="439"><code>1 &lt;= queries[i][1] &lt;= c</code></li>
</ul>

View File

@@ -0,0 +1,76 @@
<p>You are given an array of strings <code>words</code>. For each index <code>i</code> in the range <code>[0, words.length - 1]</code>, perform the following steps:</p>
<ul>
<li>Remove the element at index <code>i</code> from the <code>words</code> array.</li>
<li>Compute the <strong>length</strong> of the <strong>longest common <span data-keyword="string-prefix">prefix</span></strong> among all <strong>adjacent</strong> pairs in the modified array.</li>
</ul>
<p>Return an array <code>answer</code>, where <code>answer[i]</code> is the length of the longest common prefix between the adjacent pairs after removing the element at index <code>i</code>. If <strong>no</strong> adjacent pairs remain or if <strong>none</strong> share a common prefix, then <code>answer[i]</code> should be 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">words = [&quot;jump&quot;,&quot;run&quot;,&quot;run&quot;,&quot;jump&quot;,&quot;run&quot;]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,0,0,3,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Removing index 0:
<ul>
<li><code>words</code> becomes <code>[&quot;run&quot;, &quot;run&quot;, &quot;jump&quot;, &quot;run&quot;]</code></li>
<li>Longest adjacent pair is <code>[&quot;run&quot;, &quot;run&quot;]</code> having a common prefix <code>&quot;run&quot;</code> (length 3)</li>
</ul>
</li>
<li>Removing index 1:
<ul>
<li><code>words</code> becomes <code>[&quot;jump&quot;, &quot;run&quot;, &quot;jump&quot;, &quot;run&quot;]</code></li>
<li>No adjacent pairs share a common prefix (length 0)</li>
</ul>
</li>
<li>Removing index 2:
<ul>
<li><code>words</code> becomes <code>[&quot;jump&quot;, &quot;run&quot;, &quot;jump&quot;, &quot;run&quot;]</code></li>
<li>No adjacent pairs share a common prefix (length 0)</li>
</ul>
</li>
<li>Removing index 3:
<ul>
<li><code>words</code> becomes <code>[&quot;jump&quot;, &quot;run&quot;, &quot;run&quot;, &quot;run&quot;]</code></li>
<li>Longest adjacent pair is <code>[&quot;run&quot;, &quot;run&quot;]</code> having a common prefix <code>&quot;run&quot;</code> (length 3)</li>
</ul>
</li>
<li>Removing index 4:
<ul>
<li>words becomes <code>[&quot;jump&quot;, &quot;run&quot;, &quot;run&quot;, &quot;jump&quot;]</code></li>
<li>Longest adjacent pair is <code>[&quot;run&quot;, &quot;run&quot;]</code> having a common prefix <code>&quot;run&quot;</code> (length 3)</li>
</ul>
</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;dog&quot;,&quot;racer&quot;,&quot;car&quot;]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,0,0]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Removing any index results in an answer of 0.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= words[i].length &lt;= 10<sup>4</sup></code></li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
<li>The sum of <code>words[i].length</code> is smaller than or equal <code>10<sup>5</sup></code>.</li>
</ul>