1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-25 06:48:57 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-10-19 23:12:56 +08:00
parent c93b54f0ca
commit 73ae78c4b4
59 changed files with 16301 additions and 9859 deletions

View File

@@ -0,0 +1,65 @@
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Return an integer denoting the <strong>sum</strong> of all elements in <code>nums</code> whose <strong><span data-keyword="frequency-array">frequency</span></strong> is divisible by <code>k</code>, or 0 if there are no such elements.</p>
<p><strong>Note:</strong> An element is included in the sum <strong>exactly</strong> as many times as it appears in the array if its total frequency is divisible by <code>k</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3,3,3,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The number 1 appears once (odd frequency).</li>
<li>The number 2 appears twice (even frequency).</li>
<li>The number 3 appears four times (even frequency).</li>
<li>The number 4 appears once (odd frequency).</li>
</ul>
<p>So, the total sum is <code>2 + 2 + 3 + 3 + 3 + 3 = 16</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,3,4,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no elements that appear an even number of times, so the total sum is 0.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,4,1,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The number 1 appears once.</li>
<li>The number 2 appears once.</li>
<li>The number 3 appears once.</li>
<li>The number 4 appears three times.</li>
</ul>
<p>So, the total sum is <code>4 + 4 + 4 = 12</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,59 @@
<p>You are given two strings <code>s</code> and <code>target</code>, both having length <code>n</code>, consisting of lowercase English letters.</p>
<p>Return the <strong>lexicographically smallest <span data-keyword="permutation-string">permutation</span></strong> of <code>s</code> that is <strong>strictly</strong> greater than <code>target</code>. If no permutation of <code>s</code> is lexicographically strictly greater than <code>target</code>, return an empty string.</p>
<p>A string <code>a</code> is <strong>lexicographically strictly greater </strong>than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears later in the alphabet than the corresponding letter in <code>b</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;abc&quot;, target = &quot;bba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;bca&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The permutations of <code>s</code> (in lexicographical order) are <code>&quot;abc&quot;</code>, <code>&quot;acb&quot;</code>, <code>&quot;bac&quot;</code>, <code>&quot;bca&quot;</code>, <code>&quot;cab&quot;</code>, and <code>&quot;cba&quot;</code>.</li>
<li>The lexicographically smallest permutation that is strictly greater than <code>target</code> is <code>&quot;bca&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">s = &quot;leet&quot;, target = &quot;code&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;eelt&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The permutations of <code>s</code> (in lexicographical order) are <code>&quot;eelt&quot;</code>, <code>&quot;eetl&quot;</code>, <code>&quot;elet&quot;</code>, <code>&quot;elte&quot;</code>, <code>&quot;etel&quot;</code>, <code>&quot;etle&quot;</code>, <code>&quot;leet&quot;</code>, <code>&quot;lete&quot;</code>, <code>&quot;ltee&quot;</code>, <code>&quot;teel&quot;</code>, <code>&quot;tele&quot;</code>, and <code>&quot;tlee&quot;</code>.</li>
<li>The lexicographically smallest permutation that is strictly greater than <code>target</code> is <code>&quot;eelt&quot;</code>.</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;baba&quot;, target = &quot;bbaa&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The permutations of <code>s</code> (in lexicographical order) are <code>&quot;aabb&quot;</code>, <code>&quot;abab&quot;</code>, <code>&quot;abba&quot;</code>, <code>&quot;baab&quot;</code>, <code>&quot;baba&quot;</code>, and <code>&quot;bbaa&quot;</code>.</li>
<li>None of them is lexicographically strictly greater than <code>target</code>. Therefore, the answer is <code>&quot;&quot;</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length == target.length &lt;= 300</code></li>
<li><code>s</code> and <code>target</code> consist of only lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,156 @@
<p>You are given an integer <code>n</code> and an undirected tree rooted at node 0 with <code>n</code> nodes numbered from 0 to <code>n - 1</code>. This is represented by a 2D array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates an undirected edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p>
<p>You are also given an integer array <code>nums</code>, where <code>nums[i]</code> is the positive integer assigned to node <code>i</code>.</p>
<p>Define a value <code>t<sub>i</sub></code> as the number of <strong>ancestors</strong> of node <code>i</code> such that the product <code>nums[i] * nums[ancestor]</code> is a <strong><span data-keyword="perfect-square">perfect square</span></strong>.</p>
<p>Return the sum of all <code>t<sub>i</sub></code> values for all nodes <code>i</code> in range <code>[1, n - 1]</code>.</p>
<p><strong>Note</strong>:</p>
<ul>
<li>In a rooted tree, the <strong>ancestors</strong> of node <code>i</code> are all nodes on the path from node <code>i</code> to the root node 0, <strong>excluding</strong> <code>i</code> itself.</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">n = 3, edges = [[0,1],[1,2]], nums = [2,8,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code><strong>i</strong></code></th>
<th style="border: 1px solid black;"><strong>Ancestors</strong></th>
<th style="border: 1px solid black;"><code><strong>nums[i] * nums[ancestor]</strong></code></th>
<th style="border: 1px solid black;">Square Check</th>
<th style="border: 1px solid black;"><code><strong>t<sub>i</sub></strong></code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0]</td>
<td style="border: 1px solid black;"><code>nums[1] * nums[0] = 8 * 2 = 16</code></td>
<td style="border: 1px solid black;">16 is a perfect square</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">[1, 0]</td>
<td style="border: 1px solid black;"><code>nums[2] * nums[1] = 2 * 8 = 16</code><br />
<code>nums[2] * nums[0] = 2 * 2 = 4</code></td>
<td style="border: 1px solid black;">Both 4 and 16 are perfect squares</td>
<td style="border: 1px solid black;">2</td>
</tr>
</tbody>
</table>
<p>Thus, the total number of valid ancestor pairs across all non-root nodes 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">n = 3, edges = [[0,1],[0,2]], nums = [1,2,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code><strong>i</strong></code></th>
<th style="border: 1px solid black;"><strong>Ancestors</strong></th>
<th style="border: 1px solid black;"><code><strong>nums[i] * nums[ancestor]</strong></code></th>
<th style="border: 1px solid black;">Square Check</th>
<th style="border: 1px solid black;"><code><strong>t<sub>i</sub></strong></code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0]</td>
<td style="border: 1px solid black;"><code>nums[1] * nums[0] = 2 * 1 = 2</code></td>
<td style="border: 1px solid black;">2 is <strong>not</strong> a perfect square</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;">[0]</td>
<td style="border: 1px solid black;"><code>nums[2] * nums[0] = 4 * 1 = 4</code></td>
<td style="border: 1px solid black;">4 is a perfect square</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p data-end="996" data-start="929">Thus, the total number of valid ancestor pairs across all non-root nodes is 1.</p>
</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,1],[0,2],[1,3]], nums = [1,2,9,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><strong>Ancestors</strong></th>
<th style="border: 1px solid black;"><code><strong>nums[i] * nums[ancestor]</strong></code></th>
<th style="border: 1px solid black;">Square Check</th>
<th style="border: 1px solid black;"><code><strong>t<sub>i</sub></strong></code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">[0]</td>
<td style="border: 1px solid black;"><code>nums[1] * nums[0] = 2 * 1 = 2</code></td>
<td style="border: 1px solid black;">2 is <strong>not</strong> a perfect square</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;">[0]</td>
<td style="border: 1px solid black;"><code>nums[2] * nums[0] = 9 * 1 = 9</code></td>
<td style="border: 1px solid black;">9 is a perfect square</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">[1, 0]</td>
<td style="border: 1px solid black;"><code>nums[3] * nums[1] = 4 * 2 = 8</code><br />
<code>nums[3] * nums[0] = 4 * 1 = 4</code></td>
<td style="border: 1px solid black;">Only 4 is a perfect square</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, the total number of valid ancestor pairs across all non-root nodes is <code>0 + 1 + 1 = 2</code>.</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>edges.length == n - 1</code></li>
<li><code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li>
<li><code>nums.length == n</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
</ul>

View File

@@ -0,0 +1,132 @@
<p>Table: <code>subscription_events</code></p>
<pre>
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| event_id | int |
| user_id | int |
| event_date | date |
| event_type | varchar |
| plan_name | varchar |
| monthly_amount | decimal |
+------------------+---------+
event_id is the unique identifier for this table.
event_type can be start, upgrade, downgrade, or cancel.
plan_name can be basic, standard, premium, or NULL (when event_type is cancel).
monthly_amount represents the monthly subscription cost after this event.
For cancel events, monthly_amount is 0.
</pre>
<p>Write a solution to <strong>Find Churn Risk Customers</strong> - users who show warning signs before churning. A user is considered <b>churn risk customer</b>&nbsp;if they meet ALL the following criteria:</p>
<ul>
<li>Currently have an <strong>active subscription</strong> (their last event is not cancel).</li>
<li>Have performed <strong>at least one</strong> downgrade in their subscription history.</li>
<li>Their <strong>current plan revenue</strong> is less than <code>50%</code> of their historical maximum plan revenue.</li>
<li>Have been a subscriber for <strong>at least</strong> <code>60</code> days.</li>
</ul>
<p>Return <em>the result table&nbsp;ordered by</em> <code>days_as_subscriber</code> <em>in <strong>descending</strong> order, then by</em> <code>user_id</code> <em>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>subscription_events table:</p>
<pre class="example-io">
+----------+---------+------------+------------+-----------+----------------+
| event_id | user_id | event_date | event_type | plan_name | monthly_amount |
+----------+---------+------------+------------+-----------+----------------+
| 1 | 501 | 2024-01-01 | start | premium | 29.99 |
| 2 | 501 | 2024-02-15 | downgrade | standard | 19.99 |
| 3 | 501 | 2024-03-20 | downgrade | basic | 9.99 |
| 4 | 502 | 2024-01-05 | start | standard | 19.99 |
| 5 | 502 | 2024-02-10 | upgrade | premium | 29.99 |
| 6 | 502 | 2024-03-15 | downgrade | basic | 9.99 |
| 7 | 503 | 2024-01-10 | start | basic | 9.99 |
| 8 | 503 | 2024-02-20 | upgrade | standard | 19.99 |
| 9 | 503 | 2024-03-25 | upgrade | premium | 29.99 |
| 10 | 504 | 2024-01-15 | start | premium | 29.99 |
| 11 | 504 | 2024-03-01 | downgrade | standard | 19.99 |
| 12 | 504 | 2024-03-30 | cancel | NULL | 0.00 |
| 13 | 505 | 2024-02-01 | start | basic | 9.99 |
| 14 | 505 | 2024-02-28 | upgrade | standard | 19.99 |
| 15 | 506 | 2024-01-20 | start | premium | 29.99 |
| 16 | 506 | 2024-03-10 | downgrade | basic | 9.99 |
+----------+---------+------------+------------+-----------+----------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+----------+--------------+------------------------+-----------------------+--------------------+
| user_id | current_plan | current_monthly_amount | max_historical_amount | days_as_subscriber |
+----------+--------------+------------------------+-----------------------+--------------------+
| 501 | basic | 9.99 | 29.99 | 79 |
| 502 | basic | 9.99 | 29.99 | 69 |
+----------+--------------+------------------------+-----------------------+--------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>User 501</strong>:
<ul>
<li>Currently active: Last event is downgrade&nbsp;to basic (not cancelled)&nbsp;</li>
<li>Has downgrades: Yes, 2 downgrades in history&nbsp;</li>
<li>Current revenue (9.99) vs max (29.99): 9.99/29.99 = 33.3% (less than 50%)&nbsp;</li>
<li>Days as subscriber: Jan 1 to Mar 20 = 79 days (at least 60)&nbsp;</li>
<li>Result: <strong>Churn Risk Customer</strong></li>
</ul>
</li>
<li><strong>User 502</strong>:
<ul>
<li>Currently active: Last event is downgrade&nbsp;to basic (not cancelled)&nbsp;</li>
<li>Has downgrades: Yes, 1 downgrade in history&nbsp;</li>
<li>Current revenue (9.99) vs max (29.99): 9.99/29.99 = 33.3% (less than 50%)&nbsp;</li>
<li>Days as subscriber: Jan 5 to Mar 15 = 70 days (at least 60)&nbsp;</li>
<li>Result: <strong>Churn Risk Customer</strong></li>
</ul>
</li>
<li><strong>User 503</strong>:
<ul>
<li>Currently active: Last event is upgrade&nbsp;to premium (not cancelled)&nbsp;</li>
<li>Has downgrades: No downgrades in history&nbsp;</li>
<li>Result: <strong>Not at-risk</strong> (no downgrade history)</li>
</ul>
</li>
<li><strong>User 504</strong>:
<ul>
<li>Currently active: Last event is cancel</li>
<li>Result: <strong>Not at-risk</strong> (subscription cancelled)</li>
</ul>
</li>
<li><strong>User 505</strong>:
<ul>
<li>Currently active: Last event is &#39;upgrade&#39; to standard (not cancelled)&nbsp;</li>
<li>Has downgrades: No downgrades in history&nbsp;</li>
<li>Result: <strong>Not at-risk</strong> (no downgrade history)</li>
</ul>
</li>
<li><strong>User 506</strong>:
<ul>
<li>Currently active: Last event is downgrade&nbsp;to basic (not cancelled)&nbsp;</li>
<li>Has downgrades: Yes, 1 downgrade in history&nbsp;</li>
<li>Current revenue (9.99) vs max (29.99): 9.99/29.99 = 33.3% (less than 50%)&nbsp;</li>
<li>Days as subscriber: Jan 20 to Mar 10 = 50 days (less than 60)&nbsp;</li>
<li>Result: <strong>Not at-risk</strong> (insufficient subscription duration)</li>
</ul>
</li>
</ul>
<p>Result table is ordered by days_as_subscriber DESC, then user_id ASC.</p>
<p><strong>Note:</strong> days_as_subscriber is calculated from the first event date to the last event date for each user.</p>
</div>

View File

@@ -0,0 +1,131 @@
<p>Table: <code>restaurant_orders</code></p>
<pre>
+------------------+----------+
| Column Name | Type |
+------------------+----------+
| order_id | int |
| customer_id | int |
| order_timestamp | datetime |
| order_amount | decimal |
| payment_method | varchar |
| order_rating | int |
+------------------+----------+
order_id is the unique identifier for this table.
payment_method can be cash, card, or app.
order_rating is between 1 and 5, where 5 is the best (NULL if not rated).
order_timestamp contains both date and time information.
</pre>
<p>Write a solution to find <strong>golden hour customers</strong>&nbsp;- customers who consistently order during peak hours and provide high satisfaction. A customer is a <strong>golden hour customer</strong> if they meet ALL the following criteria:</p>
<ul>
<li>Made <strong>at least</strong> <code>3</code> orders.</li>
<li><strong>At least</strong> <code>60%</code> of their orders are during <strong>peak hours&nbsp;</strong>(<code>11:00</code>-<code>14:00</code> or <code>18:00</code>-<code>21:00</code>).</li>
<li>Their <strong>average rating</strong> for rated orders is at least <code>4.0,</code> round it to<code> 2 </code>decimal places.</li>
<li>Have rated <strong>at least</strong> <code>50%</code> of their orders.</li>
</ul>
<p>Return <em>the result table ordered by</em> <code>average_rating</code> <em>in <strong>descending</strong> order, then by</em> <code>customer_id</code> <em>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>restaurant_orders table:</p>
<pre class="example-io">
+----------+-------------+---------------------+--------------+----------------+--------------+
| order_id | customer_id | order_timestamp | order_amount | payment_method | order_rating |
+----------+-------------+---------------------+--------------+----------------+--------------+
| 1 | 101 | 2024-03-01 12:30:00 | 25.50 | card | 5 |
| 2 | 101 | 2024-03-02 19:15:00 | 32.00 | app | 4 |
| 3 | 101 | 2024-03-03 13:45:00 | 28.75 | card | 5 |
| 4 | 101 | 2024-03-04 20:30:00 | 41.00 | app | NULL |
| 5 | 102 | 2024-03-01 11:30:00 | 18.50 | cash | 4 |
| 6 | 102 | 2024-03-02 12:00:00 | 22.00 | card | 3 |
| 7 | 102 | 2024-03-03 15:30:00 | 19.75 | cash | NULL |
| 8 | 103 | 2024-03-01 19:00:00 | 55.00 | app | 5 |
| 9 | 103 | 2024-03-02 20:45:00 | 48.50 | app | 4 |
| 10 | 103 | 2024-03-03 18:30:00 | 62.00 | card | 5 |
| 11 | 104 | 2024-03-01 10:00:00 | 15.00 | cash | 3 |
| 12 | 104 | 2024-03-02 09:30:00 | 18.00 | cash | 2 |
| 13 | 104 | 2024-03-03 16:00:00 | 20.00 | card | 3 |
| 14 | 105 | 2024-03-01 12:15:00 | 30.00 | app | 4 |
| 15 | 105 | 2024-03-02 13:00:00 | 35.50 | app | 5 |
| 16 | 105 | 2024-03-03 11:45:00 | 28.00 | card | 4 |
+----------+-------------+---------------------+--------------+----------------+--------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-------------+--------------+----------------------+----------------+
| customer_id | total_orders | peak_hour_percentage | average_rating |
+-------------+--------------+----------------------+----------------+
| 103 | 3 | 100 | 4.67 |
| 101 | 4 | 75 | 4.67 |
| 105 | 3 | 100 | 4.33 |
+-------------+--------------+----------------------+----------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Customer 101</strong>:
<ul>
<li>Total orders: 4 (at least 3)&nbsp;</li>
<li>Peak hour orders: 3 out of 4 (12:30, 19:15, 13:45, and 20:30 are in peak hours)</li>
<li>Peak hour percentage: 3/4 = 75% (at least 60%)&nbsp;</li>
<li>Rated orders: 3 out of 4 (75% rating completion)&nbsp;</li>
<li>Average rating: (5+4+5)/3 = 4.67 (at least 4.0)&nbsp;</li>
<li>Result: <strong>Golden hour customer</strong></li>
</ul>
</li>
<li><strong>Customer 102</strong>:
<ul>
<li>Total orders: 3 (at least 3)&nbsp;</li>
<li>Peak hour orders: 2 out of 3 (11:30, 12:00 are in peak hours; 15:30 is not)</li>
<li>Peak hour percentage: 2/3 = 66.67% (at least 60%)&nbsp;</li>
<li>Rated orders: 2 out of 3 (66.67% rating completion)&nbsp;</li>
<li>Average rating: (4+3)/2 = 3.5 (less than 4.0)&nbsp;</li>
<li>Result: <strong>Not a golden hour customer</strong> (average rating too low)</li>
</ul>
</li>
<li><strong>Customer 103</strong>:
<ul>
<li>Total orders: 3 (at least 3)&nbsp;</li>
<li>Peak hour orders: 3 out of 3 (19:00, 20:45, 18:30 all in evening peak)</li>
<li>Peak hour percentage: 3/3 = 100% (at least 60%)&nbsp;</li>
<li>Rated orders: 3 out of 3 (100% rating completion)&nbsp;</li>
<li>Average rating: (5+4+5)/3 = 4.67 (at least 4.0)&nbsp;</li>
<li>Result: <strong>Golden hour customer</strong></li>
</ul>
</li>
<li><strong>Customer 104</strong>:
<ul>
<li>Total orders: 3 (at least 3)&nbsp;</li>
<li>Peak hour orders: 0 out of 3 (10:00, 09:30, 16:00 all outside peak hours)</li>
<li>Peak hour percentage: 0/3 = 0% (less than 60%)&nbsp;</li>
<li>Result: <strong>Not a golden hour customer</strong> (insufficient peak hour orders)</li>
</ul>
</li>
<li><strong>Customer 105</strong>:
<ul>
<li>Total orders: 3 (at least 3)&nbsp;</li>
<li>Peak hour orders: 3 out of 3 (12:15, 13:00, 11:45 all in lunch peak)</li>
<li>Peak hour percentage: 3/3 = 100% (at least 60%)&nbsp;</li>
<li>Rated orders: 3 out of 3 (100% rating completion)&nbsp;</li>
<li>Average rating: (4+5+4)/3 = 4.33 (at least 4.0)&nbsp;</li>
<li>Result: <strong>Golden hour customer</strong></li>
</ul>
</li>
</ul>
<p>The results table is ordered by average_rating DESC, then customer_id DESC.</p>
</div>

View File

@@ -0,0 +1,36 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>Return the length of the <strong>longest <span data-keyword="subsequence-array-nonempty">subsequence</span></strong> in <code>nums</code> whose bitwise <strong>XOR</strong> is <strong>non-zero</strong>. If no such <strong>subsequence</strong> 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 = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One longest subsequence is <code>[2, 3]</code>. The bitwise XOR is computed as <code>2 XOR 3 = 1</code>, which is non-zero.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest subsequence is <code>[2, 3, 4]</code>. The bitwise XOR is computed as <code>2 XOR 3 XOR 4 = 5</code>, which is non-zero.</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,65 @@
<p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of the <code><font>i<sup>th</sup></font></code> point on the Cartesian plane.</p>
<p>The <strong>Manhattan distance</strong> between two points <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> and <code>points[j] = [x<sub>j</sub>, y<sub>j</sub>]</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.</p>
<p>Split the <code>n</code> points into <strong>exactly two non-empty</strong> groups. The <strong>partition factor</strong> of a split is the <strong>minimum</strong> Manhattan distance among all unordered pairs of points that lie in the same group.</p>
<p>Return the <strong>maximum</strong> possible <strong>partition factor</strong> over all valid splits.</p>
<p>Note: A group of size 1 contributes no intra-group pairs. When <code>n = 2</code> (both groups size 1), there are no intra-group pairs, so define the partition factor as 0.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span>points = [[0,0],[0,2],[2,0],[2,2]]</span></p>
<p><strong>Output:</strong> <span>4</span></p>
<p><strong>Explanation:</strong></p>
<p>We split the points into two groups: <code>{[0, 0], [2, 2]}</code> and <code>{[0, 2], [2, 0]}</code>.</p>
<ul>
<li>
<p>In the first group, the only pair has Manhattan distance <code>|0 - 2| + |0 - 2| = 4</code>.</p>
</li>
<li>
<p>In the second group, the only pair also has Manhattan distance <code>|0 - 2| + |2 - 0| = 4</code>.</p>
</li>
</ul>
<p>The partition factor of this split is <code>min(4, 4) = 4</code>, which is maximal.</p>
</div>
<p><strong>Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span>points = [[0,0],[0,1],[10,0]]</span></p>
<p><strong>Output:</strong> <span>11</span></p>
<p><strong>Explanation:</strong></p>
<p>We split the points into two groups: <code>{[0, 1], [10, 0]}</code> and <code>{[0, 0]}</code>.</p>
<ul>
<li>
<p>In the first group, the only pair has Manhattan distance <code>|0 - 10| + |1 - 0| = 11</code>.</p>
</li>
<li>
<p>The second group is a singleton, so it contributes no pairs.</p>
</li>
</ul>
<p>The partition factor of this split is <code>11</code>, which is maximal.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= points.length &lt;= 500</code></li>
<li><code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code></li>
<li><code>-10<sup>8</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>8</sup></code></li>
</ul>

View File

@@ -0,0 +1,59 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>A <strong><span data-keyword="subarray-nonempty">subarray</span></strong> is called <strong>balanced</strong> if the number of <strong>distinct even</strong> numbers in the subarray is equal to the number of <strong>distinct odd</strong> numbers.</p>
<p>Return the length of the <strong>longest</strong> balanced subarray.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The longest balanced subarray is <code>[2, 5, 4, 3]</code>.</li>
<li>It has 2 distinct even numbers <code>[2, 4]</code> and 2 distinct odd numbers <code>[5, 3]</code>. Thus, the answer 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">nums = [3,2,2,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The longest balanced subarray is <code>[3, 2, 2, 5, 4]</code>.</li>
<li>It has 2 distinct even numbers <code>[2, 4]</code> and 2 distinct odd numbers <code>[3, 5]</code>. Thus, the answer is 5.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The longest balanced subarray is <code>[2, 3, 2]</code>.</li>
<li>It has 1 distinct even number <code>[2]</code> and 1 distinct odd number <code>[3]</code>. Thus, the answer is 3.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1500</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,59 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>A <strong><span data-keyword="subarray-nonempty">subarray</span></strong> is called <strong>balanced</strong> if the number of <strong>distinct even</strong> numbers in the subarray is equal to the number of <strong>distinct odd</strong> numbers.</p>
<p>Return the length of the <strong>longest</strong> balanced subarray.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The longest balanced subarray is <code>[2, 5, 4, 3]</code>.</li>
<li>It has 2 distinct even numbers <code>[2, 4]</code> and 2 distinct odd numbers <code>[5, 3]</code>. Thus, the answer 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">nums = [3,2,2,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The longest balanced subarray is <code>[3, 2, 2, 5, 4]</code>.</li>
<li>It has 2 distinct even numbers <code>[2, 4]</code> and 2 distinct odd numbers <code>[3, 5]</code>. Thus, the answer is 5.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The longest balanced subarray is <code>[2, 3, 2]</code>.</li>
<li>It has 1 distinct even number <code>[2]</code> and 1 distinct odd number <code>[3]</code>. Thus, the answer is 3.</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>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,58 @@
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>A <strong>Fibonacci</strong> array is a contiguous sequence whose third and subsequent terms each equal the sum of the two preceding terms.</p>
<p>Return the length of the longest <strong>Fibonacci</strong> <strong><span data-keyword="subarray-nonempty">subarray</span></strong> in <code>nums</code>.</p>
<p><strong>Note:</strong> Subarrays of length 1 or 2 are always <strong>Fibonacci</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">nums = [1,1,1,1,2,3,5,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest Fibonacci subarray is <code>nums[2..6] = [1, 1, 2, 3, 5]</code>.</p>
<p><code>[1, 1, 2, 3, 5]</code> is Fibonacci because <code>1 + 1 = 2</code>, <code>1 + 2 = 3</code>, and <code>2 + 3 = 5</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,2,7,9,16]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest Fibonacci subarray is <code>nums[0..4] = [5, 2, 7, 9, 16]</code>.</p>
<p><code>[5, 2, 7, 9, 16]</code> is Fibonacci because <code>5 + 2 = 7</code>, <code>2 + 7 = 9</code>, and <code>7 + 9 = 16</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1000000000,1000000000,1000000000]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest Fibonacci subarray is <code>nums[1..2] = [1000000000, 1000000000]</code>.</p>
<p><code>[1000000000, 1000000000]</code> is Fibonacci because its length is 2.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,50 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters.</p>
<p>A <strong><span data-keyword="substring-nonempty">substring</span></strong> of <code>s</code> is called <strong>balanced</strong> if all <strong>distinct</strong> characters in the <strong>substring</strong> appear the <strong>same</strong> number of times.</p>
<p>Return the <strong>length</strong> of the <strong>longest balanced substring</strong> of <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;abbac&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest balanced substring is <code>&quot;abba&quot;</code> because both distinct characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code> each appear exactly 2 times.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;zzabccy&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest balanced substring is <code>&quot;zabc&quot;</code> because the distinct characters <code>&#39;z&#39;</code>, <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code> each appear exactly 1 time.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><strong></strong>One of the longest balanced substrings is <code>&quot;ab&quot;</code> because both distinct characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code> each appear exactly 1 time. Another longest balanced substring is <code>&quot;ba&quot;</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
<li><code>s</code> consists of lowercase English letters.</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>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p>
<p>A <strong><span data-keyword="substring-nonempty">substring</span></strong> of <code>s</code> is called <strong>balanced</strong> if all <strong>distinct</strong> characters in the <strong>substring</strong> appear the <strong>same</strong> number of times.</p>
<p>Return the <strong>length of the longest balanced substring</strong> of <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;abbac&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest balanced substring is <code>&quot;abba&quot;</code> because both distinct characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code> each appear exactly 2 times.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aabcc&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest balanced substring is <code>&quot;abc&quot;</code> because all distinct characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code> and <code>&#39;c&#39;</code> each appear exactly 1 time.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>One of the longest balanced substrings is <code>&quot;ab&quot;</code> because both distinct characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code> each appear exactly 1 time. Another longest balanced substring is <code>&quot;ba&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 the characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</li>
</ul>

