1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-04-18 08:39:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2023-04-04 00:26:09 +08:00
parent 6e78d50055
commit 0f5817758d
45 changed files with 16894 additions and 12800 deletions
README.md
leetcode-cn
origin-data.json
originData
problem (Chinese)
problem (English)
leetcode

@ -1,6 +1,6 @@
# 力扣题库(完整版)
> 最后更新日期: **2023.02.27**
> 最后更新日期: **2023.03.26**
>
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件

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

@ -0,0 +1,26 @@
给你两个只包含 1 到 9 之间数字的数组&nbsp;<code>nums1</code>&nbsp;<code>nums2</code>&nbsp;,每个数组中的元素 <strong>互不相同</strong>&nbsp;,请你返回 <strong>最小</strong> 的数字,两个数组都 <strong>至少</strong> 包含这个数字的某个数位。
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>nums1 = [4,1,3], nums2 = [5,7]
<b>输出:</b>15
<b>解释:</b>数字 15 的数位 1 在 nums1 中出现,数位 5 在 nums2 中出现。15 是我们能得到的最小数字。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums1 = [3,5,2,6], nums2 = [3,1,7]
<b>输出:</b>3
<b>解释:</b>数字 3 的数位 3 在两个数组中都出现了。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 9</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
<li>每个数组中,元素 <strong>互不相同</strong>&nbsp;</li>
</ul>

@ -0,0 +1,46 @@
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>arr</code> 和一个整数 <code>k</code> 。数组 <code>arr</code> 是一个循环数组。换句话说,数组中的最后一个元素的下一个元素是数组中的第一个元素,数组中第一个元素的前一个元素是数组中的最后一个元素。</p>
<p>你可以执行下述运算任意次:</p>
<ul>
<li>选中 <code>arr</code> 中任意一个元素,并使其值加上 <code>1</code> 或减去 <code>1</code></li>
</ul>
<p>执行运算使每个长度为 <code>k</code><strong>子数组</strong> 的元素总和都相等,返回所需要的最少运算次数。</p>
<p><strong>子数组</strong> 是数组的一个连续部分。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>arr = [1,4,1,3], k = 2
<strong>输出:</strong>1
<strong>解释:</strong>在下标为 1 的元素那里执行一次运算,使其等于 3 。
执行运算后,数组变为 [1,3,1,3] 。
- 0 处起始的子数组为 [1, 3] ,元素总和为 4
- 1 处起始的子数组为 [3, 1] ,元素总和为 4
- 2 处起始的子数组为 [1, 3] ,元素总和为 4
- 3 处起始的子数组为 [3, 1] ,元素总和为 4
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>arr = [2,5,5,7], k = 3
<strong>输出:</strong>5
<strong>解释:</strong>在下标为 0 的元素那里执行三次运算,使其等于 5 。在下标为 3 的元素那里执行两次运算,使其等于 5 。
执行运算后,数组变为 [5,5,5,5] 。
- 0 处起始的子数组为 [5, 5, 5] ,元素总和为 15
- 1 处起始的子数组为 [5, 5, 5] ,元素总和为 15
- 2 处起始的子数组为 [5, 5, 5] ,元素总和为 15
- 3 处起始的子数组为 [5, 5, 5] ,元素总和为 15
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>

@ -0,0 +1,34 @@
<p>现有一个含 <code>n</code> 个顶点的 <strong>双向</strong> 图,每个顶点按从 <code>0</code><code>n - 1</code> 标记。图中的边由二维整数数组 <code>edges</code> 表示,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示顶点 <code>u<sub>i</sub></code><code>v<sub>i</sub></code> 之间存在一条边。每对顶点最多通过一条边连接,并且不存在与自身相连的顶点。</p>
<p>返回图中 <strong>最短</strong> 环的长度。如果不存在环,则返回 <code>-1</code></p>
<p><strong></strong> 是指以同一节点开始和结束,并且路径中的每条边仅使用一次。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;">
<pre><strong>输入:</strong>n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
<strong>输出:</strong>3
<strong>解释:</strong>长度最小的循环是0 -&gt; 1 -&gt; 2 -&gt; 0
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;">
<pre><strong>输入:</strong>n = 4, edges = [[0,1],[0,2]]
<strong>输出:</strong>-1
<strong>解释:</strong>图中不存在循环
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= edges.length &lt;= 1000</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>不存在重复的边</li>
</ul>

