1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-06 07:51:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-03-14 03:44:12 +08:00
parent 1627cd9a3b
commit 0054d66982
60 changed files with 16341 additions and 9984 deletions

View File

@@ -0,0 +1,130 @@
<p>Table: <code>Samples</code></p>
<pre>
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| sample_id | int |
| dna_sequence | varchar |
| species | varchar |
+----------------+---------+
sample_id is the unique key for this table.
Each row contains a DNA sequence represented as a string of characters (A, T, G, C) and the species it was collected from.
</pre>
<p>Biologists are studying basic patterns in DNA sequences. Write a solution to identify <code>sample_id</code> with the following patterns:</p>
<ul>
<li>Sequences that <strong>start</strong> with <strong>ATG</strong>&nbsp;(a common <strong>start codon</strong>)</li>
<li>Sequences that <strong>end</strong> with either <strong>TAA</strong>, <strong>TAG</strong>, or <strong>TGA</strong>&nbsp;(<strong>stop codons</strong>)</li>
<li>Sequences containing the motif <strong>ATAT</strong>&nbsp;(a simple repeated pattern)</li>
<li>Sequences that have <strong>at least</strong> <code>3</code> <strong>consecutive</strong> <strong>G</strong>&nbsp;(like <strong>GGG</strong>&nbsp;or <strong>GGGG</strong>)</li>
</ul>
<p>Return <em>the result table ordered by&nbsp;</em><em>sample_id 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>Samples table:</p>
<pre class="example-io">
+-----------+------------------+-----------+
| sample_id | dna_sequence | species |
+-----------+------------------+-----------+
| 1 | ATGCTAGCTAGCTAA | Human |
| 2 | GGGTCAATCATC | Human |
| 3 | ATATATCGTAGCTA | Human |
| 4 | ATGGGGTCATCATAA | Mouse |
| 5 | TCAGTCAGTCAG | Mouse |
| 6 | ATATCGCGCTAG | Zebrafish |
| 7 | CGTATGCGTCGTA | Zebrafish |
+-----------+------------------+-----------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-----------+------------------+-------------+-------------+------------+------------+------------+
| sample_id | dna_sequence | species | has_start | has_stop | has_atat | has_ggg |
+-----------+------------------+-------------+-------------+------------+------------+------------+
| 1 | ATGCTAGCTAGCTAA | Human | 1 | 1 | 0 | 0 |
| 2 | GGGTCAATCATC | Human | 0 | 0 | 0 | 1 |
| 3 | ATATATCGTAGCTA | Human | 0 | 0 | 1 | 0 |
| 4 | ATGGGGTCATCATAA | Mouse | 1 | 1 | 0 | 1 |
| 5 | TCAGTCAGTCAG | Mouse | 0 | 0 | 0 | 0 |
| 6 | ATATCGCGCTAG | Zebrafish | 0 | 1 | 1 | 0 |
| 7 | CGTATGCGTCGTA | Zebrafish | 0 | 0 | 0 | 0 |
+-----------+------------------+-------------+-------------+------------+------------+------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sample 1 (ATGCTAGCTAGCTAA):
<ul>
<li>Starts with ATG&nbsp;(has_start = 1)</li>
<li>Ends with TAA&nbsp;(has_stop = 1)</li>
<li>Does not contain ATAT&nbsp;(has_atat = 0)</li>
<li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li>
</ul>
</li>
<li>Sample 2 (GGGTCAATCATC):
<ul>
<li>Does not start with ATG&nbsp;(has_start = 0)</li>
<li>Does not end with TAA, TAG, or TGA&nbsp;(has_stop = 0)</li>
<li>Does not contain ATAT&nbsp;(has_atat = 0)</li>
<li>Contains GGG&nbsp;(has_ggg = 1)</li>
</ul>
</li>
<li>Sample 3 (ATATATCGTAGCTA):
<ul>
<li>Does not start with ATG&nbsp;(has_start = 0)</li>
<li>Does not end with TAA, TAG, or TGA&nbsp;(has_stop = 0)</li>
<li>Contains ATAT&nbsp;(has_atat = 1)</li>
<li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li>
</ul>
</li>
<li>Sample 4 (ATGGGGTCATCATAA):
<ul>
<li>Starts with ATG&nbsp;(has_start = 1)</li>
<li>Ends with TAA&nbsp;(has_stop = 1)</li>
<li>Does not contain ATAT&nbsp;(has_atat = 0)</li>
<li>Contains GGGG&nbsp;(has_ggg = 1)</li>
</ul>
</li>
<li>Sample 5 (TCAGTCAGTCAG):
<ul>
<li>Does not match any patterns (all fields = 0)</li>
</ul>
</li>
<li>Sample 6 (ATATCGCGCTAG):
<ul>
<li>Does not start with ATG&nbsp;(has_start = 0)</li>
<li>Ends with TAG&nbsp;(has_stop = 1)</li>
<li>Starts with ATAT&nbsp;(has_atat = 1)</li>
<li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li>
</ul>
</li>
<li>Sample 7 (CGTATGCGTCGTA):
<ul>
<li>Does not start with ATG&nbsp;(has_start = 0)</li>
<li>Does not end with TAA, &quot;TAG&quot;, or &quot;TGA&quot; (has_stop = 0)</li>
<li>Does not contain ATAT&nbsp;(has_atat = 0)</li>
<li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li>
</ul>
</li>
</ul>
<p><strong>Note:</strong></p>
<ul>
<li>The result is ordered by sample_id in ascending order</li>
<li>For each pattern, 1 indicates the pattern is present and 0 indicates it is not present</li>
</ul>
</div>

