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:
程序员小墨 2022-11-14 20:01:29 +08:00
parent 932b731690
commit 8a37a26300
44 changed files with 16150 additions and 11965 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,50 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <strong>偶数</strong>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;</p>
<p>只要&nbsp;<code>nums</code> <strong>不是</strong>&nbsp;空数组,你就重复执行以下步骤:</p>
<ul>
<li>找到&nbsp;<code>nums</code>&nbsp;中的最小值,并删除它。</li>
<li>找到&nbsp;<code>nums</code>&nbsp;中的最大值,并删除它。</li>
<li>计算删除两数的平均值。</li>
</ul>
<p>两数 <code>a</code>&nbsp;<code>b</code>&nbsp;<strong>平均值</strong>&nbsp;&nbsp;<code>(a + b) / 2</code>&nbsp;</p>
<ul>
<li>比方说,<code>2</code>&nbsp;&nbsp;<code>3</code>&nbsp;的平均值是&nbsp;<code>(2 + 3) / 2 = 2.5</code>&nbsp;</li>
</ul>
<p>返回上述过程能得到的 <strong>不同</strong>&nbsp;平均值的数目。</p>
<p><strong>注意</strong>&nbsp;,如果最小值或者最大值有重复元素,可以删除任意一个。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>nums = [4,1,4,0,3,5]
<b>输出:</b>2
<strong>解释:</strong>
1. 删除 0 和 5 ,平均值是 (0 + 5) / 2 = 2.5 ,现在 nums = [4,1,4,3] 。
2. 删除 1 和 4 ,平均值是 (1 + 4) / 2 = 2.5 ,现在 nums = [4,3] 。
3. 删除 3 和 4 ,平均值是 (3 + 4) / 2 = 3.5 。
2.5 2.5 和 3.5 之中总共有 2 个不同的数,我们返回 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums = [1,100]
<b>输出:</b>1
<strong>解释:</strong>
删除 1 和 100 后只有一个平均值,所以我们返回 1 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>nums.length</code>&nbsp;是偶数。</li>
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@ -0,0 +1,40 @@
<p>给你一个字符串 <code>s</code> 和一个 <strong></strong> 整数 <code>k</code></p>
<p>从字符串 <code>s</code> 中选出一组满足下述条件且 <strong>不重叠</strong> 的子字符串:</p>
<ul>
<li>每个子字符串的长度 <strong>至少</strong><code>k</code></li>
<li>每个子字符串是一个 <strong>回文串</strong></li>
</ul>
<p>返回最优方案中能选择的子字符串的 <strong>最大</strong> 数目。</p>
<p><strong>子字符串</strong> 是字符串中一个连续的字符序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1 </strong></p>
<pre>
<strong>输入:</strong>s = "abaccdbbd", k = 3
<strong>输出:</strong>2
<strong>解释:</strong>可以选择 s = "<em><strong>aba</strong></em>cc<em><strong>dbbd</strong></em>" 中斜体加粗的子字符串。"aba" 和 "dbbd" 都是回文,且长度至少为 k = 3 。
可以证明,无法选出两个以上的有效子字符串。
</pre>
<p><strong>示例 2 </strong></p>
<pre>
<strong>输入:</strong>s = "adbcda", k = 2
<strong>输出:</strong>0
<strong>解释:</strong>字符串中不存在长度至少为 2 的回文子字符串。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= s.length &lt;= 2000</code></li>
<li><code>s</code> 仅由小写英文字母组成</li>
</ul>

View File

@ -0,0 +1,34 @@
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,请你统计并返回 <code>nums</code><strong>子数组</strong> 中满足 <em>元素最小公倍数为 <code>k</code> </em>的子数组数目。</p>
<p><strong>子数组</strong> 是数组中一个连续非空的元素序列。</p>
<p><strong>数组的最小公倍数</strong> 是可被所有数组元素整除的最小正整数。</p>
<p>&nbsp;</p>
<p><strong>示例 1 </strong></p>
<pre><strong>输入:</strong>nums = [3,6,2,7,1], k = 6
<strong>输出:</strong>4
<strong>解释:</strong>以 6 为最小公倍数的子数组是:
- [<em><strong>3</strong></em>,<em><strong>6</strong></em>,2,7,1]
- [<em><strong>3</strong></em>,<em><strong>6</strong></em>,<em><strong>2</strong></em>,7,1]
- [3,<em><strong>6</strong></em>,2,7,1]
- [3,<em><strong>6</strong></em>,<em><strong>2</strong></em>,7,1]
</pre>
<p><strong>示例 2 </strong></p>
<pre><strong>输入:</strong>nums = [3], k = 2
<strong>输出:</strong>0
<strong>解释:</strong>不存在以 2 为最小公倍数的子数组。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i], k &lt;= 1000</code></li>
</ul>

