1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 10:38:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2023-05-03 20:22:43 +08:00
parent 6de82df1ca
commit e11824a0e7
44 changed files with 17339 additions and 12900 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,47 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code> 和一个整数&nbsp;<code>k</code>&nbsp;。你需要执行以下操作<strong>&nbsp;恰好</strong> <code>k</code>&nbsp;次,最大化你的得分:</p>
<ol>
<li><code>nums</code>&nbsp;中选择一个元素&nbsp;<code>m</code>&nbsp;</li>
<li>将选中的元素&nbsp;<code>m</code>&nbsp;从数组中删除。</li>
<li>将新元素&nbsp;<code>m + 1</code>&nbsp;添加到数组中。</li>
<li>你的得分增加&nbsp;<code>m</code>&nbsp;</li>
</ol>
<p>请你返回执行以上操作恰好 <code>k</code>&nbsp;次后的最大得分。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3,4,5], k = 3
<b>输出:</b>18
<b>解释:</b>我们需要从 nums 中恰好选择 3 个元素并最大化得分。
第一次选择 5 。和为 5 nums = [1,2,3,4,6] 。
第二次选择 6 。和为 6 nums = [1,2,3,4,7] 。
第三次选择 7 。和为 5 + 6 + 7 = 18 nums = [1,2,3,4,8] 。
所以我们返回 18 。
18 是可以得到的最大答案。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [5,5,5], k = 2
<b>输出:</b>11
<b>解释:</b>我们需要从 nums 中恰好选择 2 个元素并最大化得分。
第一次选择 5 。和为 5 nums = [5,5,6] 。
第二次选择 6 。和为 6 nums = [5,5,7] 。
所以我们返回 11 。
11 是可以得到的最大答案。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</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,61 @@
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>player1</code><code>player2</code> ,分别表示玩家 1 和玩家 2 击中的瓶数。</p>
<p>保龄球比赛由 <code>n</code> 轮组成,每轮的瓶数恰好为 <code>10</code></p>
<p>假设玩家在第 <code>i</code> 轮中击中&nbsp;<code>x<sub>i</sub></code> 个瓶子。玩家第 <code>i</code> 轮的价值为:</p>
<ul>
<li>如果玩家在前两轮中击中了 <code>10</code> 个瓶子,则为 <code>2x<sub>i</sub></code></li>
<li>否则,为&nbsp;<code>x<sub>i</sub></code></li>
</ul>
<p>玩家的得分是其 <code>n</code> 轮价值的总和。</p>
<p>返回</p>
<ul>
<li>如果玩家 1 的得分高于玩家 2 的得分,则为 <code>1</code> </li>
<li>如果玩家 2 的得分高于玩家 1 的得分,则为 <code>2</code> </li>
<li>如果平局,则为 <code>0</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>player1 = [4,10,7,9], player2 = [6,5,2,3]
<strong>输出:</strong>1
<strong>解释:</strong>player1 的得分是 4 + 10 + 2*7 + 2*9 = 46 。
player2 的得分是 6 + 5 + 2 + 3 = 16 。
player1 的得分高于 player2 的得分,所以 play1 在比赛中获胜,答案为 1 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>player1 = [3,5,7,6], player2 = [8,10,10,2]
<strong>输出:</strong>2
<strong>解释:</strong>player1 的得分是 3 + 5 + 7 + 6 = 21 。
player2 的得分是 8 + 10 + 2*10 + 2*2 = 42 。
player2 的得分高于 player1 的得分,所以 play2 在比赛中获胜,答案为 2 。</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>player1 = [2,3], player2 = [4,1]
<strong>输出:</strong>0
<strong>解释:</strong>player1 的得分是 2 + 3 = 5 。
player2 的得分是 4 + 1 = 5 。
player1 的得分等于 player2 的得分,所以这一场比赛平局,答案为 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == player1.length == player2.length</code></li>
<li><code>1 &lt;= n &lt;= 1000</code></li>
<li><code>0 &lt;= player1[i], player2[i] &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,44 @@
<p>给你一个数组 <code>start</code> ,其中 <code>start = [startX, startY]</code> 表示你的初始位置位于二维空间上的 <code>(startX, startY)</code> 。另给你一个数组 <code>target</code> ,其中 <code>target = [targetX, targetY]</code> 表示你的目标位置 <code>(targetX, targetY)</code></p>
<p>从位置 <code>(x1, y1)</code> 到空间中任一其他位置 <code>(x2, y2)</code> 的代价是 <code>|x2 - x1| + |y2 - y1|</code></p>
<p>给你一个二维数组 <code>specialRoads</code> ,表示空间中存在的一些特殊路径。其中 <code>specialRoads[i] = [x1<sub>i</sub>, y1<sub>i</sub>, x2<sub>i</sub>, y2<sub>i</sub>, cost<sub>i</sub>]</code> 表示第 <code>i</code> 条特殊路径可以从 <code>(x1<sub>i</sub>, y1<sub>i</sub>)</code><code>(x2<sub>i</sub>, y2<sub>i</sub>)</code> ,但成本等于 <code>cost<sub>i</sub></code> 。你可以使用每条特殊路径任意次数。</p>
<p>返回从 <code>(startX, startY)</code><code>(targetX, targetY)</code> 所需的最小代价。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
<strong>输出:</strong>5
<strong>解释:</strong>从 (1,1) 到 (4,5) 的最优路径如下:
- (1,1) -&gt; (1,2) ,移动的代价是 |1 - 1| + |2 - 1| = 1 。
- (1,2) -&gt; (3,3) ,移动使用第一条特殊路径,代价是 2 。
- (3,3) -&gt; (3,4) ,移动的代价是 |3 - 3| + |4 - 3| = 1.
- (3,4) -&gt; (4,5) ,移动使用第二条特殊路径,代价是 1 。
总代价是 1 + 2 + 1 + 1 = 5 。
可以证明无法以小于 5 的代价完成从 (1,1) 到 (4,5) 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
<strong>输出:</strong>7
<strong>解释:</strong>最优路径是不使用任何特殊路径,直接以 |5 - 3| + |7 - 2| = 7 的代价从初始位置到达目标位置。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>start.length == target.length == 2</code></li>
<li><code>1 &lt;= startX &lt;= targetX &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= startY &lt;= targetY &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= specialRoads.length &lt;= 200</code></li>
<li><code>specialRoads[i].length == 5</code></li>
<li><code>startX &lt;= x1<sub>i</sub>, x2<sub>i</sub> &lt;= targetX</code></li>
<li><code>startY &lt;= y1<sub>i</sub>, y2<sub>i</sub> &lt;= targetY</code></li>
<li><code>1 &lt;= cost<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@ -0,0 +1,44 @@
<p>如果一个字符串满足以下条件,则称其为 <strong>美丽字符串</strong> </p>
<ul>
<li>它由英语小写字母表的前 <code>k</code> 个字母组成。</li>
<li>它不包含任何长度为 <code>2</code> 或更长的回文子字符串。</li>
</ul>
<p>给你一个长度为 <code>n</code> 的美丽字符串 <code>s</code> 和一个正整数 <code>k</code></p>
<p>请你找出并返回一个长度为 <code>n</code> 的美丽字符串,该字符串还满足:在字典序大于 <code>s</code> 的所有美丽字符串中字典序最小。如果不存在这样的字符串,则返回一个空字符串。</p>
<p>对于长度相同的两个字符串 <code>a</code><code>b</code> ,如果字符串 <code>a</code> 在与字符串 <code>b</code> 不同的第一个位置上的字符字典序更大,则字符串 <code>a</code> 的字典序大于字符串 <code>b</code></p>
<ul>
<li>例如,<code>"abcd"</code> 的字典序比 <code>"abcc"</code> 更大,因为在不同的第一个位置(第四个字符)上 <code>d</code> 的字典序大于 <code>c</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "abcz", k = 26
<strong>输出:</strong>"abda"
<strong>解释:</strong>字符串 "abda" 既是美丽字符串,又满足字典序大于 "abcz" 。
可以证明不存在字符串同时满足字典序大于 "abcz"、美丽字符串、字典序小于 "abda" 这三个条件。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "dc", k = 4
<strong>输出:</strong>""
<strong>解释:</strong>可以证明,不存在既是美丽字符串,又字典序大于 "dc" 的字符串。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
<li><code>4 &lt;= k &lt;= 26</code></li>
<li><code>s</code> 是一个美丽字符串</li>
</ul>