@ -0,0 +1,50 @@
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;,一个字符&nbsp;<strong>互不相同</strong>&nbsp;的字符串&nbsp;<code>chars</code>&nbsp;和一个长度与 <code>chars</code>&nbsp;相同的整数数组&nbsp;<code>vals</code>&nbsp;</p>
<p><strong>子字符串的开销</strong>&nbsp;是一个子字符串中所有字符对应价值之和。空字符串的开销是 <code>0</code>&nbsp;</p>
<p><strong>字符的价值</strong>&nbsp;定义如下:</p>
<ul>
<li>如果字符不在字符串&nbsp;<code>chars</code>&nbsp;中,那么它的价值是它在字母表中的位置(下标从 <strong>1</strong>&nbsp;开始)。
<ul>
<li>比方说,<code>'a'</code>&nbsp;的价值为&nbsp;<code>1</code>&nbsp;<code>'b'</code>&nbsp;的价值为&nbsp;<code>2</code>&nbsp;,以此类推,<code>'z'</code>&nbsp;的价值为&nbsp;<code>26</code>&nbsp;</li>
</ul>
</li>
<li>否则,如果这个字符在 <code>chars</code>&nbsp;中的位置为 <code>i</code>&nbsp;,那么它的价值就是&nbsp;<code>vals[i]</code>&nbsp;</li>
</ul>
<p>请你返回字符串 <code>s</code>&nbsp;的所有子字符串中的最大开销。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>s = "adaa", chars = "d", vals = [-1000]
<b>输出:</b>2
<b>解释:</b>字符 "a" 和 "d" 的价值分别为 1 和 -1000 。
最大开销子字符串是 "aa" ,它的开销为 1 + 1 = 2 。
2 是最大开销。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>s = "abc", chars = "abc", vals = [-1,-1,-1]
<b>输出:</b>0
<b>解释:</b>字符 "a" "b" 和 "c" 的价值分别为 -1 -1 和 -1 。
最大开销子字符串是 "" ,它的开销为 0 。
0 是最大开销。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
<li><code>1 &lt;= chars.length &lt;= 26</code></li>
<li><code>chars</code>&nbsp;只包含小写英文字母,且 <strong>互不相同</strong>&nbsp;</li>
<li><code>vals.length == chars.length</code></li>
<li><code>-1000 &lt;= vals[i] &lt;= 1000</code></li>
</ul>

@ -0,0 +1,57 @@
<p>给你一个整数&nbsp;<code>n</code>&nbsp;和一个在范围 <code>[0, n - 1]</code>&nbsp;以内的整数&nbsp;<code>p</code>&nbsp;,它们表示一个长度为 <code>n</code> 且下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>arr</code>&nbsp;,数组中除了下标为&nbsp;<code>p</code>&nbsp;处是 <code>1</code>&nbsp;以外,其他所有数都是 <code>0</code>&nbsp;</p>
<p>同时给你一个整数数组&nbsp;<code>banned</code>&nbsp;,它包含数组中的一些位置。<code>banned</code>&nbsp;中第&nbsp;<strong>i</strong>&nbsp;个位置表示&nbsp;<code>arr[banned[i]] = 0</code>&nbsp;,题目保证&nbsp;<code>banned[i] != p</code>&nbsp;</p>
<p>你可以对 <code>arr</code>&nbsp;进行 <strong>若干次</strong>&nbsp;操作。一次操作中,你选择大小为 <code>k</code>&nbsp;的一个 <strong>子数组</strong>&nbsp;,并将它 <b>翻转</b>&nbsp;。在任何一次翻转操作后,你都需要确保 <code>arr</code>&nbsp;中唯一的 <code>1</code>&nbsp;不会到达任何 <code>banned</code>&nbsp;中的位置。换句话说,<code>arr[banned[i]]</code>&nbsp;始终&nbsp;<strong>保持</strong>&nbsp;<code>0</code>&nbsp;</p>
<p>请你返回一个数组&nbsp;<code>ans</code>&nbsp;,对于<em>&nbsp;</em><code>[0, n - 1]</code>&nbsp;之间的任意下标&nbsp;<code>i</code>&nbsp;<code>ans[i]</code>&nbsp;是将&nbsp;<code>1</code>&nbsp;放到位置&nbsp;<code>i</code>&nbsp;处的&nbsp;<strong>最少</strong>&nbsp;翻转操作次数,如果无法放到位置&nbsp;<code>i</code>&nbsp;处,此数为&nbsp;<code>-1</code>&nbsp;</p>
<ul>
<li><strong>子数组</strong>&nbsp;指的是一个数组里一段连续 <strong>非空</strong>&nbsp;的元素序列。</li>
<li>对于所有的 <code>i</code>&nbsp;<code>ans[i]</code>&nbsp;相互之间独立计算。</li>
<li>将一个数组中的元素 <strong>翻转</strong> 指的是将数组中的值变成 <strong>相反顺序</strong>&nbsp;</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>n = 4, p = 0, banned = [1,2], k = 4
<b>输出:</b>[0,-1,-1,1]
<b>解释:</b><code>k = 4所以只有一种可行的翻转操作就是将整个数组翻转。一开始 </code>1<strong> </strong>在位置 0 处,所以将它翻转到位置 0 处需要的操作数为 0 。
我们不能将 1 翻转到 banned 中的位置,所以位置 1 和 2 处的答案都是 -1 。
通过一次翻转操作,可以将 1 放到位置 3 处,所以位置 3 的答案是 1 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>n = 5, p = 0, banned = [2,4], k = 3
<b>输出:</b>[0,-1,-1,-1,-1]
<b>解释:</b>这个例子中 1 一开始在位置 0 处,所以此下标的答案为 0 。
翻转的子数组长度为 k = 3 1 此时在位置 0 处,所以我们可以翻转子数组 [0, 2],但翻转后的下标 2 在 banned 中,所以不能执行此操作。
由于 1 没法离开位置 0 ,所以其他位置的答案都是 -1 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>n = 4, p = 2, banned = [0,1,3], k = 1
<b>输出:</b>[-1,-1,0,-1]
<b>解释:</b>这个例子中,我们只能对长度为 1 的子数组执行翻转操作,所以 1 无法离开初始位置。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= p &lt;= n - 1</code></li>
<li><code>0 &lt;= banned.length &lt;= n - 1</code></li>
<li><code>0 &lt;= banned[i] &lt;= n - 1</code></li>
<li><code>1 &lt;= k &lt;= n&nbsp;</code></li>
<li><code>banned[i] != p</code></li>
<li><code>banned</code>&nbsp;中的值 <strong>互不相同</strong>&nbsp;</li>
</ul>