View File

@ -0,0 +1,73 @@
<p>一个 <code>n</code>&nbsp;个节点的无向树,节点编号为&nbsp;<code>0</code>&nbsp;&nbsp;<code>n - 1</code>&nbsp;,树的根结点是&nbsp;<code>0</code>&nbsp;号节点。给你一个长度为 <code>n - 1</code>&nbsp;的二维整数数组&nbsp;<code>edges</code>&nbsp;,其中&nbsp;<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;,表示节点&nbsp;<code>a<sub>i</sub></code>&nbsp;<code>b<sub>i</sub></code>&nbsp;在树中有一条边。</p>
<p>在每一个节点&nbsp;<code>i</code>&nbsp;处有一扇门。同时给你一个都是偶数的数组&nbsp;<code>amount</code>&nbsp;,其中&nbsp;<code>amount[i]</code>&nbsp;表示:</p>
<ul>
<li>如果 <code>amount[i]</code>&nbsp;的值是负数,那么它表示打开节点&nbsp;<code>i</code>&nbsp;处门扣除的分数。</li>
<li>如果 <code>amount[i]</code>&nbsp;的值是正数,那么它表示打开节点 <code>i</code>&nbsp;处门加上的分数。</li>
</ul>
<p>游戏按照如下规则进行:</p>
<ul>
<li>一开始Alice 在节点&nbsp;<code>0</code>&nbsp;Bob 在节点&nbsp;<code>bob</code>&nbsp;处。</li>
<li>每一秒钟Alice 和 Bob <strong>分别</strong>&nbsp;移动到相邻的节点。Alice 朝着某个&nbsp;<strong>叶子结点</strong>&nbsp;移动Bob 朝着节点&nbsp;<code>0</code>&nbsp;移动。</li>
<li>对于他们之间路径上的 <strong>每一个</strong>&nbsp;节点Alice 和 Bob 要么打开门并扣分,要么打开门并加分。注意:
<ul>
<li>如果门 <strong>已经打开</strong>&nbsp;(被另一个人打开),不会有额外加分也不会扣分。</li>
<li>如果&nbsp;Alice 和 Bob <strong>同时</strong>&nbsp;到达一个节点,他们会共享这个节点的加分或者扣分。换言之,如果打开这扇门扣&nbsp;<code>c</code>&nbsp;分,那么&nbsp;Alice 和 Bob 分别扣&nbsp;<code>c / 2</code>&nbsp;分。如果这扇门的加分为&nbsp;<code>c</code>&nbsp;,那么他们分别加&nbsp;<code>c / 2</code>&nbsp;分。</li>
</ul>
</li>
<li>如果 Alice 到达了一个叶子结点,她会停止移动。类似的,如果&nbsp;Bob 到达了节点&nbsp;<code>0</code>&nbsp;,他也会停止移动。注意这些事件互相 <strong>独立</strong>&nbsp;,不会影响另一方移动。</li>
</ul>
<p>请你返回&nbsp;Alice 朝最优叶子结点移动的 <strong>最大</strong>&nbsp;净得分。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;"></p>
<pre><b>输入:</b>edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
<b>输出:</b>6
<b>解释:</b>
上图展示了输入给出的一棵树。游戏进行如下:
- Alice 一开始在节点 0 处Bob 在节点 3 处。他们分别打开所在节点的门。
Alice 得分为 -2 。
- Alice 和 Bob 都移动到节点 1 。
&nbsp; 因为他们同时到达这个节点,他们一起打开门并平分得分。
&nbsp; Alice 的得分变为 -2 + (4 / 2) = 0 。
- Alice 移动到节点 3 。因为 Bob 已经打开了这扇门Alice 得分不变。
&nbsp; Bob 移动到节点 0 ,并停止移动。
- Alice 移动到节点 4 并打开这个节点的门,她得分变为 0 + 6 = 6 。
现在Alice 和 Bob 都不能进行任何移动了,所以游戏结束。
Alice 无法得到更高分数。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;"></p>
<pre><b>输入:</b>edges = [[0,1]], bob = 1, amount = [-7280,2350]
<b>输出:</b>-7280
<b>解释:</b>
Alice 按照路径 0-&gt;1 移动,同时 Bob 按照路径 1-&gt;0 移动。
所以 Alice 只打开节点 0 处的门,她的得分为 -7280 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code>&nbsp;表示一棵有效的树。</li>
<li><code>1 &lt;= bob &lt; n</code></li>
<li><code>amount.length == n</code></li>
<li><code>amount[i]</code>&nbsp;是范围&nbsp;<code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>&nbsp;之间的一个&nbsp;<strong>偶数</strong>&nbsp;</li>
</ul>