View File

@@ -0,0 +1,75 @@
<p>Given two integers, <code>n</code> and <code>k</code>, an <strong>alternating permutation</strong> is a permutation of the first <code>n</code> positive integers such that no <strong>two</strong> adjacent elements are both odd or both even.</p>
<p>Return the <strong>k-th</strong> <strong>alternating permutation</strong> sorted in <em>lexicographical order</em>. If there are fewer than <code>k</code> valid <strong>alternating permutations</strong>, return an empty list.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,4,1,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The lexicographically-sorted alternating permutations of <code>[1, 2, 3, 4]</code> are:</p>
<ol>
<li><code>[1, 2, 3, 4]</code></li>
<li><code>[1, 4, 3, 2]</code></li>
<li><code>[2, 1, 4, 3]</code></li>
<li><code>[2, 3, 4, 1]</code></li>
<li><code>[3, 2, 1, 4]</code></li>
<li><code>[3, 4, 1, 2]</code> &larr; 6th permutation</li>
<li><code>[4, 1, 2, 3]</code></li>
<li><code>[4, 3, 2, 1]</code></li>
</ol>
<p>Since <code>k = 6</code>, we return <code>[3, 4, 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 = 3, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p>The lexicographically-sorted alternating permutations of <code>[1, 2, 3]</code> are:</p>
<ol>
<li><code>[1, 2, 3]</code></li>
<li><code>[3, 2, 1]</code> &larr; 2nd permutation</li>
</ol>
<p>Since <code>k = 2</code>, we return <code>[3, 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 = 2, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
<p><strong>Explanation:</strong></p>
<p>The lexicographically-sorted alternating permutations of <code>[1, 2]</code> are:</p>
<ol>
<li><code>[1, 2]</code></li>
<li><code>[2, 1]</code></li>
</ol>
<p>There are only 2 alternating permutations, but <code>k = 3</code>, which is out of range. Thus, we return an empty list <code>[]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= 10<sup>15</sup></code></li>
</ul>

View File

@@ -0,0 +1,49 @@
<p>You are given an integer <code>n</code> which represents an array <code>nums</code> containing the numbers from 1 to <code>n</code> in order. Additionally, you are given a 2D array <code>conflictingPairs</code>, where <code>conflictingPairs[i] = [a, b]</code> indicates that <code>a</code> and <code>b</code> form a conflicting pair.</p>
<p>Remove <strong>exactly</strong> one element from <code>conflictingPairs</code>. Afterward, count the number of <span data-keyword="subarray-nonempty">non-empty subarrays</span> of <code>nums</code> which do not contain both <code>a</code> and <code>b</code> for any remaining conflicting pair <code>[a, b]</code>.</p>
<p>Return the <strong>maximum</strong> number of subarrays possible after removing <strong>exactly</strong> one conflicting pair.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, conflictingPairs = [[2,3],[1,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>[2, 3]</code> from <code>conflictingPairs</code>. Now, <code>conflictingPairs = [[1, 4]]</code>.</li>
<li>There are 9 subarrays in <code>nums</code> where <code>[1, 4]</code> do not appear together. They are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[4]</code>, <code>[1, 2]</code>, <code>[2, 3]</code>, <code>[3, 4]</code>, <code>[1, 2, 3]</code> and <code>[2, 3, 4]</code>.</li>
<li>The maximum number of subarrays we can achieve after removing one element from <code>conflictingPairs</code> is 9.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>[1, 2]</code> from <code>conflictingPairs</code>. Now, <code>conflictingPairs = [[2, 5], [3, 5]]</code>.</li>
<li>There are 12 subarrays in <code>nums</code> where <code>[2, 5]</code> and <code>[3, 5]</code> do not appear together.</li>
<li>The maximum number of subarrays we can achieve after removing one element from <code>conflictingPairs</code> is 12.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= conflictingPairs.length &lt;= 2 * n</code></li>
<li><code>conflictingPairs[i].length == 2</code></li>
<li><code>1 &lt;= conflictingPairs[i][j] &lt;= n</code></li>
<li><code>conflictingPairs[i][0] != conflictingPairs[i][1]</code></li>
</ul>

View File

@@ -0,0 +1,65 @@
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p>
<ul>
<li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <strong>modulo</strong> 10.</li>
<li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li>
</ul>
<p>Return <code>true</code> if the final two digits in <code>s</code> are the <strong>same</strong>; 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;3902&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, <code>s = &quot;3902&quot;</code></li>
<li>First operation:
<ul>
<li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li>
<li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li>
<li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li>
<li><code>s</code> becomes <code>&quot;292&quot;</code></li>
</ul>
</li>
<li>Second operation:
<ul>
<li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li>
<li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li>
<li><code>s</code> becomes <code>&quot;11&quot;</code></li>
</ul>
</li>
<li>Since the digits in <code>&quot;11&quot;</code> are the same, the output is <code>true</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;34789&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, <code>s = &quot;34789&quot;</code>.</li>
<li>After the first operation, <code>s = &quot;7157&quot;</code>.</li>
<li>After the second operation, <code>s = &quot;862&quot;</code>.</li>
<li>After the third operation, <code>s = &quot;48&quot;</code>.</li>
<li>Since <code>&#39;4&#39; != &#39;8&#39;</code>, the output is <code>false</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> consists of only digits.</li>
</ul>

View File

@@ -0,0 +1,65 @@
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p>
<ul>
<li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <strong>modulo</strong> 10.</li>
<li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li>
</ul>
<p>Return <code>true</code> if the final two digits in <code>s</code> are the <strong>same</strong>; 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;3902&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, <code>s = &quot;3902&quot;</code></li>
<li>First operation:
<ul>
<li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li>
<li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li>
<li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li>
<li><code>s</code> becomes <code>&quot;292&quot;</code></li>
</ul>
</li>
<li>Second operation:
<ul>
<li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li>
<li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li>
<li><code>s</code> becomes <code>&quot;11&quot;</code></li>
</ul>
</li>
<li>Since the digits in <code>&quot;11&quot;</code> are the same, the output is <code>true</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;34789&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, <code>s = &quot;34789&quot;</code>.</li>
<li>After the first operation, <code>s = &quot;7157&quot;</code>.</li>
<li>After the second operation, <code>s = &quot;862&quot;</code>.</li>
<li>After the third operation, <code>s = &quot;48&quot;</code>.</li>
<li>Since <code>&#39;4&#39; != &#39;8&#39;</code>, the output is <code>false</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only digits.</li>
</ul>

View File

@@ -0,0 +1,70 @@
<p>You are given an array <code>original</code> of length <code>n</code> and a 2D array <code>bounds</code> of length <code>n x 2</code>, where <code>bounds[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>.</p>
<p>You need to find the number of <strong>possible</strong> arrays <code>copy</code> of length <code>n</code> such that:</p>
<ol>
<li><code>(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])</code> for <code>1 &lt;= i &lt;= n - 1</code>.</li>
<li><code>u<sub>i</sub> &lt;= copy[i] &lt;= v<sub>i</sub></code> for <code>0 &lt;= i &lt;= n - 1</code>.</li>
</ol>
<p>Return the number of such arrays.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The possible arrays are:</p>
<ul>
<li><code>[1, 2, 3, 4]</code></li>
<li><code>[2, 3, 4, 5]</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>The possible arrays are:</p>
<ul>
<li><code>[1, 2, 3, 4]</code></li>
<li><code>[2, 3, 4, 5]</code></li>
<li><code>[3, 4, 5, 6]</code></li>
<li><code>[4, 5, 6, 7]</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No array is possible.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n == original.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= original[i] &lt;= 10<sup>9</sup></code></li>
<li><code>bounds.length == n</code></li>
<li><code>bounds[i].length == 2</code></li>
<li><code>1 &lt;= bounds[i][0] &lt;= bounds[i][1] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,87 @@
<p>You are given two strings, <code>str1</code> and <code>str2</code>, of lengths <code>n</code> and <code>m</code>, respectively.</p>
<p>A string <code>word</code> of length <code>n + m - 1</code> is defined to be <strong>generated</strong> by <code>str1</code> and <code>str2</code> if it satisfies the following conditions for <strong>each</strong> index <code>0 &lt;= i &lt;= n - 1</code>:</p>
<ul>
<li>If <code>str1[i] == &#39;T&#39;</code>, the <strong><span data-keyword="substring-nonempty">substring</span></strong> of <code>word</code> with size <code>m</code> starting at index <code>i</code> is <strong>equal</strong> to <code>str2</code>, i.e., <code>word[i..(i + m - 1)] == str2</code>.</li>
<li>If <code>str1[i] == &#39;F&#39;</code>, the <strong><span data-keyword="substring-nonempty">substring</span></strong> of <code>word</code> with size <code>m</code> starting at index <code>i</code> is <strong>not equal</strong> to <code>str2</code>, i.e., <code>word[i..(i + m - 1)] != str2</code>.</li>
</ul>
<p>Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> possible string that can be <strong>generated</strong> by <code>str1</code> and <code>str2</code>. If no string can be generated, return an empty string <code>&quot;&quot;</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">str1 = &quot;TFTF&quot;, str2 = &quot;ab&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;ababa&quot;</span></p>
<p><strong>Explanation:</strong></p>
<h4>The table below represents the string <code>&quot;ababa&quot;</code></h4>
<table>
<tbody>
<tr>
<th style="border: 1px solid black;">Index</th>
<th style="border: 1px solid black;">T/F</th>
<th style="border: 1px solid black;">Substring of length <code>m</code></th>
</tr>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;T&#39;</code></td>
<td style="border: 1px solid black;">&quot;ab&quot;</td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;F&#39;</code></td>
<td style="border: 1px solid black;">&quot;ba&quot;</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;T&#39;</code></td>
<td style="border: 1px solid black;">&quot;ab&quot;</td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>&#39;F&#39;</code></td>
<td style="border: 1px solid black;">&quot;ba&quot;</td>
</tr>
</tbody>
</table>
<p>The strings <code>&quot;ababa&quot;</code> and <code>&quot;ababb&quot;</code> can be generated by <code>str1</code> and <code>str2</code>.</p>
<p>Return <code>&quot;ababa&quot;</code> since it is the lexicographically smaller string.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">str1 = &quot;TFTF&quot;, str2 = &quot;abc&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>No string that satisfies the conditions can be generated.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">str1 = &quot;F&quot;, str2 = &quot;d&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;a&quot;</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == str1.length &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= m == str2.length &lt;= 500</code></li>
<li><code>str1</code> consists only of <code>&#39;T&#39;</code> or <code>&#39;F&#39;</code>.</li>
<li><code>str2</code> consists only of lowercase English characters.</li>
</ul>

View File

@@ -0,0 +1,48 @@
<p>You are given an integer array <code>nums</code>. Transform <code>nums</code> by performing the following operations in the <strong>exact</strong> order specified:</p>
<ol>
<li>Replace each even number with 0.</li>
<li>Replace each odd numbers with 1.</li>
<li>Sort the modified array in <strong>non-decreasing</strong> order.</li>
</ol>
<p>Return the resulting array after performing these 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">nums = [4,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,0,1,1]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, <code>nums = [0, 1, 0, 1]</code>.</li>
<li>After sorting <code>nums</code> in non-descending order, <code>nums = [0, 0, 1, 1]</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,5,1,4,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,0,1,1,1]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, <code>nums = [1, 1, 1, 0, 0]</code>.</li>
<li>After sorting <code>nums</code> in non-descending order, <code>nums = [0, 0, 1, 1, 1]</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;= 1000</code></li>
</ul>

View File

@@ -0,0 +1,57 @@
<p>You are given two arrays of integers, <code>fruits</code> and <code>baskets</code>, each of length <code>n</code>, where <code>fruits[i]</code> represents the <strong>quantity</strong> of the <code>i<sup>th</sup></code> type of fruit, and <code>baskets[j]</code> represents the <strong>capacity</strong> of the <code>j<sup>th</sup></code> basket.</p>
<p>From left to right, place the fruits according to these rules:</p>
<ul>
<li>Each fruit type must be placed in the <strong>leftmost available basket</strong> with a capacity <strong>greater than or equal</strong> to the quantity of that fruit type.</li>
<li>Each basket can hold <b>only one</b> type of fruit.</li>
<li>If a fruit type <b>cannot be placed</b> in any basket, it remains <b>unplaced</b>.</li>
</ul>
<p>Return the number of fruit types that remain unplaced after all possible allocations are made.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>fruits[0] = 4</code> is placed in <code>baskets[1] = 5</code>.</li>
<li><code>fruits[1] = 2</code> is placed in <code>baskets[0] = 3</code>.</li>
<li><code>fruits[2] = 5</code> cannot be placed in <code>baskets[2] = 4</code>.</li>
</ul>
<p>Since one fruit type remains unplaced, we return 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>fruits[0] = 3</code> is placed in <code>baskets[0] = 6</code>.</li>
<li><code>fruits[1] = 6</code> cannot be placed in <code>baskets[1] = 4</code> (insufficient capacity) but can be placed in the next available basket, <code>baskets[2] = 7</code>.</li>
<li><code>fruits[2] = 1</code> is placed in <code>baskets[1] = 4</code>.</li>
</ul>
<p>Since all fruits are successfully placed, we return 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == fruits.length == baskets.length</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= fruits[i], baskets[i] &lt;= 1000</code></li>
</ul>

View File

@@ -0,0 +1,57 @@
<p>You are given two arrays of integers, <code>fruits</code> and <code>baskets</code>, each of length <code>n</code>, where <code>fruits[i]</code> represents the <strong>quantity</strong> of the <code>i<sup>th</sup></code> type of fruit, and <code>baskets[j]</code> represents the <strong>capacity</strong> of the <code>j<sup>th</sup></code> basket.</p>
<p>From left to right, place the fruits according to these rules:</p>
<ul>
<li>Each fruit type must be placed in the <strong>leftmost available basket</strong> with a capacity <strong>greater than or equal</strong> to the quantity of that fruit type.</li>
<li>Each basket can hold <b>only one</b> type of fruit.</li>
<li>If a fruit type <b>cannot be placed</b> in any basket, it remains <b>unplaced</b>.</li>
</ul>
<p>Return the number of fruit types that remain unplaced after all possible allocations are made.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>fruits[0] = 4</code> is placed in <code>baskets[1] = 5</code>.</li>
<li><code>fruits[1] = 2</code> is placed in <code>baskets[0] = 3</code>.</li>
<li><code>fruits[2] = 5</code> cannot be placed in <code>baskets[2] = 4</code>.</li>
</ul>
<p>Since one fruit type remains unplaced, we return 1.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>fruits[0] = 3</code> is placed in <code>baskets[0] = 6</code>.</li>
<li><code>fruits[1] = 6</code> cannot be placed in <code>baskets[1] = 4</code> (insufficient capacity) but can be placed in the next available basket, <code>baskets[2] = 7</code>.</li>
<li><code>fruits[2] = 1</code> is placed in <code>baskets[1] = 4</code>.</li>
</ul>
<p>Since all fruits are successfully placed, we return 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == fruits.length == baskets.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= fruits[i], baskets[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,67 @@
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>An integer <code>x</code> is <strong>almost missing</strong> from <code>nums</code> if <code>x</code> appears in <em>exactly</em> one subarray of size <code>k</code> within <code>nums</code>.</p>
<p>Return the <b>largest</b> <strong>almost missing</strong> integer from <code>nums</code>. If no such integer exists, return <code>-1</code>.</p>
A <strong>subarray</strong> is a contiguous sequence of elements within an array.
<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,9,2,1,7], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>1 appears in 2 subarrays of size 3: <code>[9, 2, 1]</code> and <code>[2, 1, 7]</code>.</li>
<li>2 appears in 3 subarrays of size 3: <code>[3, 9, 2]</code>, <code>[9, 2, 1]</code>, <code>[2, 1, 7]</code>.</li>
<li index="2">3 appears in 1 subarray of size 3: <code>[3, 9, 2]</code>.</li>
<li index="3">7 appears in 1 subarray of size 3: <code>[2, 1, 7]</code>.</li>
<li index="4">9 appears in 2 subarrays of size 3: <code>[3, 9, 2]</code>, and <code>[9, 2, 1]</code>.</li>
</ul>
<p>We return 7 since it is the largest integer that appears in exactly one subarray of size <code>k</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,9,7,2,1,7], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>1 appears in 2 subarrays of size 4: <code>[9, 7, 2, 1]</code>, <code>[7, 2, 1, 7]</code>.</li>
<li>2 appears in 3 subarrays of size 4: <code>[3, 9, 7, 2]</code>, <code>[9, 7, 2, 1]</code>, <code>[7, 2, 1, 7]</code>.</li>
<li>3 appears in 1 subarray of size 4: <code>[3, 9, 7, 2]</code>.</li>
<li>7 appears in 3 subarrays of size 4: <code>[3, 9, 7, 2]</code>, <code>[9, 7, 2, 1]</code>, <code>[7, 2, 1, 7]</code>.</li>
<li>9 appears in 2 subarrays of size 4: <code>[3, 9, 7, 2]</code>, <code>[9, 7, 2, 1]</code>.</li>
</ul>
<p>We return 3 since it is the largest and only integer that appears in exactly one subarray of size <code>k</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,0], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no integer that appears in only one subarray of size 1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>0 &lt;= nums[i] &lt;= 50</code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

View File

@@ -0,0 +1,53 @@
<p data-pm-slice="1 3 []">You are given a 2D integer matrix <code>grid</code> of size <code>n x m</code>, an integer array <code>limits</code> of length <code>n</code>, and an integer <code>k</code>. The task is to find the <strong>maximum sum</strong> of <strong>at most</strong> <code>k</code> elements from the matrix <code>grid</code> such that:</p>
<ul data-spread="false">
<li>
<p>The number of elements taken from the <code>i<sup>th</sup></code> row of <code>grid</code> does not exceed <code>limits[i]</code>.</p>
</li>
</ul>
<p data-pm-slice="1 1 []">Return the <strong>maximum sum</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">grid = [[1,2],[3,4]], limits = [1,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>From the second row, we can take at most 2 elements. The elements taken are 4 and 3.</li>
<li>The maximum possible sum of at most 2 selected elements is <code>4 + 3 = 7</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">21</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>From the first row, we can take at most 2 elements. The element taken is 7.</li>
<li>From the second row, we can take at most 2 elements. The elements taken are 8 and 6.</li>
<li>The maximum possible sum of at most 3 selected elements is <code>7 + 8 + 6 = 21</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == limits.length</code></li>
<li><code>m == grid[i].length</code></li>
<li><code>1 &lt;= n, m &lt;= 500</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= limits[i] &lt;= m</code></li>
<li><code>0 &lt;= k &lt;= min(n * m, sum(limits))</code></li>
</ul>

View File

@@ -0,0 +1,71 @@
<p>Table: <code>products</code></p>
<pre>
+--------------+------------+
| Column Name | Type |
+--------------+------------+
| product_id | int |
| product_name | varchar |
| description | varchar |
+--------------+------------+
(product_id) is the unique key for this table.
Each row in the table represents a product with its unique ID, name, and description.
</pre>
<p>Write a solution to find all products whose description <strong>contains a valid serial number</strong> pattern. A valid serial number follows these rules:</p>
<ul>
<li>It starts with the letters <strong>SN</strong>&nbsp;(case-sensitive).</li>
<li>Followed by exactly <code>4</code> digits.</li>
<li>It must have a hyphen (-) <strong>followed by exactly</strong> <code>4</code> digits.</li>
<li>The serial number must be within the description (it may not necessarily start at the beginning).</li>
</ul>
<p>Return <em>the result table&nbsp;ordered by</em> <code>product_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>products table:</p>
<pre class="example-io">
+------------+--------------+------------------------------------------------------+
| product_id | product_name | description |
+------------+--------------+------------------------------------------------------+
| 1 | Widget A | This is a sample product with SN1234-5678 |
| 2 | Widget B | A product with serial SN9876-1234 in the description |
| 3 | Widget C | Product SN1234-56789 is available now |
| 4 | Widget D | No serial number here |
| 5 | Widget E | Check out SN4321-8765 in this description |
+------------+--------------+------------------------------------------------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+------------+--------------+------------------------------------------------------+
| product_id | product_name | description |
+------------+--------------+------------------------------------------------------+
| 1 | Widget A | This is a sample product with SN1234-5678 |
| 2 | Widget B | A product with serial SN9876-1234 in the description |
| 5 | Widget E | Check out SN4321-8765 in this description |
+------------+--------------+------------------------------------------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Product 1:</strong> Valid serial number SN1234-5678</li>
<li><strong>Product 2:</strong> Valid serial number SN9876-1234</li>
<li><strong>Product 3:</strong> Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)</li>
<li><strong>Product 4:</strong> No serial number in the description</li>
<li><strong>Product 5:</strong> Valid serial number SN4321-8765</li>
</ul>
<p>The result table is ordered by product_id in ascending order.</p>
</div>