@ -0,0 +1,42 @@
<p>给你一个仅由 <code>0</code><code>1</code> 组成的二进制字符串 <code>s</code><span style="">&nbsp;</span><span style="">&nbsp;</span></p>
<p>如果子字符串中 <strong>所有的<span style=""> </span></strong><code><span style="">0</span></code><strong><span style=""> </span>都在 </strong><code>1</code><strong> 之前</strong> 且其中 <code>0</code> 的数量等于 <code>1</code> 的数量,则认为 <code>s</code> 的这个子字符串是平衡子字符串。请注意,空子字符串也视作平衡子字符串。<span style="">&nbsp;</span></p>
<p>返回&nbsp;<span style=""> </span><code>s</code> 中最长的平衡子字符串长度。</p>
<p>子字符串是字符串中的一个连续字符序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "01000111"
<strong>输出:</strong>6
<strong>解释:</strong>最长的平衡子字符串是 "000111" ,长度为 6 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "00111"
<strong>输出:</strong>4
<strong>解释:</strong>最长的平衡子字符串是 "0011" ,长度为 <span style="">&nbsp;</span>4 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s = "111"
<strong>输出:</strong>0
<strong>解释:</strong>除了空子字符串之外不存在其他平衡子字符串,所以答案为 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>'0' &lt;= s[i] &lt;= '1'</code></li>
</ul>

@ -0,0 +1,44 @@
<p>有两只老鼠和&nbsp;<code>n</code>&nbsp;块不同类型的奶酪,每块奶酪都只能被其中一只老鼠吃掉。</p>
<p>下标为 <code>i</code>&nbsp;处的奶酪被吃掉的得分为:</p>
<ul>
<li>如果第一只老鼠吃掉,则得分为&nbsp;<code>reward1[i]</code>&nbsp;</li>
<li>如果第二只老鼠吃掉,则得分为&nbsp;<code>reward2[i]</code>&nbsp;</li>
</ul>
<p>给你一个正整数数组&nbsp;<code>reward1</code>&nbsp;,一个正整数数组&nbsp;<code>reward2</code>&nbsp;,和一个非负整数&nbsp;<code>k</code>&nbsp;</p>
<p>请你返回第一只老鼠恰好吃掉 <code>k</code>&nbsp;块奶酪的情况下,<strong>最大</strong>&nbsp;得分为多少。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2
<b>输出:</b>15
<b>解释:</b>这个例子中,第一只老鼠吃掉第 2&nbsp;和 3 块奶酪(下标从 0 开始),第二只老鼠吃掉第 0 和 1 块奶酪。
总得分为 4 + 4 + 3 + 4 = 15 。
15 是最高得分。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>reward1 = [1,1], reward2 = [1,1], k = 2
<b>输出:</b>2
<b>解释:</b>这个例子中,第一只老鼠吃掉第 0 和 1 块奶酪(下标从 0 开始),第二只老鼠不吃任何奶酪。
总得分为 1 + 1 = 2 。
2 是最高得分。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n == reward1.length == reward2.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= reward1[i],&nbsp;reward2[i] &lt;= 1000</code></li>
<li><code>0 &lt;= k &lt;= n</code></li>
</ul>