View File

@ -0,0 +1,40 @@
<p>给你一个字符串&nbsp;<code>message</code>&nbsp;和一个正整数&nbsp;<code>limit</code>&nbsp;</p>
<p>你需要根据 <code>limit</code>&nbsp;&nbsp;<code>message</code> <strong>分割</strong>&nbsp;成一个或多个 <strong>部分</strong>&nbsp;。每个部分的结尾都是&nbsp;<code>"&lt;a/b&gt;"</code>&nbsp;,其中&nbsp;<code>"b"</code>&nbsp;用分割出来的总数 <b>替换</b>&nbsp;<code>"a"</code>&nbsp;用当前部分所在的编号 <strong>替换</strong>&nbsp;,编号从&nbsp;<code>1</code>&nbsp;&nbsp;<code>b</code>&nbsp;依次编号。除此以外,除了最后一部分长度 <strong>小于等于</strong>&nbsp;<code>limit</code>&nbsp;以外,其他每一部分(包括结尾部分)的长度都应该&nbsp;<strong>等于</strong>&nbsp;<code>limit</code>&nbsp;</p>
<p>你需要确保分割后的结果数组,删掉每部分的结尾并<strong>&nbsp;按顺序&nbsp;</strong>连起来后,能够得到&nbsp;<code>message</code>&nbsp;。同时,结果数组越短越好。</p>
<p>请你返回<em>&nbsp;</em><code>message</code>&nbsp; 分割后得到的结果数组。如果无法按要求分割&nbsp;<code>message</code>&nbsp;,返回一个空数组。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>message = "this is really a very awesome message", limit = 9
<b>输出:</b>["thi&lt;1/14&gt;","s i&lt;2/14&gt;","s r&lt;3/14&gt;","eal&lt;4/14&gt;","ly &lt;5/14&gt;","a v&lt;6/14&gt;","ery&lt;7/14&gt;"," aw&lt;8/14&gt;","eso&lt;9/14&gt;","me&lt;10/14&gt;"," m&lt;11/14&gt;","es&lt;12/14&gt;","sa&lt;13/14&gt;","ge&lt;14/14&gt;"]
<strong>解释:</strong>
前面 9 个部分分别从 message 中得到 3 个字符。
接下来的 5 个部分分别从 message 中得到 2 个字符。
这个例子中,包含最后一个部分在内,每个部分的长度都为 9 。
可以证明没有办法分割成少于 14 个部分。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>message = "short message", limit = 15
<b>输出:</b>["short mess&lt;1/2&gt;","age&lt;2/2&gt;"]
<strong>解释:</strong>
在给定限制下,字符串可以分成两个部分:
- 第一个部分包含 10 个字符,长度为 15 。
- 第二个部分包含 3 个字符,长度为 8 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= message.length &lt;= 10<sup>4</sup></code></li>
<li><code>message</code>&nbsp;只包含小写英文字母和&nbsp;<code>' '</code>&nbsp;</li>
<li><code>1 &lt;= limit &lt;= 10<sup>4</sup></code></li>
</ul>

View File

@ -0,0 +1,35 @@
<p>给你一个四舍五入到两位小数的非负浮点数 <code>celsius</code> 来表示温度,以 <strong>摄氏度</strong><strong>Celsius</strong>)为单位。</p>
<p>你需要将摄氏度转换为 <strong>开氏度</strong><strong>Kelvin</strong>)和 <strong>华氏度</strong><strong>Fahrenheit</strong>),并以数组 <code>ans = [kelvin, fahrenheit]</code> 的形式返回结果。</p>
<p>返回数组<em> <code>ans</code></em> 。与实际答案误差不超过 <code>10<sup>-5</sup></code> 的会视为正确答案<strong></strong></p>
<p><strong>注意:</strong></p>
<ul>
<li><code>开氏度 = 摄氏度 + 273.15</code></li>
<li><code>华氏度 = 摄氏度 * 1.80 + 32.00</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1 </strong></p>
<pre><strong>输入:</strong>celsius = 36.50
<strong>输出:</strong>[309.65000,97.70000]
<strong>解释:</strong>36.50 摄氏度:转换为开氏度是 309.65 ,转换为华氏度是 97.70 。</pre>
<p><strong>示例 2 </strong></p>
<pre><strong>输入:</strong>celsius = 122.11
<strong>输出:</strong>[395.26000,251.79800]
<strong>解释:</strong>122.11 摄氏度:转换为开氏度是 395.26 ,转换为华氏度是 251.798 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= celsius &lt;= 1000</code></li>
</ul>