View File

@@ -0,0 +1,47 @@
<p>You are given a string <code>s</code> consisting of lowercase English letters.</p>
<p>The <strong>score</strong> of a string is the sum of the positions of its characters in the alphabet, where <code>&#39;a&#39; = 1</code>, <code>&#39;b&#39; = 2</code>, ..., <code>&#39;z&#39; = 26</code>.</p>
<p>Determine whether there exists an index <code>i</code> such that the string can be split into two <strong>non-empty</strong> <strong><strong><span data-keyword="substring-nonempty">substrings</span></strong></strong> <code>s[0..i]</code> and <code>s[(i + 1)..(n - 1)]</code> that have <strong>equal</strong> scores.</p>
<p>Return <code>true</code> if such a split exists, otherwise return <code>false</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;adcb&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Split at index <code>i = 1</code>:</p>
<ul>
<li>Left substring = <code>s[0..1] = &quot;ad&quot;</code> with <code>score = 1 + 4 = 5</code></li>
<li>Right substring = <code>s[2..3] = &quot;cb&quot;</code> with <code>score = 3 + 2 = 5</code></li>
</ul>
<p>Both substrings have equal scores, so the output is <code>true</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;bace&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p><strong></strong>No split produces equal scores, so the output is <code>false</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,136 @@
<p>You are given a string <code>s</code> consisting of <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>, and an integer <code>k</code>.</p>
<p>A <strong>string</strong> is <strong>k-balanced</strong> if it is <strong>exactly</strong> <code>k</code> <strong>consecutive</strong> <code>&#39;(&#39;</code> followed by <code>k</code> <strong>consecutive</strong> <code>&#39;)&#39;</code>, i.e., <code>&#39;(&#39; * k + &#39;)&#39; * k</code>.</p>
<p>For example, if <code>k = 3</code>, k-balanced is <code>&quot;((()))&quot;</code>.</p>
<p>You must <strong>repeatedly</strong> remove all <strong>non-overlapping k-balanced <span data-keyword="substring-nonempty">substrings</span></strong> from <code>s</code>, and then join the remaining parts. Continue this process until no k-balanced <strong>substring</strong> exists.</p>
<p>Return the final string after all possible removals.</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;(())&quot;, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>k-balanced substring is <code>&quot;()&quot;</code></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Step</th>
<th style="border: 1px solid black;">Current <code>s</code></th>
<th style="border: 1px solid black;"><code>k-balanced</code></th>
<th style="border: 1px solid black;">Result <code>s</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>(())</code></td>
<td style="border: 1px solid black;"><code>(<s><strong>()</strong></s>)</code></td>
<td style="border: 1px solid black;"><code>()</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>()</code></td>
<td style="border: 1px solid black;"><s><strong><code>()</code></strong></s></td>
<td style="border: 1px solid black;">Empty</td>
</tr>
</tbody>
</table>
<p>Thus, the final string is <code>&quot;&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;(()(&quot;, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;((&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>k-balanced substring is <code>&quot;()&quot;</code></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Step</th>
<th style="border: 1px solid black;">Current <code>s</code></th>
<th style="border: 1px solid black;"><code>k-balanced</code></th>
<th style="border: 1px solid black;">Result <code>s</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>(()(</code></td>
<td style="border: 1px solid black;"><code>(<s><strong>()</strong></s>(</code></td>
<td style="border: 1px solid black;"><code>((</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>((</code></td>
<td style="border: 1px solid black;">-</td>
<td style="border: 1px solid black;"><code>((</code></td>
</tr>
</tbody>
</table>
<p>Thus, the final string is <code>&quot;((&quot;</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;((()))()()()&quot;, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;()()()&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>k-balanced substring is <code>&quot;((()))&quot;</code></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Step</th>
<th style="border: 1px solid black;">Current <code>s</code></th>
<th style="border: 1px solid black;"><code>k-balanced</code></th>
<th style="border: 1px solid black;">Result <code>s</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>((()))()()()</code></td>
<td style="border: 1px solid black;"><code><s><strong>((()))</strong></s>()()()</code></td>
<td style="border: 1px solid black;"><code>()()()</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>()()()</code></td>
<td style="border: 1px solid black;">-</td>
<td style="border: 1px solid black;"><code>()()()</code></td>
</tr>
</tbody>
</table>
<p>Thus, the final string is <code>&quot;()()()&quot;</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>.</li>
<li><code>1 &lt;= k &lt;= s.length / 2</code></li>
</ul>

View File

@@ -0,0 +1,54 @@
<p>A <strong>no-zero</strong> integer is a <strong>positive</strong> integer that <strong>does not contain the digit</strong> 0 in its decimal representation.</p>
<p>Given an integer <code>n</code>, count the number of pairs <code>(a, b)</code> where:</p>
<ul>
<li><code>a</code> and <code>b</code> are <strong>no-zero</strong> integers.</li>
<li><code>a + b = n</code></li>
</ul>
<p>Return an integer denoting the number of such pairs.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only pair is <code>(1, 1)</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The pairs are <code>(1, 2)</code> and <code>(2, 1)</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 11</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p>The pairs are <code>(2, 9)</code>, <code>(3, 8)</code>, <code>(4, 7)</code>, <code>(5, 6)</code>, <code>(6, 5)</code>, <code>(7, 4)</code>, <code>(8, 3)</code>, and <code>(9, 2)</code>. Note that <code>(1, 10)</code> and <code>(10, 1)</code> do not satisfy the conditions because 10 contains 0 in its decimal representation.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>

View File

@@ -0,0 +1,37 @@
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the <strong>smallest positive multiple</strong> of <code>k</code> that is <strong>missing</strong> from <code>nums</code>.</p>
<p>A <strong>multiple</strong> of <code>k</code> is any positive integer divisible by <code>k</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [8,2,3,4,6], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>The multiples of <code>k = 2</code> are 2, 4, 6, 8, 10, 12... and the smallest multiple missing from <code>nums</code> is 10.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,7,10,15], k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The multiples of <code>k = 5</code> are 5, 10, 15, 20... and the smallest multiple missing from <code>nums</code> is 5.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,46 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>The <strong>alternating sum</strong> of <code>nums</code> is the value obtained by <strong>adding</strong> elements at even indices and <strong>subtracting</strong> elements at odd indices. That is, <code>nums[0] - nums[1] + nums[2] - nums[3]...</code></p>
<p>Return an integer denoting the alternating sum of <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 = [1,3,5,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">-4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Elements at even indices are <code>nums[0] = 1</code> and <code>nums[2] = 5</code> because 0 and 2 are even numbers.</li>
<li>Elements at odd indices are <code>nums[1] = 3</code> and <code>nums[3] = 7</code> because 1 and 3 are odd numbers.</li>
<li>The alternating sum is <code>nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [100]</span></p>
<p><strong>Output:</strong> <span class="example-io">100</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The only element at even indices is <code>nums[0] = 100</code> because 0 is an even number.</li>
<li>There are no elements on odd indices.</li>
<li>The alternating sum is <code>nums[0] = 100</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,49 @@
<p>Alice frequently takes exams and wants to track her scores and calculate the total scores over specific time periods.</p>
<p>Implement the <code>ExamTracker</code> class:</p>
<ul>
<li><code>ExamTracker()</code>: Initializes the <code>ExamTracker</code> object.</li>
<li><code>void record(int time, int score)</code>: Alice takes a new exam at time <code>time</code> and achieves the score <code>score</code>.</li>
<li><code>long long totalScore(int startTime, int endTime)</code>: Returns an integer that represents the <strong>total</strong> score of all exams taken by Alice between <code>startTime</code> and <code>endTime</code> (inclusive). If there are no recorded exams taken by Alice within the specified time interval, return 0.</li>
</ul>
<p>It is guaranteed that the function calls are made in chronological order. That is,</p>
<ul>
<li>Calls to <code>record()</code> will be made with <strong>strictly increasing</strong> <code>time</code>.</li>
<li>Alice will never ask for total scores that require information from the future. That is, if the latest <code>record()</code> is called with <code>time = t</code>, then <code>totalScore()</code> will always be called with <code>startTime &lt;= endTime &lt;= t</code>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong><br />
<span class="example-io">[&quot;ExamTracker&quot;, &quot;record&quot;, &quot;totalScore&quot;, &quot;record&quot;, &quot;totalScore&quot;, &quot;totalScore&quot;, &quot;totalScore&quot;, &quot;totalScore&quot;]<br />
[[], [1, 98], [1, 1], [5, 99], [1, 3], [1, 5], [3, 4], [2, 5]]</span></p>
<p><strong>Output:</strong><br />
<span class="example-io">[null, null, 98, null, 98, 197, 0, 99] </span></p>
<p><strong>Explanation</strong></p>
ExamTracker examTracker = new ExamTracker();<br />
examTracker.record(1, 98); // Alice takes a new exam at time 1, scoring 98.<br />
examTracker.totalScore(1, 1); // Between time 1 and time 1, Alice took 1 exam at time 1, scoring 98. The total score is 98.<br />
examTracker.record(5, 99); // Alice takes a new exam at time 5, scoring 99.<br />
examTracker.totalScore(1, 3); // Between time 1 and time 3, Alice took 1 exam at time 1, scoring 98. The total score is 98.<br />
examTracker.totalScore(1, 5); // Between time 1 and time 5, Alice took 2 exams at time 1 and 5, scoring 98 and 99. The total score is <code>98 + 99 = 197</code>.<br />
examTracker.totalScore(3, 4); // Alice did not take any exam between time 3 and time 4. Therefore, the answer is 0.<br />
examTracker.totalScore(2, 5); // Between time 2 and time 5, Alice took 1 exam at time 5, scoring 99. The total score is 99.</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= time &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= score &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= startTime &lt;= endTime &lt;= t</code>, where <code>t</code> is the value of <code>time</code> from the most recent call of <code>record()</code>.</li>
<li>Calls of <code>record()</code> will be made with <strong>strictly increasing</strong> <code>time</code>.</li>
<li>After <code>ExamTracker()</code>, the first function call will always be <code>record()</code>.</li>
<li>At most <code>10<sup>5</sup></code> calls will be made in total to <code>record()</code> and <code>totalScore()</code>.</li>
</ul>