View File

@@ -0,0 +1,68 @@
<p>You are given an integer <code><font face="monospace">side</font></code>, representing the edge length of a square with corners at <code>(0, 0)</code>, <code>(0, side)</code>, <code>(side, 0)</code>, and <code>(side, side)</code> on a Cartesian plane.</p>
<p>You are also given a <strong>positive</strong> integer <code>k</code> and a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinate of a point lying on the <strong>boundary</strong> of the square.</p>
<p>You need to select <code>k</code> elements among <code>points</code> such that the <strong>minimum</strong> Manhattan distance between any two points is <strong>maximized</strong>.</p>
<p>Return the <strong>maximum</strong> possible <strong>minimum</strong> Manhattan distance between the selected <code>k</code> points.</p>
<p>The Manhattan Distance between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/4080_example0_revised.png" style="width: 200px; height: 200px;" /></p>
<p>Select all four points.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/4080_example1_revised.png" style="width: 211px; height: 200px;" /></p>
<p>Select the points <code>(0, 0)</code>, <code>(2, 0)</code>, <code>(2, 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">side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/4080_example2_revised.png" style="width: 200px; height: 200px;" /></p>
<p>Select the points <code>(0, 0)</code>, <code>(0, 1)</code>, <code>(0, 2)</code>, <code>(1, 2)</code>, and <code>(2, 2)</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= side &lt;= 10<sup>9</sup></code></li>
<li><code>4 &lt;= points.length &lt;= min(4 * side, 15 * 10<sup>3</sup>)</code></li>
<li><code>points[i] == [xi, yi]</code></li>
<li>The input is generated such that:
<ul>
<li><code>points[i]</code> lies on the boundary of the square.</li>
<li>All <code>points[i]</code> are <strong>unique</strong>.</li>
</ul>
</li>
<li><code>4 &lt;= k &lt;= min(25, points.length)</code></li>
</ul>

View File

@@ -0,0 +1,55 @@
<p>You are given an integer array <code>nums</code>. Your task is to remove <strong>all elements</strong> from the array by performing one of the following operations at each step until <code>nums</code> is empty:</p>
<ul>
<li>Choose any two elements from the first three elements of <code>nums</code> and remove them. The cost of this operation is the <strong>maximum</strong> of the two elements removed.</li>
<li>If fewer than three elements remain in <code>nums</code>, remove all the remaining elements in a single operation. The cost of this operation is the <strong>maximum</strong> of the remaining elements.</li>
</ul>
<p>Return the <strong>minimum</strong> cost required to remove all the elements.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [6,2,8,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>Initially, <code>nums = [6, 2, 8, 4]</code>.</p>
<ul>
<li>In the first operation, remove <code>nums[0] = 6</code> and <code>nums[2] = 8</code> with a cost of <code>max(6, 8) = 8</code>. Now, <code>nums = [2, 4]</code>.</li>
<li>In the second operation, remove the remaining elements with a cost of <code>max(2, 4) = 4</code>.</li>
</ul>
<p>The cost to remove all elements is <code>8 + 4 = 12</code>. This is the minimum cost to remove all elements in <code>nums</code>. Hence, the output is 12.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>Initially, <code>nums = [2, 1, 3, 3]</code>.</p>
<ul>
<li>In the first operation, remove <code>nums[0] = 2</code> and <code>nums[1] = 1</code> with a cost of <code>max(2, 1) = 2</code>. Now, <code>nums = [3, 3]</code>.</li>
<li>In the second operation remove the remaining elements with a cost of <code>max(3, 3) = 3</code>.</li>
</ul>
<p>The cost to remove all elements is <code>2 + 3 = 5</code>. This is the minimum cost to remove all elements in <code>nums</code>. Hence, the output is 5.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</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> and an integer <code>k</code>.</p>
<p>In one operation, you can replace the character at any position with the next or previous letter in the alphabet (wrapping around so that <code>&#39;a&#39;</code> is after <code>&#39;z&#39;</code>). For example, replacing <code>&#39;a&#39;</code> with the next letter results in <code>&#39;b&#39;</code>, and replacing <code>&#39;a&#39;</code> with the previous letter results in <code>&#39;z&#39;</code>. Similarly, replacing <code>&#39;z&#39;</code> with the next letter results in <code>&#39;a&#39;</code>, and replacing <code>&#39;z&#39;</code> with the previous letter results in <code>&#39;y&#39;</code>.</p>
<p>Return the length of the <strong>longest <span data-keyword="palindrome-string">palindromic</span> <span data-keyword="subsequence-string-nonempty">subsequence</span></strong> of <code>s</code> that can be obtained after performing <strong>at most</strong> <code>k</code> 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 = &quot;abced&quot;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Replace <code>s[1]</code> with the next letter, and <code>s</code> becomes <code>&quot;acced&quot;</code>.</li>
<li>Replace <code>s[4]</code> with the previous letter, and <code>s</code> becomes <code>&quot;accec&quot;</code>.</li>
</ul>
<p>The subsequence <code>&quot;ccc&quot;</code> forms a palindrome of length 3, which is the maximum.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;</span>aaazzz<span class="example-io">&quot;, k = 4</span></p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Replace <code>s[0]</code> with the previous letter, and <code>s</code> becomes <code>&quot;zaazzz&quot;</code>.</li>
<li>Replace <code>s[4]</code> with the next letter, and <code>s</code> becomes <code>&quot;zaazaz&quot;</code>.</li>
<li>Replace <code>s[3]</code> with the next letter, and <code>s</code> becomes <code>&quot;zaaaaz&quot;</code>.</li>
</ul>
<p>The entire string forms a palindrome of length 6.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 200</code></li>
<li><code>1 &lt;= k &lt;= 200</code></li>
<li><code>s</code> consists of only lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,51 @@
<p>You are given two integer arrays, <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>, along with a positive integer <code>k</code>.</p>
<p>For each index <code>i</code> from <code>0</code> to <code>n - 1</code>, perform the following:</p>
<ul>
<li>Find <strong>all</strong> indices <code>j</code> where <code>nums1[j]</code> is less than <code>nums1[i]</code>.</li>
<li>Choose <strong>at most</strong> <code>k</code> values of <code>nums2[j]</code> at these indices to <strong>maximize</strong> the total sum.</li>
</ul>
<p>Return an array <code>answer</code> of size <code>n</code>, where <code>answer[i]</code> represents the result for the corresponding index <code>i</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">nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[80,30,0,80,50]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: Select the 2 largest values from <code>nums2</code> at indices <code>[1, 2, 4]</code> where <code>nums1[j] &lt; nums1[0]</code>, resulting in <code>50 + 30 = 80</code>.</li>
<li>For <code>i = 1</code>: Select the 2 largest values from <code>nums2</code> at index <code>[2]</code> where <code>nums1[j] &lt; nums1[1]</code>, resulting in 30.</li>
<li>For <code>i = 2</code>: No indices satisfy <code>nums1[j] &lt; nums1[2]</code>, resulting in 0.</li>
<li>For <code>i = 3</code>: Select the 2 largest values from <code>nums2</code> at indices <code>[0, 1, 2, 4]</code> where <code>nums1[j] &lt; nums1[3]</code>, resulting in <code>50 + 30 = 80</code>.</li>
<li>For <code>i = 4</code>: Select the 2 largest values from <code>nums2</code> at indices <code>[1, 2]</code> where <code>nums1[j] &lt; nums1[4]</code>, resulting in <code>30 + 20 = 50</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,0,0,0]</span></p>
<p><strong>Explanation:</strong></p>
<p>Since all elements in <code>nums1</code> are equal, no indices satisfy the condition <code>nums1[j] &lt; nums1[i]</code> for any <code>i</code>, resulting in 0 for all positions.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length == nums2.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n</code></li>
</ul>

View File

@@ -0,0 +1,45 @@
<p>You are given an integer array <code>nums</code> and two integers, <code>k</code> and <code>m</code>.</p>
<p>Return the <strong>maximum</strong> sum of <code>k</code> non-overlapping <span data-keyword="subarray">subarrays</span> of <code>nums</code>, where each subarray has a length of <strong>at least</strong> <code>m</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,-1,3,3,4], k = 2, m = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">13</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal choice is:</p>
<ul>
<li>Subarray <code>nums[3..5]</code> with sum <code>3 + 3 + 4 = 10</code> (length is <code>3 &gt;= m</code>).</li>
<li>Subarray <code>nums[0..1]</code> with sum <code>1 + 2 = 3</code> (length is <code>2 &gt;= m</code>).</li>
</ul>
<p>The total sum is <code>10 + 3 = 13</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-10,3,-1,-2], k = 4, m = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">-10</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal choice is choosing each element as a subarray. The output is <code>(-10) + 3 + (-1) + (-2) = -10</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2000</code></li>
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= k &lt;= floor(nums.length / m)</code></li>
<li><code>1 &lt;= m &lt;= 3</code></li>
</ul>