View File

@ -0,0 +1,130 @@
<p>给你一个包含若干 <strong>互不相同</strong>&nbsp;整数的数组&nbsp;<code>nums</code>&nbsp;,你需要执行以下操作 <strong>直到</strong><strong>数组为空</strong>&nbsp;</p>
<ul>
<li>如果数组中第一个元素是当前数组中的 <strong>最小值</strong>&nbsp;,则删除它。</li>
<li>否则,将第一个元素移动到数组的 <strong>末尾</strong>&nbsp;</li>
</ul>
<p>请你返回需要多少个操作使<em>&nbsp;</em><code>nums</code><em>&nbsp;</em>为空。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [3,4,-1]
<b>输出:</b>5
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[4, -1, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[-1, 3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">4</td>
<td style="border: 2px solid black; padding: 5px;">[4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">5</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [1,2,4,3]
<b>输出:</b>5
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[2, 4, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[4, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">4</td>
<td style="border: 2px solid black; padding: 5px;">[4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">5</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3]
<b>输出:</b>3
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[2, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9&nbsp;</sup>&lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>nums</code>&nbsp;中的元素 <strong>互不相同</strong>&nbsp;</li>
</ul>

View File

@ -0,0 +1,38 @@
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>arr</code> 和一个 <code>m x n</code> 的整数 <strong>矩阵</strong> <code>mat</code><code>arr</code><code>mat</code> 都包含范围 <code>[1m * n]</code> 内的 <strong>所有</strong> 整数。</p>
<p>从下标 <code>0</code> 开始遍历 <code>arr</code> 中的每个下标 <code>i</code> ,并将包含整数 <code>arr[i]</code><code>mat</code> 单元格涂色。</p>
<p>请你找出 <code>arr</code> 中在 <code>mat</code> 的某一行或某一列上都被涂色且下标最小的元素,并返回其下标 <code>i</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="image explanation for example 1" src="https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg" style="width: 321px; height: 81px;" />
<pre>
<strong>输入:</strong>arr = [1,3,4,2], mat = [[1,4],[2,3]]
<strong>输出:</strong>2
<strong>解释:</strong>遍历如上图所示arr[2] 在矩阵中的第一行或第二列上都被涂色。
</pre>
<p><strong>示例 2</strong></p>
<img alt="image explanation for example 2" src="https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg" style="width: 601px; height: 121px;" />
<pre>
<strong>输入:</strong>arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
<strong>输出:</strong>3
<strong>解释:</strong>遍历如上图所示arr[3] 在矩阵中的第二列上都被涂色。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n = mat[i].length</code></li>
<li><code>arr.length == m * n</code></li>
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= arr[i], mat[r][c] &lt;= m * n</code></li>
<li><code>arr</code> 中的所有整数 <strong>互不相同</strong></li>
<li><code>mat</code> 中的所有整数 <strong>互不相同</strong></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>给你两个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的整数排列&nbsp;<code>A</code>&nbsp;<code>B</code>&nbsp;</p>
<p><code>A</code>&nbsp;&nbsp;<code>B</code>&nbsp;<strong>前缀公共数组</strong>&nbsp;定义为数组&nbsp;<code>C</code>&nbsp;,其中&nbsp;<code>C[i]</code>&nbsp;是数组&nbsp;<code>A</code>&nbsp;<code>B</code>&nbsp;到下标为&nbsp;<code>i</code>&nbsp;之前公共元素的数目。</p>
<p>请你返回 <code>A</code>&nbsp;<code>B</code>&nbsp;<strong>前缀公共数组</strong>&nbsp;</p>
<p>如果一个长度为 <code>n</code>&nbsp;的数组包含 <code>1</code>&nbsp;<code>n</code>&nbsp;的元素恰好一次,我们称这个数组是一个长度为 <code>n</code>&nbsp;<strong>排列</strong>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>A = [1,3,2,4], B = [3,1,2,4]
<b>输出:</b>[0,2,3,4]
<b>解释:</b>i = 0没有公共元素所以 C[0] = 0 。
i = 11 和 3 是两个数组的前缀公共元素,所以 C[1] = 2 。
i = 212 和 3 是两个数组的前缀公共元素,所以 C[2] = 3 。
i = 3123 和 4 是两个数组的前缀公共元素,所以 C[3] = 4 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>A = [2,3,1], B = [3,1,2]
<b>输出:</b>[0,1,3]
<b>解释:</b>i = 0没有公共元素所以 C[0] = 0 。
i = 1只有 3 是公共元素,所以 C[1] = 1 。
i = 212 和 3 是两个数组的前缀公共元素,所以 C[2] = 3 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= A.length == B.length == n &lt;= 50</code></li>
<li><code>1 &lt;= A[i], B[i] &lt;= n</code></li>
<li>题目保证&nbsp;<code>A</code>&nbsp;&nbsp;<code>B</code>&nbsp;两个数组都是&nbsp;<code>n</code>&nbsp;个元素的排列。</li>
</ul>

View File

@ -0,0 +1,48 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始大小为 <code>m x n</code>&nbsp;的二维整数数组&nbsp;<code>grid</code>&nbsp;,其中下标在&nbsp;<code>(r, c)</code>&nbsp;处的整数表示:</p>
<ul>
<li>如果&nbsp;<code>grid[r][c] = 0</code>&nbsp;,那么它是一块 <strong>陆地</strong>&nbsp;</li>
<li>如果&nbsp;<code>grid[r][c] &gt; 0</code>&nbsp;,那么它是一块&nbsp;<strong>水域</strong>&nbsp;,且包含&nbsp;<code>grid[r][c]</code>&nbsp;条鱼。</li>
</ul>
<p>一位渔夫可以从任意 <strong>水域</strong>&nbsp;格子&nbsp;<code>(r, c)</code>&nbsp;出发,然后执行以下操作任意次:</p>
<ul>
<li>捕捞格子&nbsp;<code>(r, c)</code>&nbsp;处所有的鱼,或者</li>
<li>移动到相邻的 <strong>水域</strong>&nbsp;格子。</li>
</ul>
<p>请你返回渔夫最优策略下,&nbsp;<strong>最多</strong>&nbsp;可以捕捞多少条鱼。如果没有水域格子,请你返回 <code>0</code>&nbsp;</p>
<p>格子&nbsp;<code>(r, c)</code>&nbsp;<strong>相邻</strong>&nbsp;的格子为&nbsp;<code>(r, c + 1)</code>&nbsp;<code>(r, c - 1)</code>&nbsp;<code>(r + 1, c)</code>&nbsp;<code>(r - 1, c)</code>&nbsp;,前提是相邻格子在网格图内。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example.png" style="width: 241px; height: 161px;"></p>
<pre><b>输入:</b>grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
<b>输出:</b>7
<b>解释:</b>渔夫可以从格子 <code>(1,3)</code> 出发,捕捞 3 条鱼,然后移动到格子 <code>(2,3)</code>&nbsp;,捕捞 4 条鱼。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example2.png"></p>
<pre><b>输入:</b>grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
<b>输出:</b>1
<b>解释:</b>渔夫可以从格子 (0,0) 或者 (3,3) ,捕捞 1 条鱼。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 10</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,54 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>. Your task is to perform the following operation <strong>exactly</strong> <code>k</code> times in order to maximize your score:</p>
<ol>
<li>Select an element <code>m</code> from <code>nums</code>.</li>
<li>Remove the selected element <code>m</code> from the array.</li>
<li>Add a new element with a value of <code>m + 1</code> to the array.</li>
<li>Increase your score by <code>m</code>.</li>
</ol>
<p>Return <em>the maximum score you can achieve after performing the operation exactly</em> <code>k</code> <em>times.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 3
<strong>Output:</strong> 18
<strong>Explanation:</strong> We need to choose exactly 3 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
So, we will return 18.
It can be proven, that 18 is the maximum answer that we can achieve.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,5], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> We need to choose exactly 2 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
So, we will return 11.
It can be proven, that 11 is the maximum answer that we can achieve.
</pre>
<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>
<p>&nbsp;</p>
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
}
.spoiler {overflow:hidden;}
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
</style>

View File

@ -0,0 +1,61 @@
<p>You are given two <strong>0-indexed</strong> integer arrays <code><font face="monospace">player1</font></code> and <code>player2</code>, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.</p>
<p>The bowling game consists of <code>n</code> turns, and the number of pins in each turn is exactly <code>10</code>.</p>
<p>Assume a player hit <code>x<sub>i</sub></code> pins in the <code>i<sup>th</sup></code> turn. The value of the <code>i<sup>th</sup></code> turn for the player is:</p>
<ul>
<li><code>2x<sub>i</sub></code> if the player hit <code>10</code> pins in any of the previous two turns.</li>
<li>Otherwise, It is <code>x<sub>i</sub></code>.</li>
</ul>
<p>The score of the player is the sum of the values of their <code>n</code> turns.</p>
<p>Return</p>
<ul>
<li><code>1</code> <em>if the score of player 1 is more than the score of player 2,</em></li>
<li><code>2</code> <em>if the score of player 2 is more than the score of player 1, and</em></li>
<li><code>0</code> <em>in case of a draw.</em></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> player1 = [4,10,7,9], player2 = [6,5,2,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
The score of player2 is 6 + 5 + 2 + 3 = 16.
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> player1 = [3,5,7,6], player2 = [8,10,10,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The score of player1 is 3 + 5 + 7 + 6 = 21.
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> player1 = [2,3], player2 = [4,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The score of player1 is 2 + 3 = 5
The score of player2 is 4 + 1 = 5
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == player1.length == player2.length</code></li>
<li><code>1 &lt;= n &lt;= 1000</code></li>
<li><code>0 &lt;= player1[i], player2[i] &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,44 @@
<p>You are given an array <code>start</code> where <code>start = [startX, startY]</code> represents your initial position <code>(startX, startY)</code> in a 2D space. You are also given the array <code>target</code> where <code>target = [targetX, targetY]</code> represents your target position <code>(targetX, targetY)</code>.</p>
<p>The cost of going from a position <code>(x1, y1)</code> to any other position in the space <code>(x2, y2)</code> is <code>|x2 - x1| + |y2 - y1|</code>.</p>
<p>There are also some special roads. You are given a 2D array <code>specialRoads</code> where <code>specialRoads[i] = [x1<sub>i</sub>, y1<sub>i</sub>, x2<sub>i</sub>, y2<sub>i</sub>, cost<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> special road can take you from <code>(x1<sub>i</sub>, y1<sub>i</sub>)</code> to <code>(x2<sub>i</sub>, y2<sub>i</sub>)</code> with a cost equal to <code>cost<sub>i</sub></code>. You can use each special road any number of times.</p>
<p>Return <em>the minimum cost required to go from</em> <code>(startX, startY)</code> to <code>(targetX, targetY)</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal path from (1,1) to (4,5) is the following:
- (1,1) -&gt; (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1.
- (1,2) -&gt; (3,3). This move uses the first special edge, the cost is 2.
- (3,3) -&gt; (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1.
- (3,4) -&gt; (4,5). This move uses the second special edge, the cost is 1.
So the total cost is 1 + 2 + 1 + 1 = 5.
It can be shown that we cannot achieve a smaller total cost than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>start.length == target.length == 2</code></li>
<li><code>1 &lt;= startX &lt;= targetX &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= startY &lt;= targetY &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= specialRoads.length &lt;= 200</code></li>
<li><code>specialRoads[i].length == 5</code></li>
<li><code>startX &lt;= x1<sub>i</sub>, x2<sub>i</sub> &lt;= targetX</code></li>
<li><code>startY &lt;= y1<sub>i</sub>, y2<sub>i</sub> &lt;= targetY</code></li>
<li><code>1 &lt;= cost<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>A string is <strong>beautiful</strong> if:</p>
<ul>
<li>It consists of the first <code>k</code> letters of the English lowercase alphabet.</li>
<li>It does not contain any substring of length <code>2</code> or more which is a palindrome.</li>
</ul>
<p>You are given a beautiful string <code>s</code> of length <code>n</code> and a positive integer <code>k</code>.</p>
<p>Return <em>the lexicographically smallest string of length </em><code>n</code><em>, which is larger than </em><code>s</code><em> and is <strong>beautiful</strong></em>. If there is no such string, return an empty string.</p>
<p>A string <code>a</code> is lexicographically larger than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly larger than the corresponding character in <code>b</code>.</p>
<ul>
<li>For example, <code>&quot;abcd&quot;</code> is lexicographically larger than <code>&quot;abcc&quot;</code> because the first position they differ is at the fourth character, and <code>d</code> is greater than <code>c</code>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;abcz&quot;, k = 26
<strong>Output:</strong> &quot;abda&quot;
<strong>Explanation:</strong> The string &quot;abda&quot; is beautiful and lexicographically larger than the string &quot;abcz&quot;.
It can be proven that there is no string that is lexicographically larger than the string &quot;abcz&quot;, beautiful, and lexicographically smaller than the string &quot;abda&quot;.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;dc&quot;, k = 4
<strong>Output:</strong> &quot;&quot;
<strong>Explanation:</strong> It can be proven that there is no string that is lexicographically larger than the string &quot;dc&quot; and is beautiful.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
<li><code>4 &lt;= k &lt;= 26</code></li>
<li><code>s</code> is a beautiful string.</li>
</ul>

View File

@ -0,0 +1,124 @@
<p>You are given an integer array <code>nums</code> containing <strong>distinct</strong> numbers, and you can perform the following operations <strong>until the array is empty</strong>:</p>
<ul>
<li>If the first element has the <strong>smallest</strong> value, remove it</li>
<li>Otherwise, put the first element at the <strong>end</strong> of the array.</li>
</ul>
<p>Return <em>an integer denoting the number of operations it takes to make </em><code>nums</code><em> empty.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,-1]
<strong>Output:</strong> 5
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[4, -1, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[-1, 3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">4</td>
<td style="border: 2px solid black; padding: 5px;">[4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">5</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,4,3]
<strong>Output:</strong> 5
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[2, 4, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[4, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">4</td>
<td style="border: 2px solid black; padding: 5px;">[4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">5</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[2, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9&nbsp;</sup>&lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li>All values in <code>nums</code> are <strong>distinct</strong>.</li>
</ul>

View File

@ -0,0 +1,36 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code>, and an <code>m x n</code> integer <strong>matrix</strong> <code>mat</code>. <code>arr</code> and <code>mat</code> both contain <strong>all</strong> the integers in the range <code>[1, m * n]</code>.</p>
<p>Go through each index <code>i</code> in <code>arr</code> starting from index <code>0</code> and paint the cell in <code>mat</code> containing the integer <code>arr[i]</code>.</p>
<p>Return <em>the smallest index</em> <code>i</code> <em>at which either a row or a column will be completely painted in</em> <code>mat</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="image explanation for example 1" /><img alt="image explanation for example 1" src="https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg" style="width: 321px; height: 81px;" />
<pre>
<strong>Input:</strong> arr = [1,3,4,2], mat = [[1,4],[2,3]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="image explanation for example 2" src="https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg" style="width: 601px; height: 121px;" />
<pre>
<strong>Input:</strong> arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The second column becomes fully painted at arr[3].
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n = mat[i].length</code></li>
<li><code>arr.length == m * n</code></li>
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= arr[i], mat[r][c] &lt;= m * n</code></li>
<li>All the integers of <code>arr</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>mat</code> are <strong>unique</strong>.</li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given two <strong>0-indexed </strong>integer<strong> </strong>permutations <code>A</code> and <code>B</code> of length <code>n</code>.</p>
<p>A <strong>prefix common array</strong> of <code>A</code> and <code>B</code> is an array <code>C</code> such that <code>C[i]</code> is equal to the count of numbers that are present at or before the index <code>i</code> in both <code>A</code> and <code>B</code>.</p>
<p>Return <em>the <strong>prefix common array</strong> of </em><code>A</code><em> and </em><code>B</code>.</p>
<p>A sequence of <code>n</code> integers is called a&nbsp;<strong>permutation</strong> if it contains all integers from <code>1</code> to <code>n</code> exactly once.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> A = [1,3,2,4], B = [3,1,2,4]
<strong>Output:</strong> [0,2,3,4]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> A = [2,3,1], B = [3,1,2]
<strong>Output:</strong> [0,1,3]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= A.length == B.length == n &lt;= 50</code></li>
<li><code>1 &lt;= A[i], B[i] &lt;= n</code></li>
<li><code>It is guaranteed that A and B are both a permutation of n integers.</code></li>
</ul>

View File

@ -0,0 +1,44 @@
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>m x n</code>, where <code>(r, c)</code> represents:</p>
<ul>
<li>A <strong>land</strong> cell if <code>grid[r][c] = 0</code>, or</li>
<li>A <strong>water</strong> cell containing <code>grid[r][c]</code> fish, if <code>grid[r][c] &gt; 0</code>.</li>
</ul>
<p>A fisher can start at any <strong>water</strong> cell <code>(r, c)</code> and can do the following operations any number of times:</p>
<ul>
<li>Catch all the fish at cell <code>(r, c)</code>, or</li>
<li>Move to any adjacent <strong>water</strong> cell.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> number of fish the fisher can catch if he chooses his starting cell optimally, or </em><code>0</code> if no water cell exists.</p>
<p>An <strong>adjacent</strong> cell of the cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> or <code>(r - 1, c)</code> if it exists.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example.png" style="width: 241px; height: 161px;" />
<pre>
<strong>Input:</strong> grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The fisher can start at cell <code>(1,3)</code> and collect 3 fish, then move to cell <code>(2,3)</code>&nbsp;and collect 4 fish.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example2.png" />
<pre>
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The fisher can start at cells (0,0) or (3,3) and collect a single fish.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 10</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 10</code></li>
</ul>

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,61 @@
<p>You are given two <strong>0-indexed</strong> integer arrays <code><font face="monospace">player1</font></code> and <code>player2</code>, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.</p>
<p>The bowling game consists of <code>n</code> turns, and the number of pins in each turn is exactly <code>10</code>.</p>
<p>Assume a player hit <code>x<sub>i</sub></code> pins in the <code>i<sup>th</sup></code> turn. The value of the <code>i<sup>th</sup></code> turn for the player is:</p>
<ul>
<li><code>2x<sub>i</sub></code> if the player hit <code>10</code> pins in any of the previous two turns.</li>
<li>Otherwise, It is <code>x<sub>i</sub></code>.</li>
</ul>
<p>The score of the player is the sum of the values of their <code>n</code> turns.</p>
<p>Return</p>
<ul>
<li><code>1</code> <em>if the score of player 1 is more than the score of player 2,</em></li>
<li><code>2</code> <em>if the score of player 2 is more than the score of player 1, and</em></li>
<li><code>0</code> <em>in case of a draw.</em></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> player1 = [4,10,7,9], player2 = [6,5,2,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
The score of player2 is 6 + 5 + 2 + 3 = 16.
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> player1 = [3,5,7,6], player2 = [8,10,10,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The score of player1 is 3 + 5 + 7 + 6 = 21.
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> player1 = [2,3], player2 = [4,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The score of player1 is 2 + 3 = 5
The score of player2 is 4 + 1 = 5
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == player1.length == player2.length</code></li>
<li><code>1 &lt;= n &lt;= 1000</code></li>
<li><code>0 &lt;= player1[i], player2[i] &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given two <strong>0-indexed </strong>integer<strong> </strong>permutations <code>A</code> and <code>B</code> of length <code>n</code>.</p>
<p>A <strong>prefix common array</strong> of <code>A</code> and <code>B</code> is an array <code>C</code> such that <code>C[i]</code> is equal to the count of numbers that are present at or before the index <code>i</code> in both <code>A</code> and <code>B</code>.</p>
<p>Return <em>the <strong>prefix common array</strong> of </em><code>A</code><em> and </em><code>B</code>.</p>
<p>A sequence of <code>n</code> integers is called a&nbsp;<strong>permutation</strong> if it contains all integers from <code>1</code> to <code>n</code> exactly once.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> A = [1,3,2,4], B = [3,1,2,4]
<strong>Output:</strong> [0,2,3,4]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> A = [2,3,1], B = [3,1,2]
<strong>Output:</strong> [0,1,3]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= A.length == B.length == n &lt;= 50</code></li>
<li><code>1 &lt;= A[i], B[i] &lt;= n</code></li>
<li><code>It is guaranteed that A and B are both a permutation of n integers.</code></li>
</ul>

View File

@ -0,0 +1,36 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code>, and an <code>m x n</code> integer <strong>matrix</strong> <code>mat</code>. <code>arr</code> and <code>mat</code> both contain <strong>all</strong> the integers in the range <code>[1, m * n]</code>.</p>
<p>Go through each index <code>i</code> in <code>arr</code> starting from index <code>0</code> and paint the cell in <code>mat</code> containing the integer <code>arr[i]</code>.</p>
<p>Return <em>the smallest index</em> <code>i</code> <em>at which either a row or a column will be completely painted in</em> <code>mat</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="image explanation for example 1" /><img alt="image explanation for example 1" src="https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg" style="width: 321px; height: 81px;" />
<pre>
<strong>Input:</strong> arr = [1,3,4,2], mat = [[1,4],[2,3]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="image explanation for example 2" src="https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg" style="width: 601px; height: 121px;" />
<pre>
<strong>Input:</strong> arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The second column becomes fully painted at arr[3].
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == mat.length</code></li>
<li><code>n = mat[i].length</code></li>
<li><code>arr.length == m * n</code></li>
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= arr[i], mat[r][c] &lt;= m * n</code></li>
<li>All the integers of <code>arr</code> are <strong>unique</strong>.</li>
<li>All the integers of <code>mat</code> are <strong>unique</strong>.</li>
</ul>

View File

@ -0,0 +1,43 @@
<p>A string is <strong>beautiful</strong> if:</p>
<ul>
<li>It consists of the first <code>k</code> letters of the English lowercase alphabet.</li>
<li>It does not contain any substring of length <code>2</code> or more which is a palindrome.</li>
</ul>
<p>You are given a beautiful string <code>s</code> of length <code>n</code> and a positive integer <code>k</code>.</p>
<p>Return <em>the lexicographically smallest string of length </em><code>n</code><em>, which is larger than </em><code>s</code><em> and is <strong>beautiful</strong></em>. If there is no such string, return an empty string.</p>
<p>A string <code>a</code> is lexicographically larger than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly larger than the corresponding character in <code>b</code>.</p>
<ul>
<li>For example, <code>&quot;abcd&quot;</code> is lexicographically larger than <code>&quot;abcc&quot;</code> because the first position they differ is at the fourth character, and <code>d</code> is greater than <code>c</code>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;abcz&quot;, k = 26
<strong>Output:</strong> &quot;abda&quot;
<strong>Explanation:</strong> The string &quot;abda&quot; is beautiful and lexicographically larger than the string &quot;abcz&quot;.
It can be proven that there is no string that is lexicographically larger than the string &quot;abcz&quot;, beautiful, and lexicographically smaller than the string &quot;abda&quot;.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;dc&quot;, k = 4
<strong>Output:</strong> &quot;&quot;
<strong>Explanation:</strong> It can be proven that there is no string that is lexicographically larger than the string &quot;dc&quot; and is beautiful.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
<li><code>4 &lt;= k &lt;= 26</code></li>
<li><code>s</code> is a beautiful string.</li>
</ul>

View File

@ -0,0 +1,124 @@
<p>You are given an integer array <code>nums</code> containing <strong>distinct</strong> numbers, and you can perform the following operations <strong>until the array is empty</strong>:</p>
<ul>
<li>If the first element has the <strong>smallest</strong> value, remove it</li>
<li>Otherwise, put the first element at the <strong>end</strong> of the array.</li>
</ul>
<p>Return <em>an integer denoting the number of operations it takes to make </em><code>nums</code><em> empty.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,-1]
<strong>Output:</strong> 5
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[4, -1, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[-1, 3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">4</td>
<td style="border: 2px solid black; padding: 5px;">[4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">5</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,4,3]
<strong>Output:</strong> 5
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[2, 4, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[4, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">4</td>
<td style="border: 2px solid black; padding: 5px;">[4]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">5</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
</pre>
<table style="border: 2px solid black; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 2px solid black; padding: 5px;">Operation</th>
<th style="border: 2px solid black; padding: 5px;">Array</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 2px solid black; padding: 5px;">1</td>
<td style="border: 2px solid black; padding: 5px;">[2, 3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">2</td>
<td style="border: 2px solid black; padding: 5px;">[3]</td>
</tr>
<tr>
<td style="border: 2px solid black; padding: 5px;">3</td>
<td style="border: 2px solid black; padding: 5px;">[]</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9&nbsp;</sup>&lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li>All values in <code>nums</code> are <strong>distinct</strong>.</li>
</ul>

View File

@ -0,0 +1,44 @@
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>m x n</code>, where <code>(r, c)</code> represents:</p>
<ul>
<li>A <strong>land</strong> cell if <code>grid[r][c] = 0</code>, or</li>
<li>A <strong>water</strong> cell containing <code>grid[r][c]</code> fish, if <code>grid[r][c] &gt; 0</code>.</li>
</ul>
<p>A fisher can start at any <strong>water</strong> cell <code>(r, c)</code> and can do the following operations any number of times:</p>
<ul>
<li>Catch all the fish at cell <code>(r, c)</code>, or</li>
<li>Move to any adjacent <strong>water</strong> cell.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> number of fish the fisher can catch if he chooses his starting cell optimally, or </em><code>0</code> if no water cell exists.</p>
<p>An <strong>adjacent</strong> cell of the cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> or <code>(r - 1, c)</code> if it exists.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example.png" style="width: 241px; height: 161px;" />
<pre>
<strong>Input:</strong> grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The fisher can start at cell <code>(1,3)</code> and collect 3 fish, then move to cell <code>(2,3)</code>&nbsp;and collect 4 fish.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example2.png" />
<pre>
<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The fisher can start at cells (0,0) or (3,3) and collect a single fish.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 10</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,54 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>. Your task is to perform the following operation <strong>exactly</strong> <code>k</code> times in order to maximize your score:</p>
<ol>
<li>Select an element <code>m</code> from <code>nums</code>.</li>
<li>Remove the selected element <code>m</code> from the array.</li>
<li>Add a new element with a value of <code>m + 1</code> to the array.</li>
<li>Increase your score by <code>m</code>.</li>
</ol>
<p>Return <em>the maximum score you can achieve after performing the operation exactly</em> <code>k</code> <em>times.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 3
<strong>Output:</strong> 18
<strong>Explanation:</strong> We need to choose exactly 3 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
So, we will return 18.
It can be proven, that 18 is the maximum answer that we can achieve.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,5], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> We need to choose exactly 2 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
So, we will return 11.
It can be proven, that 11 is the maximum answer that we can achieve.
</pre>
<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>
<p>&nbsp;</p>
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
}
.spoiler {overflow:hidden;}
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
</style>

View File

@ -0,0 +1,44 @@
<p>You are given an array <code>start</code> where <code>start = [startX, startY]</code> represents your initial position <code>(startX, startY)</code> in a 2D space. You are also given the array <code>target</code> where <code>target = [targetX, targetY]</code> represents your target position <code>(targetX, targetY)</code>.</p>
<p>The cost of going from a position <code>(x1, y1)</code> to any other position in the space <code>(x2, y2)</code> is <code>|x2 - x1| + |y2 - y1|</code>.</p>
<p>There are also some special roads. You are given a 2D array <code>specialRoads</code> where <code>specialRoads[i] = [x1<sub>i</sub>, y1<sub>i</sub>, x2<sub>i</sub>, y2<sub>i</sub>, cost<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> special road can take you from <code>(x1<sub>i</sub>, y1<sub>i</sub>)</code> to <code>(x2<sub>i</sub>, y2<sub>i</sub>)</code> with a cost equal to <code>cost<sub>i</sub></code>. You can use each special road any number of times.</p>
<p>Return <em>the minimum cost required to go from</em> <code>(startX, startY)</code> to <code>(targetX, targetY)</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal path from (1,1) to (4,5) is the following:
- (1,1) -&gt; (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1.
- (1,2) -&gt; (3,3). This move uses the first special edge, the cost is 2.
- (3,3) -&gt; (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1.
- (3,4) -&gt; (4,5). This move uses the second special edge, the cost is 1.
So the total cost is 1 + 2 + 1 + 1 = 5.
It can be shown that we cannot achieve a smaller total cost than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>start.length == target.length == 2</code></li>
<li><code>1 &lt;= startX &lt;= targetX &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= startY &lt;= targetY &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= specialRoads.length &lt;= 200</code></li>
<li><code>specialRoads[i].length == 5</code></li>
<li><code>startX &lt;= x1<sub>i</sub>, x2<sub>i</sub> &lt;= targetX</code></li>
<li><code>startY &lt;= y1<sub>i</sub>, y2<sub>i</sub> &lt;= targetY</code></li>
<li><code>1 &lt;= cost<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>