@ -0,0 +1,40 @@
<p>给你一个整数数组 <code>nums</code> 。请你创建一个满足以下条件的二维数组:</p>
<ul>
<li>二维数组应该 <strong></strong> 包含数组 <code>nums</code> 中的元素。</li>
<li>二维数组中的每一行都包含 <strong>不同</strong> 的整数。</li>
<li>二维数组的行数应尽可能 <strong></strong></li>
</ul>
<p>返回结果数组。如果存在多种答案,则返回其中任何一种。</p>
<p>请注意,二维数组的每一行上可以存在不同数量的元素。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [1,3,4,1,2,3,1]
<strong>输出:</strong>[[1,3,4,2],[1,3],[1]]
<strong>解释:</strong>根据题目要求可以创建包含以下几行元素的二维数组:
- 1,3,4,2
- 1,3
- 1
nums 中的所有元素都有用到,并且每一行都由不同的整数组成,所以这是一个符合题目要求的答案。
可以证明无法创建少于三行且符合题目要求的二维数组。</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>[[4,3,2,1]]
<strong>解释:</strong>nums 中的所有元素都不同,所以我们可以将其全部保存在二维数组中的第一行。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
</ul>

@ -0,0 +1,26 @@
Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7]
<strong>Output:</strong> 15
<strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 9</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
<li>All digits in each array are <strong>unique</strong>.</li>
</ul>

@ -0,0 +1,46 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code> and an integer <code>k</code>. The array <code>arr</code> is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.</p>
<p>You can do the following operation any number of times:</p>
<ul>
<li>Pick any element from <code>arr</code> and increase or decrease it by <code>1</code>.</li>
</ul>
<p>Return <em>the minimum number of operations such that the sum of each <strong>subarray</strong> of length </em><code>k</code><em> is equal</em>.</p>
<p>A <strong>subarray</strong> is a contiguous part of the array.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,4,1,3], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> we can do one operation on index 1 to make its value equal to 3.
The array after the operation is [1,3,1,3]
- Subarray starts at index 0 is [1, 3], and its sum is 4
- Subarray starts at index 1 is [3, 1], and its sum is 4
- Subarray starts at index 2 is [1, 3], and its sum is 4
- Subarray starts at index 3 is [3, 1], and its sum is 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [2,5,5,7], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
The array after the operations is [5,5,5,5]
- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
- Subarray starts at index 3 is [5, 5, 5], and its sum is 15
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>

@ -0,0 +1,34 @@
<p>There is a <strong>bi-directional </strong>graph with <code>n</code> vertices, where each vertex is labeled from <code>0</code> to <code>n - 1</code>. The edges in the graph are represented by a given 2D integer array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes an edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.</p>
<p>Return <em>the length of the <strong>shortest </strong>cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p>
<p>A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The cycle with the smallest length is : 0 -&gt; 1 -&gt; 2 -&gt; 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[0,1],[0,2]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no cycles in this graph.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= edges.length &lt;= 1000</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>There are no repeated edges.</li>
</ul>