View File

@ -0,0 +1,40 @@
<p>给你整数&nbsp;<code>zero</code>&nbsp;<code>one</code>&nbsp;<code>low</code>&nbsp;&nbsp;<code>high</code>&nbsp;,我们从空字符串开始构造一个字符串,每一步执行下面操作中的一种:</p>
<ul>
<li>&nbsp;<code>'0'</code>&nbsp;在字符串末尾添加&nbsp;<code>zero</code>&nbsp; 次。</li>
<li>&nbsp;<code>'1'</code>&nbsp;在字符串末尾添加&nbsp;<code>one</code>&nbsp;次。</li>
</ul>
<p>以上操作可以执行任意次。</p>
<p>如果通过以上过程得到一个 <strong>长度</strong>&nbsp;&nbsp;<code>low</code>&nbsp;<code>high</code>&nbsp;之间(包含上下边界)的字符串,那么这个字符串我们称为&nbsp;<strong></strong>&nbsp;字符串。</p>
<p>请你返回满足以上要求的 <strong>不同</strong>&nbsp;好字符串数目。由于答案可能很大,请将结果对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;后返回。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>low = 3, high = 3, zero = 1, one = 1
<b>输出:</b>8
<b>解释:</b>
一个可能的好字符串是 "011" 。
可以这样构造得到:"" -&gt; "0" -&gt; "01" -&gt; "011" 。
从 "000" 到 "111" 之间所有的二进制字符串都是好字符串。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>low = 2, high = 3, zero = 1, one = 2
<b>输出:</b>5
<b>解释:</b>好字符串为 "00" "11" "000" "110" 和 "011" 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= low&nbsp;&lt;= high&nbsp;&lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= zero, one &lt;= low</code></li>
</ul>

View File

@ -0,0 +1,50 @@
<p>给你一个 <strong>值互不相同</strong> 的二叉树的根节点 <code>root</code></p>
<p>在一步操作中,你可以选择 <strong>同一层</strong> 上任意两个节点,交换这两个节点的值。</p>
<p>返回每一层按 <strong>严格递增顺序</strong> 排序所需的最少操作数目。</p>
<p>节点的 <strong>层数</strong> 是该节点和根节点之间的路径的边数。</p>
<p>&nbsp;</p>
<p><strong>示例 1 </strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;">
<pre><strong>输入:</strong>root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
<strong>输出:</strong>3
<strong>解释:</strong>
- 交换 4 和 3 。第 2 层变为 [3,4] 。
- 交换 7 和 5 。第 3 层变为 [5,6,8,7] 。
- 交换 8 和 7 。第 3 层变为 [5,6,7,8] 。
共计用了 3 步操作,所以返回 3 。
可以证明 3 是需要的最少操作数目。
</pre>
<p><strong>示例 2 </strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;">
<pre><strong>输入:</strong>root = [1,3,2,7,6,5,4]
<strong>输出:</strong>3
<strong>解释:
</strong>- 交换 3 和 2 。第 2 层变为 [2,3] 。
- 交换 7 和 4 。第 3 层变为 [4,6,5,7] 。
- 交换 6 和 5 。第 3 层变为 [4,5,6,7] 。
共计用了 3 步操作,所以返回 3 。
可以证明 3 是需要的最少操作数目。
</pre>
<p><strong>示例 3 </strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;">
<pre><strong>输入:</strong>root = [1,2,3,4,5,6]
<strong>输出:</strong>0
<strong>解释:</strong>每一层已经按递增顺序排序,所以返回 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点的数目在范围 <code>[1, 10<sup>5</sup>]</code></li>
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
<li>树中的所有值 <strong>互不相同</strong></li>
</ul>

View File

