1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-15 23:22:36 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-12-06 16:04:11 +08:00
parent 1ea1df8784
commit 19eb3b8380
59 changed files with 16595 additions and 10243 deletions

View File

@@ -0,0 +1,38 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>Choose three elements <code>a</code>, <code>b</code>, and <code>c</code> from <code>nums</code> at <strong>distinct</strong> indices such that the value of the expression <code>a + b - c</code> is maximized.</p>
<p>Return an integer denoting the <strong>maximum possible value</strong> of this expression.</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,4,2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p>We can choose <code>a = 4</code>, <code>b = 5</code>, and <code>c = 1</code>. The expression value is <code>4 + 5 - 1 = 8</code>, which is the maximum 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,0,5,-2,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">11</span></p>
<p><strong>Explanation:</strong></p>
<p>We can choose <code>a = 5</code>, <code>b = 4</code>, and <code>c = -2</code>. The expression value is <code>5 + 4 - (-2) = 11</code>, which is the maximum possible.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,57 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters.</p>
<p>Return an integer denoting the <strong>maximum</strong> number of <span data-keyword="substring-nonempty">substrings</span> you can split <code>s</code> into such that each <strong>substring</strong> starts with a <strong>distinct</strong> character (i.e., no two substrings start with the same character).</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;abab&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Split <code>&quot;abab&quot;</code> into <code>&quot;a&quot;</code> and <code>&quot;bab&quot;</code>.</li>
<li>Each substring starts with a distinct character i.e <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. Thus, the answer is 2.</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;abcd&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Split <code>&quot;abcd&quot;</code> into <code>&quot;a&quot;</code>, <code>&quot;b&quot;</code>, <code>&quot;c&quot;</code>, and <code>&quot;d&quot;</code>.</li>
<li>Each substring starts with a distinct character. 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">s = &quot;aaaa&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>All characters in <code>&quot;aaaa&quot;</code> are <code>&#39;a&#39;</code>.</li>
<li>Only one substring can start with <code>&#39;a&#39;</code>. Thus, the answer is 1.</li>
</ul>
</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 lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,128 @@
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>In one operation, you can <strong>increase or decrease </strong>any element of <code>nums</code> by <strong>exactly</strong> <code>k</code>.</p>
<p>You are also given a 2D integer array <code>queries</code>, where each <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each query, find the <strong>minimum</strong> number of operations required to make <strong>all</strong> elements in the <strong><span data-keyword="subarray-nonempty">subarray</span></strong> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code> <strong>equal</strong>. If it is impossible, the answer for that query is <code>-1</code>.</p>
<p>Return an array <code>ans</code>, where <code>ans[i]</code> is the answer for the <code>i<sup>th</sup></code> query.</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,4,7], k = 3, queries = [[0,1],[0,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal set of operations:</p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>[l<sub>i</sub>, r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;">Possibility</th>
<th style="border: 1px solid black;">Operations</th>
<th style="border: 1px solid black;">Final<br />
<code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>ans[i]</code></th>
</tr>
</tbody>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[0, 1]</td>
<td style="border: 1px solid black;">[1, 4]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;"><code>nums[0] + k = 1 + 3 = 4 = nums[1]</code></td>
<td style="border: 1px solid black;">[4, 4]</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0, 2]</td>
<td style="border: 1px solid black;">[1, 4, 7]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;"><code>nums[0] + k = 1 + 3 = 4 = nums[1]<br />
nums[2] - k = 7 - 3 = 4 = nums[1]</code></td>
<td style="border: 1px solid black;">[4, 4, 4]</td>
<td style="border: 1px solid black;">2</td>
</tr>
</tbody>
</table>
<p>Thus, <code>ans = [1, 2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4], k = 2, queries = [[0,2],[0,0],[1,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,0,1]</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal set of operations:</p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>[l<sub>i</sub>, r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;">Possibility</th>
<th style="border: 1px solid black;">Operations</th>
<th style="border: 1px solid black;">Final<br />
<code>nums[l<sub>i</sub>..r<sub>i</sub>]</code></th>
<th style="border: 1px solid black;"><code>ans[i]</code></th>
</tr>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">[0, 2]</td>
<td style="border: 1px solid black;">[1, 2, 4]</td>
<td style="border: 1px solid black;">No</td>
<td style="border: 1px solid black;">-</td>
<td style="border: 1px solid black;">[1, 2, 4]</td>
<td style="border: 1px solid black;">-1</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0, 0]</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;">Already equal</td>
<td style="border: 1px solid black;">[1]</td>
<td style="border: 1px solid black;">0</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[1, 2]</td>
<td style="border: 1px solid black;">[2, 4]</td>
<td style="border: 1px solid black;">Yes</td>
<td style="border: 1px solid black;"><code>nums[1] + k = 2 + 2 = 4 = nums[2]</code></td>
<td style="border: 1px solid black;">[4, 4]</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, <code>ans = [-1, 0, 1]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 4 &times; 10<sup>4</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 4 &times; 10<sup>4</sup></code></li>
<li><code><sup></sup>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li>
</ul>

View File

@@ -0,0 +1,51 @@
<p>Table: <code>Salary</code></p>
<pre>
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| name | varchar |
| sex | ENUM |
| salary | int |
+-------------+----------+
id is the primary key (column with unique values) for this table.
The sex column is ENUM (category) value of type (&#39;m&#39;, &#39;f&#39;).
The table contains information about an employee.
</pre>
<p>&nbsp;</p>
<p>Write a solution to swap all <code>&#39;f&#39;</code> and <code>&#39;m&#39;</code> values (i.e., change all <code>&#39;f&#39;</code> values to <code>&#39;m&#39;</code> and vice versa) with a <strong>single update statement</strong> and no intermediate temporary tables.</p>
<p>Note that you must write a single update statement, <strong>do not</strong> write any select statement for this problem.</p>
<p>The result format is in the following example.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong>
Salary table:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
+----+------+-----+--------+
<strong>Output:</strong>
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
+----+------+-----+--------+
<strong>Explanation:</strong>
(1, A) and (3, C) were changed from &#39;m&#39; to &#39;f&#39;.
(2, B) and (4, D) were changed from &#39;f&#39; to &#39;m&#39;.
</pre>

View File

@@ -0,0 +1,62 @@
<p>You are given a positive integer <code>n</code> and an integer <code>target</code>.</p>
<p>Return the <strong><span data-keyword="lexicographically-smaller-array">lexicographically smallest</span></strong> array of integers of size <code>n</code> such that:</p>
<ul>
<li>The <strong>sum</strong> of its elements equals <code>target</code>.</li>
<li>The <strong>absolute values</strong> of its elements form a <strong>permutation</strong> of size <code>n</code>.</li>
</ul>
<p>If no such array exists, return an empty array.</p>
<p>A <strong>permutation</strong> of size <code>n</code> is a rearrangement of integers <code>1, 2, ..., 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 = 3, target = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">[-3,1,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The arrays that sum to 0 and whose absolute values form a permutation of size 3 are:</p>
<ul>
<li><code>[-3, 1, 2]</code></li>
<li><code>[-3, 2, 1]</code></li>
<li><code>[-2, -1, 3]</code></li>
<li><code>[-2, 3, -1]</code></li>
<li><code>[-1, -2, 3]</code></li>
<li><code>[-1, 3, -2]</code></li>
<li><code>[1, -3, 2]</code></li>
<li><code>[1, 2, -3]</code></li>
<li><code>[2, -3, 1]</code></li>
<li><code>[2, 1, -3]</code></li>
<li><code>[3, -2, -1]</code></li>
<li><code>[3, -1, -2]</code></li>
</ul>
<p>The lexicographically smallest one is <code>[-3, 1, 2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, target = 10000000000</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no arrays that sum to <span class="example-io">10000000000 and whose absolute values form a permutation of size 1. Therefore, the answer is <code>[]</code>.</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>10</sup> &lt;= target &lt;= 10<sup>10</sup></code></li>
</ul>

View File

@@ -0,0 +1,46 @@
<p>Given an integer array <code>nums</code>, return the <strong>length</strong> of the <strong>longest <span data-keyword="subarray-nonempty">subarray</span></strong> that has a bitwise XOR of zero and contains an <strong>equal</strong> number of <strong>even</strong> and <strong>odd</strong> numbers. If no such subarray exists, return 0.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,1,3,2,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>[1, 3, 2, 0]</code> has bitwise XOR <code>1 XOR 3 XOR 2 XOR 0 = 0</code> and contains 2 even and 2 odd numbers.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,8,5,4,14,9,15]</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p>The whole array has bitwise XOR <code>0</code> and contains 4 even and 4 odd numbers.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No non-empty subarray satisfies both conditions.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,41 @@
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
<p>Let <code>s</code> be the <strong>binary representation</strong> of <code>n</code> without leading zeros.</p>
<p>The <strong>reverse</strong> of a binary string <code>s</code> is obtained by writing the characters of <code>s</code> in the opposite order.</p>
<p>You may flip any bit in <code>s</code> (change <code>0 &rarr; 1</code> or <code>1 &rarr; 0</code>). Each flip affects <strong>exactly</strong> one bit.</p>
<p>Return the <strong>minimum</strong> number of flips required to make <code>s</code> equal to the reverse of its original form.</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 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of 7 is <code>&quot;111&quot;</code>. Its reverse is also <code>&quot;111&quot;</code>, which is the same. Hence, no flips are needed.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The binary representation of 10 is <code>&quot;1010&quot;</code>. Its reverse is <code>&quot;0101&quot;</code>. All four bits must be flipped to make them equal. Thus, the minimum number of flips required is 4.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,97 @@
<p>Table: <code>course_completions</code></p>
<pre>
+-------------------+---------+
| Column Name | Type |
+-------------------+---------+
| user_id | int |
| course_id | int |
| course_name | varchar |
| completion_date | date |
| course_rating | int |
+-------------------+---------+
(user_id, course_id) is the combination of columns with unique values for this table.
Each row represents a completed course by a user with their rating (1-5 scale).
</pre>
<p>Write a solution to identify <strong>skill mastery pathways</strong> by analyzing course completion sequences among top-performing students:</p>
<ul>
<li>Consider only <strong>top-performing students</strong> (those who completed <strong>at least </strong><code>5</code><strong> courses</strong> with an <strong>average rating of </strong><code>4</code><strong> or higher</strong>).</li>
<li>For each top performer, identify the <strong>sequence of courses</strong> they completed in chronological order.</li>
<li>Find all <strong>consecutive course pairs</strong> (<code>Course A &rarr; Course B</code>) taken by these students.</li>
<li>Return the <strong>pair frequency</strong>, identifying which course transitions are most common among high achievers.</li>
</ul>
<p>Return <em>the result table ordered by</em> <em>pair frequency in <strong>descending</strong> order</em>&nbsp;<em>and then by first course name and second course 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>course_completions table:</p>
<pre class="example-io">
+---------+-----------+------------------+-----------------+---------------+
| user_id | course_id | course_name | completion_date | course_rating |
+---------+-----------+------------------+-----------------+---------------+
| 1 | 101 | Python Basics | 2024-01-05 | 5 |
| 1 | 102 | SQL Fundamentals | 2024-02-10 | 4 |
| 1 | 103 | JavaScript | 2024-03-15 | 5 |
| 1 | 104 | React Basics | 2024-04-20 | 4 |
| 1 | 105 | Node.js | 2024-05-25 | 5 |
| 1 | 106 | Docker | 2024-06-30 | 4 |
| 2 | 101 | Python Basics | 2024-01-08 | 4 |
| 2 | 104 | React Basics | 2024-02-14 | 5 |
| 2 | 105 | Node.js | 2024-03-20 | 4 |
| 2 | 106 | Docker | 2024-04-25 | 5 |
| 2 | 107 | AWS Fundamentals | 2024-05-30 | 4 |
| 3 | 101 | Python Basics | 2024-01-10 | 3 |
| 3 | 102 | SQL Fundamentals | 2024-02-12 | 3 |
| 3 | 103 | JavaScript | 2024-03-18 | 3 |
| 3 | 104 | React Basics | 2024-04-22 | 2 |
| 3 | 105 | Node.js | 2024-05-28 | 3 |
| 4 | 101 | Python Basics | 2024-01-12 | 5 |
| 4 | 108 | Data Science | 2024-02-16 | 5 |
| 4 | 109 | Machine Learning | 2024-03-22 | 5 |
+---------+-----------+------------------+-----------------+---------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+------------------+------------------+------------------+
| first_course | second_course | transition_count |
+------------------+------------------+------------------+
| Node.js | Docker | 2 |
| React Basics | Node.js | 2 |
| Docker | AWS Fundamentals | 1 |
| JavaScript | React Basics | 1 |
| Python Basics | React Basics | 1 |
| Python Basics | SQL Fundamentals | 1 |
| SQL Fundamentals | JavaScript | 1 |
+------------------+------------------+------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>User 1</strong>: Completed 6 courses with average rating 4.5 (qualifies as top performer)</li>
<li><strong>User 2</strong>: Completed 5 courses with average rating 4.4 (qualifies as top performer)</li>
<li><strong>User 3</strong>: Completed 5 courses but average rating is 2.8 (does not qualify)</li>
<li><strong>User 4</strong>: Completed only 3 courses (does not qualify)</li>
<li><strong>Course Pairs Among Top Performers</strong>:
<ul>
<li>User 1: Python Basics &rarr; SQL Fundamentals &rarr; JavaScript &rarr; React Basics &rarr; Node.js &rarr; Docker</li>
<li>User 2: Python Basics &rarr; React Basics &rarr; Node.js &rarr; Docker &rarr; AWS Fundamentals</li>
<li>Most common transitions: Node.js &rarr; Docker (2 times), React Basics &rarr; Node.js (2 times)</li>
</ul>
</li>
</ul>
<p>Results are ordered by transition_count in descending order, then by first_course in ascending order, and then by second_course in ascending order.</p>
</div>

View File

@@ -0,0 +1,103 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>The <strong>strength</strong> of the array is defined as the <strong>bitwise OR</strong> of all its elements.</p>
<p>A <strong><span data-keyword="subsequence-array-nonempty">subsequence</span></strong> is considered <strong>effective</strong> if removing that subsequence <strong>strictly decreases</strong> the strength of the remaining elements.</p>
<p>Return the number of <strong>effective subsequences</strong> in <code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>The bitwise OR of an empty array is 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 = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The Bitwise OR of the array is <code>1 OR 2 OR 3 = 3</code>.</li>
<li>Subsequences that are effective are:
<ul>
<li><code>[1, 3]</code>: The remaining element <code>[2]</code> has a Bitwise OR of 2.</li>
<li><code>[2, 3]</code>: The remaining element <code>[1]</code> has a Bitwise OR of 1.</li>
<li><code>[1, 2, 3]</code>: The remaining elements <code>[]</code> have a Bitwise OR of 0.</li>
</ul>
</li>
<li>Thus, the total number of effective subsequences 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">nums = [7,4,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The Bitwise OR of the array is <code>7 OR 4 OR 6 = 7</code>.</li>
<li>Subsequences that are effective are:
<ul>
<li><code>[7]</code>: The remaining elements <code>[4, 6]</code> have a Bitwise OR of 6.</li>
<li><code>[7, 4]</code>: The remaining element <code>[6]</code> has a Bitwise OR of 6.</li>
<li><code>[7, 6]</code>: The remaining element <code>[4]</code> has a Bitwise OR of 4.</li>
<li><code>[7, 4, 6]</code>: The remaining elements <code>[]</code> have a Bitwise OR of 0.</li>
</ul>
</li>
<li>Thus, the total number of effective subsequences 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">nums = [8,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The Bitwise OR of the array is <code>8 OR 8 = 8</code>.</li>
<li>Only the subsequence <code>[8, 8]</code> is effective since removing it leaves <code>[]</code> which has a Bitwise OR of 0.</li>
<li>Thus, the total number of effective subsequences is 1.</li>
</ul>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The Bitwise OR of the array is <code>2 OR 2 OR 1 = 3</code>.</li>
<li>Subsequences that are effective are:
<ul>
<li><code>[1]</code>: The remaining elements <code>[2, 2]</code> have a Bitwise OR of 2.</li>
<li><code>[2, 1]</code> (using <code>nums[0]</code>, <code>nums[2]</code>): The remaining element <code>[2]</code> has a Bitwise OR of 2.</li>
<li><code>[2, 1]</code> (using <code>nums[1]</code>, <code>nums[2]</code>): The remaining element <code>[2]</code> has a Bitwise OR of 2.</li>
<li><code>[2, 2]</code>: The remaining element <code>[1]</code> has a Bitwise OR of 1.</li>
<li><code>[2, 2, 1]</code>: The remaining elements <code>[]</code> have a Bitwise OR of 0.</li>
</ul>
</li>
<li>Thus, the total number of effective subsequences is 5.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,50 @@
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>.</p>
<p>You are allowed to repeatedly remove <strong>any <span data-keyword="substring-nonempty">substring</span></strong> where the number of <code>&#39;a&#39;</code> characters is equal to the number of <code>&#39;b&#39;</code> characters. After each removal, the remaining parts of the string are concatenated together without gaps.</p>
<p>Return an integer denoting the <strong>minimum possible length</strong> of the string after performing any number of such operations.</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 = <code>&quot;aabbab&quot;</code></span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>The substring <code>&quot;aabbab&quot;</code> has three <code>&#39;a&#39;</code> and three <code>&#39;b&#39;</code>. Since their counts are equal, we can remove the entire string directly. The minimum length is 0.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = <code>&quot;aaaa&quot;</code></span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>Every substring of <code>&quot;aaaa&quot;</code> contains only <code>&#39;a&#39;</code> characters. No substring can be removed as a result, so the minimum length remains 4.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = <code>&quot;aaabb&quot;</code></span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>First, remove the substring <code>&quot;ab&quot;</code>, leaving <code>&quot;aab&quot;</code>. Next, remove the new substring <code>&quot;ab&quot;</code>, leaving <code>&quot;a&quot;</code>. No further removals are possible, so the minimum length is 1.</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[i]</code> is either <code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>.</li>
</ul>

View File

@@ -0,0 +1,40 @@
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>.</p>
<p>An element in <code>nums</code> is said to be <strong>qualified</strong> if there exist <strong>at least</strong> <code>k</code> elements in the array that are <strong>strictly greater</strong> than it.</p>
<p>Return an integer denoting the total number of qualified elements in <code>nums</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,1,2], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The elements 1 and 2 each have at least <code>k = 1</code> element greater than themselves.<br />
No element is greater than 3. Therefore, the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Since all elements are equal to 5, no element is greater than the other. Therefore, the answer is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= k &lt; n</code></li>
</ul>

View File

@@ -0,0 +1,37 @@
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
<p>For every integer <code>x</code> from 1 to <code>n</code>, we write down the integer obtained by removing all zeros from the decimal representation of <code>x</code>.</p>
<p>Return an integer denoting the number of <strong>distinct</strong> integers written down.</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 = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>The integers we wrote down are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1. There are 9 distinct integers (1, 2, 3, 4, 5, 6, 7, 8, 9).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The integers we wrote down are 1, 2, 3. There are 3 distinct integers (1, 2, 3).</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>

View File

@@ -0,0 +1,82 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>A <strong><span data-keyword="subarray-nonempty">subarray</span></strong> of <code>nums</code> is called <strong>stable</strong> if it contains <strong>no inversions</strong>, i.e., there is no pair of indices <code>i &lt; j</code> such that <code>nums[i] &gt; nums[j]</code>.</p>
<p>You are also given a <strong>2D integer array</strong> <code>queries</code> of length <code>q</code>, where each <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents a query. For each query <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, compute the number of <strong>stable subarrays</strong> that lie entirely within the segment <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p>
<p>Return an integer array <code>ans</code> of length <code>q</code>, where <code>ans[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p><strong>Note</strong>:</p>
<ul>
<li>A single element subarray is considered stable.</li>
</ul>
<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,1,2], queries = [[0,1],[1,2],[0,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,4]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>queries[0] = [0, 1]</code>, the subarray is <code>[nums[0], nums[1]] = [3, 1]</code>.
<ul>
<li>The stable subarrays are <code>[3]</code> and <code>[1]</code>. The total number of stable subarrays is 2.</li>
</ul>
</li>
<li>For <code>queries[1] = [1, 2]</code>, the subarray is <code>[nums[1], nums[2]] = [1, 2]</code>.
<ul>
<li>The stable subarrays are <code>[1]</code>, <code>[2]</code>, and <code>[1, 2]</code>. The total number of stable subarrays is 3.</li>
</ul>
</li>
<li>For <code>queries[2] = [0, 2]</code>, the subarray is <code>[nums[0], nums[1], nums[2]] = [3, 1, 2]</code>.
<ul>
<li>The stable subarrays are <code>[3]</code>, <code>[1]</code>, <code>[2]</code>, and <code>[1, 2]</code>. The total number of stable subarrays is 4.</li>
</ul>
</li>
</ul>
<p>Thus, <code>ans = [2, 3, 4]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2], queries = [[0,1],[0,0]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>queries[0] = [0, 1]</code>, the subarray is <code>[nums[0], nums[1]] = [2, 2]</code>.
<ul>
<li>The stable subarrays are <code>[2]</code>, <code>[2]</code>, and <code>[2, 2]</code>. The total number of stable subarrays is 3.</li>
</ul>
</li>
<li>For <code>queries[1] = [0, 0]</code>, the subarray is <code>[nums[0]] = [2]</code>.
<ul>
<li>The stable subarray is <code>[2]</code>. The total number of stable subarrays is 1.</li>
</ul>
</li>
</ul>
<p>Thus, <code>ans = [3, 1]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= nums.length - 1</code></li>
</ul>

View File

@@ -0,0 +1,70 @@
<p>You are given two integers <code>num1</code> and <code>num2</code> representing an <strong>inclusive</strong> range <code>[num1, num2]</code>.</p>
<p>The <strong>waviness</strong> of a number is defined as the total count of its <strong>peaks</strong> and <strong>valleys</strong>:</p>
<ul>
<li>A digit is a <strong>peak</strong> if it is <strong>strictly greater</strong> than both of its immediate neighbors.</li>
<li>A digit is a <strong>valley</strong> if it is <strong>strictly less</strong> than both of its immediate neighbors.</li>
<li>The first and last digits of a number <strong>cannot</strong> be peaks or valleys.</li>
<li>Any number with fewer than 3 digits has a waviness of 0.</li>
</ul>
Return the total sum of waviness for all numbers in the range <code>[num1, num2]</code>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num1 = 120, num2 = 130</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
In the range <code>[120, 130]</code>:
<ul>
<li><code>120</code>: middle digit 2 is a peak, waviness = 1.</li>
<li><code>121</code>: middle digit 2 is a peak, waviness = 1.</li>
<li><code>130</code>: middle digit 3 is a peak, waviness = 1.</li>
<li>All other numbers in the range have a waviness of 0.</li>
</ul>
<p>Thus, total waviness is <code>1 + 1 + 1 = 3</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num1 = 198, num2 = 202</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
In the range <code>[198, 202]</code>:
<ul>
<li><code>198</code>: middle digit 9 is a peak, waviness = 1.</li>
<li><code>201</code>: middle digit 0 is a valley, waviness = 1.</li>
<li><code>202</code>: middle digit 0 is a valley, waviness = 1.</li>
<li>All other numbers in the range have a waviness of 0.</li>
</ul>
<p>Thus, total waviness is <code>1 + 1 + 1 = 3</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num1 = 4848, num2 = 4848</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Number <code>4848</code>: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num1 &lt;= num2 &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,72 @@
<p>You are given two integers <code>num1</code> and <code>num2</code> representing an <strong>inclusive</strong> range <code>[num1, num2]</code>.</p>
<p>The <strong>waviness</strong> of a number is defined as the total count of its <strong>peaks</strong> and <strong>valleys</strong>:</p>
<ul>
<li>A digit is a <strong>peak</strong> if it is <strong>strictly greater</strong> than both of its immediate neighbors.</li>
<li>A digit is a <strong>valley</strong> if it is <strong>strictly less</strong> than both of its immediate neighbors.</li>
<li>The first and last digits of a number <strong>cannot</strong> be peaks or valleys.</li>
<li>Any number with fewer than 3 digits has a waviness of 0.</li>
</ul>
Return the total sum of waviness for all numbers in the range <code>[num1, num2]</code>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num1 = 120, num2 = 130</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>In the range <code>[120, 130]</code>:</p>
<ul>
<li><code>120</code>: middle digit 2 is a peak, waviness = 1.</li>
<li><code>121</code>: middle digit 2 is a peak, waviness = 1.</li>
<li><code>130</code>: middle digit 3 is a peak, waviness = 1.</li>
<li>All other numbers in the range have a waviness of 0.</li>
</ul>
<p>Thus, total waviness is <code>1 + 1 + 1 = 3</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num1 = 198, num2 = 202</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>In the range <code>[198, 202]</code>:</p>
<ul>
<li><code>198</code>: middle digit 9 is a peak, waviness = 1.</li>
<li><code>201</code>: middle digit 0 is a valley, waviness = 1.</li>
<li><code>202</code>: middle digit 0 is a valley, waviness = 1.</li>
<li>All other numbers in the range have a waviness of 0.</li>
</ul>
<p>Thus, total waviness is <code>1 + 1 + 1 = 3</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num1 = 4848, num2 = 4848</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Number <code>4848</code>: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num1 &lt;= num2 &lt;= 10<sup>15</sup></code></li>
</ul>

View File

@@ -0,0 +1,46 @@
<p>You are given an integer <code>n</code>.</p>
<p>Form a new integer <code>x</code> by concatenating all the <strong>non-zero digits</strong> of <code>n</code> in their original order. If there are no <strong>non-zero</strong> digits, <code>x = 0</code>.</p>
<p>Let <code>sum</code> be the <strong>sum of digits</strong> in <code>x</code>.</p>
<p>Return an integer representing the value of <code>x * sum</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 = 10203004</span></p>
<p><strong>Output:</strong> <span class="example-io">12340</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The non-zero digits are 1, 2, 3, and 4. Thus, <code>x = 1234</code>.</li>
<li>The sum of digits is <code>sum = 1 + 2 + 3 + 4 = 10</code>.</li>
<li>Therefore, the answer is <code>x * sum = 1234 * 10 = 12340</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 = 1000</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The non-zero digit is 1, so <code>x = 1</code> and <code>sum = 1</code>.</li>
<li>Therefore, the answer is <code>x * sum = 1 * 1 = 1</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,109 @@
<p>You are given a string <code>s</code> of length <code>m</code> consisting of digits. You are also given a 2D integer array <code>queries</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p>
<p>For each <code>queries[i]</code>, extract the <strong><span data-keyword="substring-nonempty">substring</span></strong> <code>s[l<sub>i</sub>..r<sub>i</sub>]</code>. Then, perform the following:</p>
<ul>
<li>Form a new integer <code>x</code> by concatenating all the <strong>non-zero digits</strong> from the substring in their original order. If there are no non-zero digits, <code>x = 0</code>.</li>
<li>Let <code>sum</code> be the <strong>sum of digits</strong> in <code>x</code>. The answer is <code>x * sum</code>.</li>
</ul>
<p>Return an array of integers <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p>Since the answers may be very large, return them <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">s = &quot;10203004&quot;, queries = [[0,7],[1,3],[4,6]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[12340, 4, 9]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>s[0..7] = &quot;10203004&quot;</code>
<ul>
<li><code>x = 1234</code></li>
<li><code>sum = 1 + 2 + 3 + 4 = 10</code></li>
<li>Therefore, answer is <code>1234 * 10 = 12340</code>.</li>
</ul>
</li>
<li><code>s[1..3] = &quot;020&quot;</code>
<ul>
<li><code>x = 2</code></li>
<li><code>sum = 2</code></li>
<li>Therefore, the answer is <code>2 * 2 = 4</code>.</li>
</ul>
</li>
<li><code>s[4..6] = &quot;300&quot;</code>
<ul>
<li><code>x = 3</code></li>
<li><code>sum = 3</code></li>
<li>Therefore, the answer is <code>3 * 3 = 9</code>.</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">s = &quot;1000&quot;, queries = [[0,3],[1,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1, 0]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>s[0..3] = &quot;1000&quot;</code>
<ul>
<li><code>x = 1</code></li>
<li><code>sum = 1</code></li>
<li>Therefore, the answer is <code>1 * 1 = 1</code>.</li>
</ul>
</li>
<li><code>s[1..1] = &quot;0&quot;</code>
<ul>
<li><code>x = 0</code></li>
<li><code>sum = 0</code></li>
<li>Therefore, the answer is <code>0 * 0 = 0</code>.</li>
</ul>
</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;9876543210&quot;, queries = [[0,9]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[444444137]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>s[0..9] = &quot;9876543210&quot;</code>
<ul>
<li><code>x = 987654321</code></li>
<li><code>sum = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45</code></li>
<li>Therefore, the answer is <code>987654321 * 45 = 44444444445</code>.</li>
<li>We return <code>44444444445 modulo (10<sup>9</sup> + 7) = 444444137</code>.</li>
</ul>
</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m == s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consists of digits only.</li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li>
<li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; m</code></li>
</ul>

View File

@@ -0,0 +1,66 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>A <strong>mirror pair</strong> is a pair of indices <code>(i, j)</code> such that:</p>
<ul>
<li><code>0 &lt;= i &lt; j &lt; nums.length</code>, and</li>
<li><code>reverse(nums[i]) == nums[j]</code>, where <code>reverse(x)</code> denotes the integer formed by reversing the digits of <code>x</code>. Leading zeros are omitted after reversing, for example <code>reverse(120) = 21</code>.</li>
</ul>
<p>Return the <strong>minimum</strong> absolute distance between the indices of any mirror pair. The absolute distance between indices <code>i</code> and <code>j</code> is <code>abs(i - j)</code>.</p>
<p>If no mirror pair 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">nums = [12,21,45,33,54]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The mirror pairs are:</p>
<ul>
<li>(0, 1) since <code>reverse(nums[0]) = reverse(12) = 21 = nums[1]</code>, giving an absolute distance <code>abs(0 - 1) = 1</code>.</li>
<li>(2, 4) since <code>reverse(nums[2]) = reverse(45) = 54 = nums[4]</code>, giving an absolute distance <code>abs(2 - 4) = 2</code>.</li>
</ul>
<p>The minimum absolute distance among all pairs is 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [120,21]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is only one mirror pair (0, 1) since <code>reverse(nums[0]) = reverse(120) = 21 = nums[1]</code>.</p>
<p>The minimum absolute distance is 1.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [21,120]</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no mirror pairs in the array.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>