@ -0,0 +1,50 @@
<p>You are given a string <code>s</code>, a string <code>chars</code> of <strong>distinct</strong> characters and an integer array <code>vals</code> of the same length as <code>chars</code>.</p>
<p>The <strong>cost of the substring </strong>is the sum of the values of each character in the substring. The cost of an empty string is considered <code>0</code>.</p>
<p>The <strong>value of the character </strong>is defined in the following way:</p>
<ul>
<li>If the character is not in the string <code>chars</code>, then its value is its corresponding position <strong>(1-indexed)</strong> in the alphabet.
<ul>
<li>For example, the value of <code>&#39;a&#39;</code> is <code>1</code>, the value of <code>&#39;b&#39;</code> is <code>2</code>, and so on. The value of <code>&#39;z&#39;</code> is <code>26</code>.</li>
</ul>
</li>
<li>Otherwise, assuming <code>i</code> is the index where the character occurs in the string <code>chars</code>, then its value is <code>vals[i]</code>.</li>
</ul>
<p>Return <em>the maximum cost among all substrings of the string</em> <code>s</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;adaa&quot;, chars = &quot;d&quot;, vals = [-1000]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The value of the characters &quot;a&quot; and &quot;d&quot; is 1 and -1000 respectively.
The substring with the maximum cost is &quot;aa&quot; and its cost is 1 + 1 = 2.
It can be proven that 2 is the maximum cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;abc&quot;, chars = &quot;abc&quot;, vals = [-1,-1,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The value of the characters &quot;a&quot;, &quot;b&quot; and &quot;c&quot; is -1, -1, and -1 respectively.
The substring with the maximum cost is the empty substring &quot;&quot; and its cost is 0.
It can be proven that 0 is the maximum cost.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consist of lowercase English letters.</li>
<li><code>1 &lt;= chars.length &lt;= 26</code></li>
<li><code>chars</code> consist of <strong>distinct</strong> lowercase English letters.</li>
<li><code>vals.length == chars.length</code></li>
<li><code>-1000 &lt;= vals[i] &lt;= 1000</code></li>
</ul>

@ -0,0 +1,51 @@
<p>You are given an integer <code>n</code> and an integer <code>p</code> in the range <code>[<font face="monospace">0</font>, n - 1]</code>. Representing a <strong>0-indexed</strong> array <code>arr</code>&nbsp;of length <code>n</code> where all positions are set to <code>0</code>&#39;s, except position <code>p</code> which is set to <code>1</code>.</p>
<p>You are also given an integer array <code>banned</code> containing some positions from the array. For the <strong>i</strong><sup><strong>th</strong></sup> position in <code>banned</code>, <code>arr[banned[i]] = 0</code>, and <code>banned[i] != p</code>.</p>
<p>You can perform <strong>multiple</strong> operations on <code>arr</code>. In an operation, you can choose a <strong>subarray</strong> with size <code>k</code> and <strong>reverse</strong> the subarray. However, the <code>1</code> in <code>arr</code> should never go to any of the positions in <code>banned</code>. In other words, after each operation <code>arr[banned[i]]</code> <strong>remains</strong> <code>0</code>.</p>
<p><em>Return an array</em> <code>ans</code> <em>where</em><em> for each </em><code>i</code><em> from </em><code>[0, n - 1]</code>, <code>ans[i]</code> <em>is the <strong>minimum</strong> number of reverse operations needed to bring the</em> <code>1</code> <em>to position</em> <code>i</code><em> in arr</em>, <em>or</em> <code>-1</code> <em>if it is impossible</em>.</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</li>
<li>The values of <code>ans[i]</code> are independent for all <code>i</code>&#39;s.</li>
<li>The <strong>reverse </strong>of an array is an array containing the values in <strong>reverse order</strong>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 4, p = 0, banned = [1,2], k = 4
<strong>Output:</strong> [0,-1,-1,1]
<strong>Explanation:</strong> In this case <code>k = 4</code> so there is only one possible reverse operation we can perform, which is reversing the whole array. Initially, 1<strong> </strong>is placed at position 0 so the amount of operations we need for position 0 is <code>0</code>. We can never place a 1 on the banned positions, so the answer for positions 1 and 2 is <code>-1</code>. Finally, with one reverse operation we can bring the 1 to index 3, so the answer for position 3 is <code>1</code>.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 5, p = 0, banned = [2,4], k = 3
<strong>Output:</strong> [0,-1,-1,-1,-1]
<strong>Explanation:</strong> In this case the 1 is initially at position 0, so the answer for that position is <code>0</code>. We can perform reverse operations of size 3. The 1 is currently located at position 0, so we need to reverse the subarray <code>[0, 2]</code> for it to leave that position, but reversing that subarray makes position 2 have a 1, which shouldn&#39;t happen. So, we can&#39;t move the 1 from position 0, making the result for all the other positions <code>-1</code>.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4, p = 2, banned = [0,1,3], k = 1
<strong>Output:</strong> [-1,-1,0,-1]
<strong>Explanation:</strong> In this case we can only perform reverse operations of size 1.<strong>&nbsp;</strong>So the 1 never changes its position.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= p &lt;= n - 1</code></li>
<li><code>0 &lt;= banned.length &lt;= n - 1</code></li>
<li><code>0 &lt;= banned[i] &lt;= n - 1</code></li>
<li><code>1 &lt;= k &lt;= n&nbsp;</code></li>
<li><code>banned[i] != p</code></li>
<li>all values in <code>banned</code>&nbsp;are <strong>unique</strong>&nbsp;</li>
</ul>

@ -0,0 +1,40 @@
<p>You are given a binary string <code>s</code> consisting only of zeroes and ones.</p>
<p>A substring of <code>s</code> is considered balanced if<strong> all zeroes are before ones</strong> and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.</p>
<p>Return <em>the length of the longest balanced substring of </em><code>s</code>.</p>
<p>A <b>substring</b> is a contiguous sequence of characters within a string.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;01000111&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> The longest balanced substring is &quot;000111&quot;, which has length 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;00111&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest balanced substring is &quot;0011&quot;, which has length 4.&nbsp;
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;111&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no balanced substring except the empty substring, so the answer is 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>&#39;0&#39; &lt;= s[i] &lt;= &#39;1&#39;</code></li>
</ul>

@ -0,0 +1,42 @@
<p>There are two mice and <code>n</code> different types of cheese, each type of cheese should be eaten by exactly one mouse.</p>
<p>A point of the cheese with index <code>i</code> (<strong>0-indexed</strong>) is:</p>
<ul>
<li><code>reward1[i]</code> if the first mouse eats it.</li>
<li><code>reward2[i]</code> if the second mouse eats it.</li>
</ul>
<p>You are given a positive integer array <code>reward1</code>, a positive integer array <code>reward2</code>, and a non-negative integer <code>k</code>.</p>
<p>Return <em><strong>the maximum</strong> points the mice can achieve if the first mouse eats exactly </em><code>k</code><em> types of cheese.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2
<strong>Output:</strong> 15
<strong>Explanation:</strong> In this example, the first mouse eats the 2<sup>nd</sup>&nbsp;(0-indexed) and the 3<sup>rd</sup>&nbsp;types of cheese, and the second mouse eats the 0<sup>th</sup>&nbsp;and the 1<sup>st</sup> types of cheese.
The total points are 4 + 4 + 3 + 4 = 15.
It can be proven that 15 is the maximum total points that the mice can achieve.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> reward1 = [1,1], reward2 = [1,1], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example, the first mouse eats the 0<sup>th</sup>&nbsp;(0-indexed) and 1<sup>st</sup>&nbsp;types of cheese, and the second mouse does not eat any cheese.
The total points are 1 + 1 = 2.
It can be proven that 2 is the maximum total points that the mice can achieve.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == reward1.length == reward2.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= reward1[i],&nbsp;reward2[i] &lt;= 1000</code></li>
<li><code>0 &lt;= k &lt;= n</code></li>
</ul>

@ -0,0 +1,40 @@
<p>You are given an integer array <code>nums</code>. You need to create a 2D array from <code>nums</code> satisfying the following conditions:</p>
<ul>
<li>The 2D array should contain <strong>only</strong> the elements of the array <code>nums</code>.</li>
<li>Each row in the 2D array contains <strong>distinct</strong> integers.</li>
<li>The number of rows in the 2D array should be <strong>minimal</strong>.</li>
</ul>
<p>Return <em>the resulting array</em>. If there are multiple answers, return any of them.</p>
<p><strong>Note</strong> that the 2D array can have a different number of elements on each row.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,4,1,2,3,1]
<strong>Output:</strong> [[1,3,4,2],[1,3],[1]]
<strong>Explanation:</strong> We can create a 2D array that contains the following rows:
- 1,3,4,2
- 1,3
- 1
All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
It can be shown that we cannot have less than 3 rows in a valid array.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [[4,3,2,1]]
<strong>Explanation:</strong> All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
<li><code>1 &lt;= nums[i] &lt;= nums.length</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