@ -0,0 +1,50 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
<ul>
<li>Find the minimum number in <code>nums</code> and remove it.</li>
<li>Find the maximum number in <code>nums</code> and remove it.</li>
<li>Calculate the average of the two removed numbers.</li>
</ul>
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
<ul>
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
</ul>
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,4,0,3,5]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,100]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
There is only one average to be calculated after removing 1 and 100, so we return 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>nums.length</code> is even.</li>
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
<ul>
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
<li>Each substring is a <strong>palindrome</strong>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
<p>A <strong>substring</strong> 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;abaccdbbd&quot;, k = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can select the substrings underlined in s = &quot;<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>&quot;. Both &quot;aba&quot; and &quot;dbbd&quot; are palindromes and have a length of at least k = 3.
It can be shown that we cannot find a selection with more than two valid substrings.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;adbcda&quot;, k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= s.length &lt;= 2000</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,34 @@
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>subarrays</strong> of </em><code>nums</code><em> where the least common multiple of the subarray&#39;s elements is </em><code>k</code>.</p>
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
<p>The <strong>least common multiple of an array</strong> is the smallest positive integer that is divisible by all the array elements.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,6,2,7,1], k = 6
<strong>Output:</strong> 4
<strong>Explanation:</strong> The subarrays of nums where 6 is the least common multiple of all the subarray&#39;s elements are:
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,2,7,1]
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
- [3,<u><strong>6</strong></u>,2,7,1]
- [3,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no subarrays of nums where 2 is the least common multiple of all the subarray&#39;s elements.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i], k &lt;= 1000</code></li>
</ul>

View File

@ -0,0 +1,69 @@
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
<p>At every node <code>i</code>, there is a gate. You are also given an array of even integers <code>amount</code>, where <code>amount[i]</code> represents:</p>
<ul>
<li>the price needed to open the gate at node <code>i</code>, if <code>amount[i]</code> is negative, or,</li>
<li>the cash reward obtained on opening the gate at node <code>i</code>, otherwise.</li>
</ul>
<p>The game goes on as follows:</p>
<ul>
<li>Initially, Alice is at node <code>0</code> and Bob is at node <code>bob</code>.</li>
<li>At every second, Alice and Bob <b>each</b> move to an adjacent node. Alice moves towards some <strong>leaf node</strong>, while Bob moves towards node <code>0</code>.</li>
<li>For <strong>every</strong> node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
<ul>
<li>If the gate is <strong>already open</strong>, no price will be required, nor will there be any cash reward.</li>
<li>If Alice and Bob reach the node <strong>simultaneously</strong>, they share the price/reward for opening the gate there. In other words, if the price to open the gate is <code>c</code>, then both Alice and Bob pay&nbsp;<code>c / 2</code> each. Similarly, if the reward at the gate is <code>c</code>, both of them receive <code>c / 2</code> each.</li>
</ul>
</li>
<li>If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node <code>0</code>, he stops moving. Note that these events are <strong>independent</strong> of each other.</li>
</ul>
<p>Return<em> the <strong>maximum</strong> net income Alice can have if she travels towards the optimal leaf node.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;" />
<pre>
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
The above diagram represents the given tree. The game goes as follows:
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
Alice&#39;s net income is now -2.
- Both Alice and Bob move to node 1.
&nbsp; Since they reach here simultaneously, they open the gate together and share the reward.
&nbsp; Alice&#39;s net income becomes -2 + (4 / 2) = 0.
- Alice moves on to node 3. Since Bob already opened its gate, Alice&#39;s income remains unchanged.
&nbsp; Bob moves on to node 0, and stops moving.
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
Now, neither Alice nor Bob can make any further moves, and the game ends.
It is not possible for Alice to get a higher net income.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;" />
<pre>
<strong>Input:</strong> edges = [[0,1]], bob = 1, amount = [-7280,2350]
<strong>Output:</strong> -7280
<strong>Explanation:</strong>
Alice follows the path 0-&gt;1 whereas Bob follows the path 1-&gt;0.
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 &lt;= bob &lt; n</code></li>
<li><code>amount.length == n</code></li>
<li><code>amount[i]</code> is an <strong>even</strong> integer in the range <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>.</li>
</ul>

View File

@ -0,0 +1,40 @@
<p>You are given a string, <code>message</code>, and a positive integer, <code>limit</code>.</p>
<p>You must <strong>split</strong> <code>message</code> into one or more <strong>parts</strong> based on <code>limit</code>. Each resulting part should have the suffix <code>&quot;&lt;a/b&gt;&quot;</code>, where <code>&quot;b&quot;</code> is to be <strong>replaced</strong> with the total number of parts and <code>&quot;a&quot;</code> is to be <strong>replaced</strong> with the index of the part, starting from <code>1</code> and going up to <code>b</code>. Additionally, the length of each resulting part (including its suffix) should be <strong>equal</strong> to <code>limit</code>, except for the last part whose length can be <strong>at most</strong> <code>limit</code>.</p>
<p>The resulting parts should be formed such that when their suffixes are removed and they are all concatenated <strong>in order</strong>, they should be equal to <code>message</code>. Also, the result should contain as few parts as possible.</p>
<p>Return<em> the parts </em><code>message</code><em> would be split into as an array of strings</em>. If it is impossible to split <code>message</code> as required, return<em> an empty array</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> message = &quot;this is really a very awesome message&quot;, limit = 9
<strong>Output:</strong> [&quot;thi&lt;1/14&gt;&quot;,&quot;s i&lt;2/14&gt;&quot;,&quot;s r&lt;3/14&gt;&quot;,&quot;eal&lt;4/14&gt;&quot;,&quot;ly &lt;5/14&gt;&quot;,&quot;a v&lt;6/14&gt;&quot;,&quot;ery&lt;7/14&gt;&quot;,&quot; aw&lt;8/14&gt;&quot;,&quot;eso&lt;9/14&gt;&quot;,&quot;me&lt;10/14&gt;&quot;,&quot; m&lt;11/14&gt;&quot;,&quot;es&lt;12/14&gt;&quot;,&quot;sa&lt;13/14&gt;&quot;,&quot;ge&lt;14/14&gt;&quot;]
<strong>Explanation:</strong>
The first 9 parts take 3 characters each from the beginning of message.
The next 5 parts take 2 characters each to finish splitting message.
In this example, each part, including the last, has length 9.
It can be shown it is not possible to split message into less than 14 parts.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> message = &quot;short message&quot;, limit = 15
<strong>Output:</strong> [&quot;short mess&lt;1/2&gt;&quot;,&quot;age&lt;2/2&gt;&quot;]
<strong>Explanation:</strong>
Under the given constraints, the string can be split into two parts:
- The first part comprises of the first 10 characters, and has a length 15.
- The next part comprises of the last 3 characters, and has a length 8.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= message.length &lt;= 10<sup>4</sup></code></li>
<li><code>message</code> consists only of lowercase English letters and <code>&#39; &#39;</code>.</li>
<li><code>1 &lt;= limit &lt;= 10<sup>4</sup></code></li>
</ul>

View File

@ -0,0 +1,36 @@
<p>You are given a non-negative floating point number rounded to two decimal places <code>celsius</code>, that denotes the <strong>temperature in Celsius</strong>.</p>
<p>You should convert Celsius into <strong>Kelvin</strong> and <strong>Fahrenheit</strong> and return it as an array <code>ans = [kelvin, fahrenheit]</code>.</p>
<p>Return <em>the array <code>ans</code>. </em>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
<p><strong>Note that:</strong></p>
<ul>
<li><code>Kelvin = Celsius + 273.15</code></li>
<li><code>Fahrenheit = Celsius * 1.80 + 32.00</code></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> celsius = 36.50
<strong>Output:</strong> [309.65000,97.70000]
<strong>Explanation:</strong> Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> celsius = 122.11
<strong>Output:</strong> [395.26000,251.79800]
<strong>Explanation:</strong> Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= celsius &lt;= 1000</code></li>
</ul>

View File

@ -0,0 +1,40 @@
<p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>&#39;0&#39;</code> <code>zero</code> times.</li>
<li>Append the character <code>&#39;1&#39;</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is &quot;011&quot;.
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
All binary strings from &quot;000&quot; to &quot;111&quot; are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are &quot;00&quot;, &quot;11&quot;, &quot;000&quot;, &quot;110&quot;, and &quot;011&quot;.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= low&nbsp;&lt;= high&nbsp;&lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= zero, one &lt;= low</code></li>
</ul>

View File

@ -0,0 +1,51 @@
<p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p>
<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p>
<p>Return <em>the minimum number of operations needed to make the values at each level sorted in a <strong>strictly increasing order</strong></em>.</p>
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node<em>.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;" />
<pre>
<strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].
- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].
- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,7,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].
- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].
- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Each level is already sorted in increasing order so return 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
<li>All the values of the tree are <strong>unique</strong>.</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,36 @@
<p>You are given a non-negative floating point number rounded to two decimal places <code>celsius</code>, that denotes the <strong>temperature in Celsius</strong>.</p>
<p>You should convert Celsius into <strong>Kelvin</strong> and <strong>Fahrenheit</strong> and return it as an array <code>ans = [kelvin, fahrenheit]</code>.</p>
<p>Return <em>the array <code>ans</code>. </em>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
<p><strong>Note that:</strong></p>
<ul>
<li><code>Kelvin = Celsius + 273.15</code></li>
<li><code>Fahrenheit = Celsius * 1.80 + 32.00</code></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> celsius = 36.50
<strong>Output:</strong> [309.65000,97.70000]
<strong>Explanation:</strong> Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> celsius = 122.11
<strong>Output:</strong> [395.26000,251.79800]
<strong>Explanation:</strong> Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= celsius &lt;= 1000</code></li>
</ul>