@ -0,0 +1,40 @@
<p>You are given an integer array <code>nums</code>. You need to create a 2D array from <code>nums</code> satisfying the following conditions:</p>
<ul>
<li>The 2D array should contain <strong>only</strong> the elements of the array <code>nums</code>.</li>
<li>Each row in the 2D array contains <strong>distinct</strong> integers.</li>
<li>The number of rows in the 2D array should be <strong>minimal</strong>.</li>
</ul>
<p>Return <em>the resulting array</em>. If there are multiple answers, return any of them.</p>
<p><strong>Note</strong> that the 2D array can have a different number of elements on each row.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,4,1,2,3,1]
<strong>Output:</strong> [[1,3,4,2],[1,3],[1]]
<strong>Explanation:</strong> We can create a 2D array that contains the following rows:
- 1,3,4,2
- 1,3
- 1
All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
It can be shown that we cannot have less than 3 rows in a valid array.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [[4,3,2,1]]
<strong>Explanation:</strong> All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
</ul>

@ -0,0 +1,40 @@
<p>You are given a binary string <code>s</code> consisting only of zeroes and ones.</p>
<p>A substring of <code>s</code> is considered balanced if<strong> all zeroes are before ones</strong> and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.</p>
<p>Return <em>the length of the longest balanced substring of </em><code>s</code>.</p>
<p>A <b>substring</b> is a contiguous sequence of characters within a string.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;01000111&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> The longest balanced substring is &quot;000111&quot;, which has length 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;00111&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest balanced substring is &quot;0011&quot;, which has length 4.&nbsp;
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;111&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no balanced substring except the empty substring, so the answer is 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>&#39;0&#39; &lt;= s[i] &lt;= &#39;1&#39;</code></li>
</ul>

@ -0,0 +1,50 @@
<p>You are given a string <code>s</code>, a string <code>chars</code> of <strong>distinct</strong> characters and an integer array <code>vals</code> of the same length as <code>chars</code>.</p>
<p>The <strong>cost of the substring </strong>is the sum of the values of each character in the substring. The cost of an empty string is considered <code>0</code>.</p>
<p>The <strong>value of the character </strong>is defined in the following way:</p>
<ul>
<li>If the character is not in the string <code>chars</code>, then its value is its corresponding position <strong>(1-indexed)</strong> in the alphabet.
<ul>
<li>For example, the value of <code>&#39;a&#39;</code> is <code>1</code>, the value of <code>&#39;b&#39;</code> is <code>2</code>, and so on. The value of <code>&#39;z&#39;</code> is <code>26</code>.</li>
</ul>
</li>
<li>Otherwise, assuming <code>i</code> is the index where the character occurs in the string <code>chars</code>, then its value is <code>vals[i]</code>.</li>
</ul>
<p>Return <em>the maximum cost among all substrings of the string</em> <code>s</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;adaa&quot;, chars = &quot;d&quot;, vals = [-1000]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The value of the characters &quot;a&quot; and &quot;d&quot; is 1 and -1000 respectively.
The substring with the maximum cost is &quot;aa&quot; and its cost is 1 + 1 = 2.
It can be proven that 2 is the maximum cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;abc&quot;, chars = &quot;abc&quot;, vals = [-1,-1,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The value of the characters &quot;a&quot;, &quot;b&quot; and &quot;c&quot; is -1, -1, and -1 respectively.
The substring with the maximum cost is the empty substring &quot;&quot; and its cost is 0.
It can be proven that 0 is the maximum cost.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s</code> consist of lowercase English letters.</li>
<li><code>1 &lt;= chars.length &lt;= 26</code></li>
<li><code>chars</code> consist of <strong>distinct</strong> lowercase English letters.</li>
<li><code>vals.length == chars.length</code></li>
<li><code>-1000 &lt;= vals[i] &lt;= 1000</code></li>
</ul>

@ -0,0 +1,26 @@
Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7]
<strong>Output:</strong> 15
<strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 9</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
<li>All digits in each array are <strong>unique</strong>.</li>
</ul>

@ -0,0 +1,46 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code> and an integer <code>k</code>. The array <code>arr</code> is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.</p>
<p>You can do the following operation any number of times:</p>
<ul>
<li>Pick any element from <code>arr</code> and increase or decrease it by <code>1</code>.</li>
</ul>
<p>Return <em>the minimum number of operations such that the sum of each <strong>subarray</strong> of length </em><code>k</code><em> is equal</em>.</p>
<p>A <strong>subarray</strong> is a contiguous part of the array.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,4,1,3], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> we can do one operation on index 1 to make its value equal to 3.
The array after the operation is [1,3,1,3]
- Subarray starts at index 0 is [1, 3], and its sum is 4
- Subarray starts at index 1 is [3, 1], and its sum is 4
- Subarray starts at index 2 is [1, 3], and its sum is 4
- Subarray starts at index 3 is [3, 1], and its sum is 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [2,5,5,7], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
The array after the operations is [5,5,5,5]
- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
- Subarray starts at index 3 is [5, 5, 5], and its sum is 15
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>

@ -0,0 +1,42 @@
<p>There are two mice and <code>n</code> different types of cheese, each type of cheese should be eaten by exactly one mouse.</p>
<p>A point of the cheese with index <code>i</code> (<strong>0-indexed</strong>) is:</p>
<ul>
<li><code>reward1[i]</code> if the first mouse eats it.</li>
<li><code>reward2[i]</code> if the second mouse eats it.</li>
</ul>
<p>You are given a positive integer array <code>reward1</code>, a positive integer array <code>reward2</code>, and a non-negative integer <code>k</code>.</p>
<p>Return <em><strong>the maximum</strong> points the mice can achieve if the first mouse eats exactly </em><code>k</code><em> types of cheese.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2
<strong>Output:</strong> 15
<strong>Explanation:</strong> In this example, the first mouse eats the 2<sup>nd</sup>&nbsp;(0-indexed) and the 3<sup>rd</sup>&nbsp;types of cheese, and the second mouse eats the 0<sup>th</sup>&nbsp;and the 1<sup>st</sup> types of cheese.
The total points are 4 + 4 + 3 + 4 = 15.
It can be proven that 15 is the maximum total points that the mice can achieve.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> reward1 = [1,1], reward2 = [1,1], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example, the first mouse eats the 0<sup>th</sup>&nbsp;(0-indexed) and 1<sup>st</sup>&nbsp;types of cheese, and the second mouse does not eat any cheese.
The total points are 1 + 1 = 2.
It can be proven that 2 is the maximum total points that the mice can achieve.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == reward1.length == reward2.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= reward1[i],&nbsp;reward2[i] &lt;= 1000</code></li>
<li><code>0 &lt;= k &lt;= n</code></li>
</ul>