View File

@ -0,0 +1,40 @@
<p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>&#39;0&#39;</code> <code>zero</code> times.</li>
<li>Append the character <code>&#39;1&#39;</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is &quot;011&quot;.
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
All binary strings from &quot;000&quot; to &quot;111&quot; are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are &quot;00&quot;, &quot;11&quot;, &quot;000&quot;, &quot;110&quot;, and &quot;011&quot;.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= low&nbsp;&lt;= high&nbsp;&lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= zero, one &lt;= low</code></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
<ul>
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
<li>Each substring is a <strong>palindrome</strong>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
<p>A <strong>substring</strong> 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;abaccdbbd&quot;, k = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can select the substrings underlined in s = &quot;<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>&quot;. Both &quot;aba&quot; and &quot;dbbd&quot; are palindromes and have a length of at least k = 3.
It can be shown that we cannot find a selection with more than two valid substrings.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;adbcda&quot;, k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= s.length &lt;= 2000</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,51 @@
<p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p>
<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p>
<p>Return <em>the minimum number of operations needed to make the values at each level sorted in a <strong>strictly increasing order</strong></em>.</p>
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node<em>.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;" />
<pre>
<strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].
- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].
- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,7,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].
- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].
- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Each level is already sorted in increasing order so return 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
<li>All the values of the tree are <strong>unique</strong>.</li>
</ul>

View File

@ -0,0 +1,69 @@
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
<p>At every node <code>i</code>, there is a gate. You are also given an array of even integers <code>amount</code>, where <code>amount[i]</code> represents:</p>
<ul>
<li>the price needed to open the gate at node <code>i</code>, if <code>amount[i]</code> is negative, or,</li>
<li>the cash reward obtained on opening the gate at node <code>i</code>, otherwise.</li>
</ul>
<p>The game goes on as follows:</p>
<ul>
<li>Initially, Alice is at node <code>0</code> and Bob is at node <code>bob</code>.</li>
<li>At every second, Alice and Bob <b>each</b> move to an adjacent node. Alice moves towards some <strong>leaf node</strong>, while Bob moves towards node <code>0</code>.</li>
<li>For <strong>every</strong> node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
<ul>
<li>If the gate is <strong>already open</strong>, no price will be required, nor will there be any cash reward.</li>
<li>If Alice and Bob reach the node <strong>simultaneously</strong>, they share the price/reward for opening the gate there. In other words, if the price to open the gate is <code>c</code>, then both Alice and Bob pay&nbsp;<code>c / 2</code> each. Similarly, if the reward at the gate is <code>c</code>, both of them receive <code>c / 2</code> each.</li>
</ul>
</li>
<li>If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node <code>0</code>, he stops moving. Note that these events are <strong>independent</strong> of each other.</li>
</ul>
<p>Return<em> the <strong>maximum</strong> net income Alice can have if she travels towards the optimal leaf node.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;" />
<pre>
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
The above diagram represents the given tree. The game goes as follows:
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
Alice&#39;s net income is now -2.
- Both Alice and Bob move to node 1.
&nbsp; Since they reach here simultaneously, they open the gate together and share the reward.
&nbsp; Alice&#39;s net income becomes -2 + (4 / 2) = 0.
- Alice moves on to node 3. Since Bob already opened its gate, Alice&#39;s income remains unchanged.
&nbsp; Bob moves on to node 0, and stops moving.
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
Now, neither Alice nor Bob can make any further moves, and the game ends.
It is not possible for Alice to get a higher net income.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;" />
<pre>
<strong>Input:</strong> edges = [[0,1]], bob = 1, amount = [-7280,2350]
<strong>Output:</strong> -7280
<strong>Explanation:</strong>
Alice follows the path 0-&gt;1 whereas Bob follows the path 1-&gt;0.
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 &lt;= bob &lt; n</code></li>
<li><code>amount.length == n</code></li>
<li><code>amount[i]</code> is an <strong>even</strong> integer in the range <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>.</li>
</ul>