@ -0,0 +1,51 @@
<p>You are given an integer <code>n</code> and an integer <code>p</code> in the range <code>[<font face="monospace">0</font>, n - 1]</code>. Representing a <strong>0-indexed</strong> array <code>arr</code>&nbsp;of length <code>n</code> where all positions are set to <code>0</code>&#39;s, except position <code>p</code> which is set to <code>1</code>.</p>
<p>You are also given an integer array <code>banned</code> containing some positions from the array. For the <strong>i</strong><sup><strong>th</strong></sup> position in <code>banned</code>, <code>arr[banned[i]] = 0</code>, and <code>banned[i] != p</code>.</p>
<p>You can perform <strong>multiple</strong> operations on <code>arr</code>. In an operation, you can choose a <strong>subarray</strong> with size <code>k</code> and <strong>reverse</strong> the subarray. However, the <code>1</code> in <code>arr</code> should never go to any of the positions in <code>banned</code>. In other words, after each operation <code>arr[banned[i]]</code> <strong>remains</strong> <code>0</code>.</p>
<p><em>Return an array</em> <code>ans</code> <em>where</em><em> for each </em><code>i</code><em> from </em><code>[0, n - 1]</code>, <code>ans[i]</code> <em>is the <strong>minimum</strong> number of reverse operations needed to bring the</em> <code>1</code> <em>to position</em> <code>i</code><em> in arr</em>, <em>or</em> <code>-1</code> <em>if it is impossible</em>.</p>
<ul>
<li>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</li>
<li>The values of <code>ans[i]</code> are independent for all <code>i</code>&#39;s.</li>
<li>The <strong>reverse </strong>of an array is an array containing the values in <strong>reverse order</strong>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 4, p = 0, banned = [1,2], k = 4
<strong>Output:</strong> [0,-1,-1,1]
<strong>Explanation:</strong> In this case <code>k = 4</code> so there is only one possible reverse operation we can perform, which is reversing the whole array. Initially, 1<strong> </strong>is placed at position 0 so the amount of operations we need for position 0 is <code>0</code>. We can never place a 1 on the banned positions, so the answer for positions 1 and 2 is <code>-1</code>. Finally, with one reverse operation we can bring the 1 to index 3, so the answer for position 3 is <code>1</code>.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 5, p = 0, banned = [2,4], k = 3
<strong>Output:</strong> [0,-1,-1,-1,-1]
<strong>Explanation:</strong> In this case the 1 is initially at position 0, so the answer for that position is <code>0</code>. We can perform reverse operations of size 3. The 1 is currently located at position 0, so we need to reverse the subarray <code>[0, 2]</code> for it to leave that position, but reversing that subarray makes position 2 have a 1, which shouldn&#39;t happen. So, we can&#39;t move the 1 from position 0, making the result for all the other positions <code>-1</code>.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4, p = 2, banned = [0,1,3], k = 1
<strong>Output:</strong> [-1,-1,0,-1]
<strong>Explanation:</strong> In this case we can only perform reverse operations of size 1.<strong>&nbsp;</strong>So the 1 never changes its position.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= p &lt;= n - 1</code></li>
<li><code>0 &lt;= banned.length &lt;= n - 1</code></li>
<li><code>0 &lt;= banned[i] &lt;= n - 1</code></li>
<li><code>1 &lt;= k &lt;= n&nbsp;</code></li>
<li><code>banned[i] != p</code></li>
<li>all values in <code>banned</code>&nbsp;are <strong>unique</strong>&nbsp;</li>
</ul>

@ -0,0 +1,34 @@
<p>There is a <strong>bi-directional </strong>graph with <code>n</code> vertices, where each vertex is labeled from <code>0</code> to <code>n - 1</code>. The edges in the graph are represented by a given 2D integer array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes an edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.</p>
<p>Return <em>the length of the <strong>shortest </strong>cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p>
<p>A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The cycle with the smallest length is : 0 -&gt; 1 -&gt; 2 -&gt; 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[0,1],[0,2]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no cycles in this graph.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= edges.length &lt;= 1000</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>There are no repeated edges.</li>
</ul>