View File

@ -0,0 +1,50 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
<ul>
<li>Find the minimum number in <code>nums</code> and remove it.</li>
<li>Find the maximum number in <code>nums</code> and remove it.</li>
<li>Calculate the average of the two removed numbers.</li>
</ul>
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
<ul>
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
</ul>
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,4,0,3,5]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,100]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
There is only one average to be calculated after removing 1 and 100, so we return 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>nums.length</code> is even.</li>
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@ -0,0 +1,34 @@
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>subarrays</strong> of </em><code>nums</code><em> where the least common multiple of the subarray&#39;s elements is </em><code>k</code>.</p>
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
<p>The <strong>least common multiple of an array</strong> is the smallest positive integer that is divisible by all the array elements.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,6,2,7,1], k = 6
<strong>Output:</strong> 4
<strong>Explanation:</strong> The subarrays of nums where 6 is the least common multiple of all the subarray&#39;s elements are:
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,2,7,1]
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
- [3,<u><strong>6</strong></u>,2,7,1]
- [3,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no subarrays of nums where 2 is the least common multiple of all the subarray&#39;s elements.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i], k &lt;= 1000</code></li>
</ul>

View File

@ -0,0 +1,40 @@
<p>You are given a string, <code>message</code>, and a positive integer, <code>limit</code>.</p>
<p>You must <strong>split</strong> <code>message</code> into one or more <strong>parts</strong> based on <code>limit</code>. Each resulting part should have the suffix <code>&quot;&lt;a/b&gt;&quot;</code>, where <code>&quot;b&quot;</code> is to be <strong>replaced</strong> with the total number of parts and <code>&quot;a&quot;</code> is to be <strong>replaced</strong> with the index of the part, starting from <code>1</code> and going up to <code>b</code>. Additionally, the length of each resulting part (including its suffix) should be <strong>equal</strong> to <code>limit</code>, except for the last part whose length can be <strong>at most</strong> <code>limit</code>.</p>
<p>The resulting parts should be formed such that when their suffixes are removed and they are all concatenated <strong>in order</strong>, they should be equal to <code>message</code>. Also, the result should contain as few parts as possible.</p>
<p>Return<em> the parts </em><code>message</code><em> would be split into as an array of strings</em>. If it is impossible to split <code>message</code> as required, return<em> an empty array</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> message = &quot;this is really a very awesome message&quot;, limit = 9
<strong>Output:</strong> [&quot;thi&lt;1/14&gt;&quot;,&quot;s i&lt;2/14&gt;&quot;,&quot;s r&lt;3/14&gt;&quot;,&quot;eal&lt;4/14&gt;&quot;,&quot;ly &lt;5/14&gt;&quot;,&quot;a v&lt;6/14&gt;&quot;,&quot;ery&lt;7/14&gt;&quot;,&quot; aw&lt;8/14&gt;&quot;,&quot;eso&lt;9/14&gt;&quot;,&quot;me&lt;10/14&gt;&quot;,&quot; m&lt;11/14&gt;&quot;,&quot;es&lt;12/14&gt;&quot;,&quot;sa&lt;13/14&gt;&quot;,&quot;ge&lt;14/14&gt;&quot;]
<strong>Explanation:</strong>
The first 9 parts take 3 characters each from the beginning of message.
The next 5 parts take 2 characters each to finish splitting message.
In this example, each part, including the last, has length 9.
It can be shown it is not possible to split message into less than 14 parts.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> message = &quot;short message&quot;, limit = 15
<strong>Output:</strong> [&quot;short mess&lt;1/2&gt;&quot;,&quot;age&lt;2/2&gt;&quot;]
<strong>Explanation:</strong>
Under the given constraints, the string can be split into two parts:
- The first part comprises of the first 10 characters, and has a length 15.
- The next part comprises of the last 3 characters, and has a length 8.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= message.length &lt;= 10<sup>4</sup></code></li>
<li><code>message</code> consists only of lowercase English letters and <code>&#39; &#39;</code>.</li>
<li><code>1 &lt;= limit &lt;= 10<sup>4</sup></code></li>
</ul>