mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-04-18 08:39:41 +08:00
update
This commit is contained in:
parent
48eaf39261
commit
560c99b559
leetcode-cn
origin-data.json
originData
[no content]design-a-todo-list.json[no content]make-the-prefix-sum-non-negative.jsoncheck-knight-tour-configuration.jsoncount-number-of-possible-root-nodes.jsoncount-the-number-of-beautiful-subarrays.jsoncount-the-number-of-vowel-strings-in-range.jsoncount-total-number-of-colored-cells.jsoncount-ways-to-group-overlapping-ranges.jsondistribute-money-to-maximum-children.jsonfind-score-of-an-array-after-marking-all-elements.jsonkth-largest-sum-in-a-binary-tree.jsonmaximize-greatness-of-an-array.jsonminimum-time-to-complete-all-tasks.jsonminimum-time-to-repair-cars.jsonnumber-of-even-and-odd-bits.jsonnumber-of-ways-to-earn-points.jsonpass-the-pillow.jsonrearrange-array-to-maximize-prefix-score.jsonsmallest-missing-non-negative-integer-after-operations.jsonsplit-the-array-to-make-coprime-products.jsonsplit-with-minimum-sum.jsonthe-number-of-beautiful-subsets.json
problem (Chinese)
二叉树中的第 K 大层和 [kth-largest-sum-in-a-binary-tree].html修车的最少时间 [minimum-time-to-repair-cars].html分割数组使乘积互质 [split-the-array-to-make-coprime-products].html奇偶位数 [number-of-even-and-odd-bits].html完成所有任务的最少时间 [minimum-time-to-complete-all-tasks].html将钱分给最多的儿童 [distribute-money-to-maximum-children].html执行操作后的最大 MEX [smallest-missing-non-negative-integer-after-operations].html最大化数组的伟大值 [maximize-greatness-of-an-array].html最小和分割 [split-with-minimum-sum].html标记所有元素后数组的分数 [find-score-of-an-array-after-marking-all-elements].html检查骑士巡视方案 [check-knight-tour-configuration].html统计可能的树根数目 [count-number-of-possible-root-nodes].html统计将重叠区间合并成组的方案数 [count-ways-to-group-overlapping-ranges].html统计染色格子数 [count-total-number-of-colored-cells].html统计美丽子数组数目 [count-the-number-of-beautiful-subarrays].html统计范围内的元音字符串数 [count-the-number-of-vowel-strings-in-range].html美丽子集的数目 [the-number-of-beautiful-subsets].html获得分数的方法数 [number-of-ways-to-earn-points].html递枕头 [pass-the-pillow].html重排数组以得到最大前缀分数 [rearrange-array-to-maximize-prefix-score].html
problem (English)
二叉树中的第 K 大层和(English) [kth-largest-sum-in-a-binary-tree].html修车的最少时间(English) [minimum-time-to-repair-cars].html分割数组使乘积互质(English) [split-the-array-to-make-coprime-products].html奇偶位数(English) [number-of-even-and-odd-bits].html完成所有任务的最少时间(English) [minimum-time-to-complete-all-tasks].html将钱分给最多的儿童(English) [distribute-money-to-maximum-children].html执行操作后的最大 MEX(English) [smallest-missing-non-negative-integer-after-operations].html最大化数组的伟大值(English) [maximize-greatness-of-an-array].html最小和分割(English) [split-with-minimum-sum].html标记所有元素后数组的分数(English) [find-score-of-an-array-after-marking-all-elements].html检查骑士巡视方案(English) [check-knight-tour-configuration].html统计可能的树根数目(English) [count-number-of-possible-root-nodes].html统计将重叠区间合并成组的方案数(English) [count-ways-to-group-overlapping-ranges].html统计染色格子数(English) [count-total-number-of-colored-cells].html统计美丽子数组数目(English) [count-the-number-of-beautiful-subarrays].html统计范围内的元音字符串数(English) [count-the-number-of-vowel-strings-in-range].html美丽子集的数目(English) [the-number-of-beautiful-subsets].html获得分数的方法数(English) [number-of-ways-to-earn-points].html递枕头(English) [pass-the-pillow].html重排数组以得到最大前缀分数(English) [rearrange-array-to-maximize-prefix-score].html
leetcode
origin-data.json
originData
[no content]design-a-todo-list.json[no content]make-the-prefix-sum-non-negative.jsoncheck-knight-tour-configuration.jsoncount-number-of-possible-root-nodes.jsoncount-the-number-of-beautiful-subarrays.jsoncount-the-number-of-vowel-strings-in-range.jsoncount-total-number-of-colored-cells.jsoncount-ways-to-group-overlapping-ranges.jsondistribute-money-to-maximum-children.jsonfind-score-of-an-array-after-marking-all-elements.jsonkth-largest-sum-in-a-binary-tree.jsonmaximize-greatness-of-an-array.jsonminimum-time-to-complete-all-tasks.jsonminimum-time-to-repair-cars.jsonnumber-of-even-and-odd-bits.jsonnumber-of-ways-to-earn-points.jsonpass-the-pillow.jsonrearrange-array-to-maximize-prefix-score.jsonsmallest-missing-non-negative-integer-after-operations.jsonsplit-the-array-to-make-coprime-products.jsonsplit-with-minimum-sum.jsonthe-number-of-beautiful-subsets.json
problem
check-knight-tour-configuration.htmlcount-number-of-possible-root-nodes.htmlcount-the-number-of-beautiful-subarrays.htmlcount-the-number-of-vowel-strings-in-range.htmlcount-total-number-of-colored-cells.htmlcount-ways-to-group-overlapping-ranges.htmldistribute-money-to-maximum-children.htmlfind-score-of-an-array-after-marking-all-elements.htmlkth-largest-sum-in-a-binary-tree.htmlmaximize-greatness-of-an-array.htmlminimum-time-to-complete-all-tasks.htmlminimum-time-to-repair-cars.htmlnumber-of-even-and-odd-bits.htmlnumber-of-ways-to-earn-points.html
File diff suppressed because it is too large
Load Diff
77
leetcode-cn/originData/[no content]design-a-todo-list.json
Normal file
77
leetcode-cn/originData/[no content]design-a-todo-list.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
195
leetcode-cn/originData/check-knight-tour-configuration.json
Normal file
195
leetcode-cn/originData/check-knight-tour-configuration.json
Normal file
File diff suppressed because one or more lines are too long
190
leetcode-cn/originData/count-number-of-possible-root-nodes.json
Normal file
190
leetcode-cn/originData/count-number-of-possible-root-nodes.json
Normal file
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
170
leetcode-cn/originData/count-total-number-of-colored-cells.json
Normal file
170
leetcode-cn/originData/count-total-number-of-colored-cells.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/distribute-money-to-maximum-children.json
Normal file
177
leetcode-cn/originData/distribute-money-to-maximum-children.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/kth-largest-sum-in-a-binary-tree.json
Normal file
183
leetcode-cn/originData/kth-largest-sum-in-a-binary-tree.json
Normal file
File diff suppressed because one or more lines are too long
189
leetcode-cn/originData/maximize-greatness-of-an-array.json
Normal file
189
leetcode-cn/originData/maximize-greatness-of-an-array.json
Normal file
File diff suppressed because one or more lines are too long
196
leetcode-cn/originData/minimum-time-to-complete-all-tasks.json
Normal file
196
leetcode-cn/originData/minimum-time-to-complete-all-tasks.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/minimum-time-to-repair-cars.json
Normal file
177
leetcode-cn/originData/minimum-time-to-repair-cars.json
Normal file
File diff suppressed because one or more lines are too long
171
leetcode-cn/originData/number-of-even-and-odd-bits.json
Normal file
171
leetcode-cn/originData/number-of-even-and-odd-bits.json
Normal file
File diff suppressed because one or more lines are too long
178
leetcode-cn/originData/number-of-ways-to-earn-points.json
Normal file
178
leetcode-cn/originData/number-of-ways-to-earn-points.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/pass-the-pillow.json
Normal file
177
leetcode-cn/originData/pass-the-pillow.json
Normal file
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
183
leetcode-cn/originData/split-with-minimum-sum.json
Normal file
183
leetcode-cn/originData/split-with-minimum-sum.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/the-number-of-beautiful-subsets.json
Normal file
183
leetcode-cn/originData/the-number-of-beautiful-subsets.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,45 @@
|
||||
<p>给你一棵二叉树的根节点 <code>root</code> 和一个正整数 <code>k</code> 。</p>
|
||||
|
||||
<p>树中的 <strong>层和</strong> 是指 <strong>同一层</strong> 上节点值的总和。</p>
|
||||
|
||||
<p>返回树中第 <code>k</code> 大的层和(不一定不同)。如果树少于 <code>k</code> 层,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>注意</strong>,如果两个节点与根节点的距离相同,则认为它们在同一层。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,8,9,2,1,3,7,4,6], k = 2
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>树中每一层的层和分别是:
|
||||
- Level 1: 5
|
||||
- Level 2: 8 + 9 = 17
|
||||
- Level 3: 2 + 1 + 3 + 7 = 13
|
||||
- Level 4: 4 + 6 = 10
|
||||
第 2 大的层和等于 13 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" style="width: 181px; height: 181px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,null,3], k = 1
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>最大的层和是 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数为 <code>n</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>给你一个整数数组 <code>ranks</code> ,表示一些机械工的 <strong>能力值</strong> 。<code>ranks<sub>i</sub></code> 是第 <code>i</code> 位机械工的能力值。能力值为 <code>r</code> 的机械工可以在 <code>r * n<sup>2</sup></code> 分钟内修好 <code>n</code> 辆车。</p>
|
||||
|
||||
<p>同时给你一个整数 <code>cars</code> ,表示总共需要修理的汽车数目。</p>
|
||||
|
||||
<p>请你返回修理所有汽车 <strong>最少</strong> 需要多少时间。</p>
|
||||
|
||||
<p><strong>注意:</strong>所有机械工可以同时修理汽车。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>ranks = [4,2,3,1], cars = 10
|
||||
<b>输出:</b>16
|
||||
<b>解释:</b>
|
||||
- 第一位机械工修 2 辆车,需要 4 * 2 * 2 = 16 分钟。
|
||||
- 第二位机械工修 2 辆车,需要 2 * 2 * 2 = 8 分钟。
|
||||
- 第三位机械工修 2 辆车,需要 3 * 2 * 2 = 12 分钟。
|
||||
- 第四位机械工修 4 辆车,需要 1 * 4 * 4 = 16 分钟。
|
||||
16 分钟是修理完所有车需要的最少时间。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>ranks = [5,1,8], cars = 6
|
||||
<b>输出:</b>16
|
||||
<b>解释:</b>
|
||||
- 第一位机械工修 1 辆车,需要 5 * 1 * 1 = 5 分钟。
|
||||
- 第二位机械工修 4 辆车,需要 1 * 4 * 4 = 16 分钟。
|
||||
- 第三位机械工修 1 辆车,需要 8 * 1 * 1 = 8 分钟。
|
||||
16 分钟时修理完所有车需要的最少时间。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= ranks[i] <= 100</code></li>
|
||||
<li><code>1 <= cars <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
44
leetcode-cn/problem (Chinese)/分割数组使乘积互质 [split-the-array-to-make-coprime-products].html
Normal file
44
leetcode-cn/problem (Chinese)/分割数组使乘积互质 [split-the-array-to-make-coprime-products].html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,下标从 <strong>0</strong> 开始。</p>
|
||||
|
||||
<p>如果在下标 <code>i</code> 处 <strong>分割</strong> 数组,其中 <code>0 <= i <= n - 2</code> ,使前 <code>i + 1</code> 个元素的乘积和剩余元素的乘积互质,则认为该分割 <strong>有效</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果 <code>nums = [2, 3, 3]</code> ,那么在下标 <code>i = 0</code> 处的分割有效,因为 <code>2</code> 和 <code>9</code> 互质,而在下标 <code>i = 1</code> 处的分割无效,因为 <code>6</code> 和 <code>3</code> 不互质。在下标 <code>i = 2</code> 处的分割也无效,因为 <code>i == n - 1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回可以有效分割数组的最小下标 <code>i</code> ,如果不存在有效分割,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>当且仅当 <code>gcd(val1, val2) == 1</code> 成立时,<code>val1</code> 和 <code>val2</code> 这两个值才是互质的,其中 <code>gcd(val1, val2)</code> 表示 <code>val1</code> 和 <code>val2</code> 的最大公约数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/second.PNG" style="width: 450px; height: 211px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,7,8,15,3,5]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>上表展示了每个下标 i 处的前 i + 1 个元素的乘积、剩余元素的乘积和它们的最大公约数的值。
|
||||
唯一一个有效分割位于下标 2 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/capture.PNG" style="width: 450px; height: 215px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,7,15,8,3,5]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>上表展示了每个下标 i 处的前 i + 1 个元素的乘积、剩余元素的乘积和它们的最大公约数的值。
|
||||
不存在有效分割。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>给你一个 <strong>正</strong> 整数 <code>n</code> 。</p>
|
||||
|
||||
<p>用 <code>even</code> 表示在 <code>n</code> 的二进制形式(下标从 <strong>0</strong> 开始)中值为 <code>1</code> 的偶数下标的个数。</p>
|
||||
|
||||
<p>用 <code>odd</code> 表示在 <code>n</code> 的二进制形式(下标从 <strong>0</strong> 开始)中值为 <code>1</code> 的奇数下标的个数。</p>
|
||||
|
||||
<p>返回整数数组<em> </em><code>answer</code><em> </em>,其中<em> </em><code>answer = [even, odd]</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 17
|
||||
<strong>输出:</strong>[2,0]
|
||||
<strong>解释:</strong>17 的二进制形式是 10001 。
|
||||
下标 0 和 下标 4 对应的值为 1 。
|
||||
共有 2 个偶数下标,0 个奇数下标。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>[0,1]
|
||||
<strong>解释:</strong>2 的二进制形式是 10 。
|
||||
下标 1 对应的值为 1 。
|
||||
共有 0 个偶数下标,1 个奇数下标。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>你有一台电脑,它可以 <strong>同时</strong> 运行无数个任务。给你一个二维整数数组 <code>tasks</code> ,其中 <code>tasks[i] = [start<sub>i</sub>, end<sub>i</sub>, duration<sub>i</sub>]</code> 表示第 <code>i</code> 个任务需要在 <strong>闭区间</strong> 时间段 <code>[start<sub>i</sub>, end<sub>i</sub>]</code> 内运行 <code>duration<sub>i</sub></code> 个整数时间点(但不需要连续)。</p>
|
||||
|
||||
<p>当电脑需要运行任务时,你可以打开电脑,如果空闲时,你可以将电脑关闭。</p>
|
||||
|
||||
<p>请你返回完成所有任务的情况下,电脑最少需要运行多少秒。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [[2,3,1],[4,5,1],[1,5,2]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>
|
||||
- 第一个任务在闭区间 [2, 2] 运行。
|
||||
- 第二个任务在闭区间 [5, 5] 运行。
|
||||
- 第三个任务在闭区间 [2, 2] 和 [5, 5] 运行。
|
||||
电脑总共运行 2 个整数时间点。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [[1,3,2],[2,5,3],[5,6,2]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>
|
||||
- 第一个任务在闭区间 [2, 3] 运行
|
||||
- 第二个任务在闭区间 [2, 3] 和 [5, 5] 运行。
|
||||
- 第三个任务在闭区间 [5, 6] 运行。
|
||||
电脑总共运行 4 个整数时间点。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 2000</code></li>
|
||||
<li><code>tasks[i].length == 3</code></li>
|
||||
<li><code>1 <= start<sub>i</sub>, end<sub>i</sub> <= 2000</code></li>
|
||||
<li><code>1 <= duration<sub>i</sub> <= end<sub>i</sub> - start<sub>i</sub> + 1 </code></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你一个整数 <code>money</code> ,表示你总共有的钱数(单位为美元)和另一个整数 <code>children</code> ,表示你要将钱分配给多少个儿童。</p>
|
||||
|
||||
<p>你需要按照如下规则分配:</p>
|
||||
|
||||
<ul>
|
||||
<li>所有的钱都必须被分配。</li>
|
||||
<li>每个儿童至少获得 <code>1</code> 美元。</li>
|
||||
<li>没有人获得 <code>4</code> 美元。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你按照上述规则分配金钱,并返回 <strong>最多</strong> 有多少个儿童获得 <strong>恰好</strong><em> </em><code>8</code> 美元。如果没有任何分配方案,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>money = 20, children = 3
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>
|
||||
最多获得 8 美元的儿童数为 1 。一种分配方案为:
|
||||
- 给第一个儿童分配 8 美元。
|
||||
- 给第二个儿童分配 9 美元。
|
||||
- 给第三个儿童分配 3 美元。
|
||||
没有分配方案能让获得 8 美元的儿童数超过 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>money = 16, children = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>每个儿童都可以获得 8 美元。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= money <= 200</code></li>
|
||||
<li><code>2 <= children <= 30</code></li>
|
||||
</ul>
|
46
leetcode-cn/problem (Chinese)/执行操作后的最大 MEX [smallest-missing-non-negative-integer-after-operations].html
Normal file
46
leetcode-cn/problem (Chinese)/执行操作后的最大 MEX [smallest-missing-non-negative-integer-after-operations].html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>value</code> 。</p>
|
||||
|
||||
<p>在一步操作中,你可以对 <code>nums</code> 中的任一元素加上或减去 <code>value</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果 <code>nums = [1,2,3]</code> 且 <code>value = 2</code> ,你可以选择 <code>nums[0]</code> 减去 <code>value</code> ,得到 <code>nums = [-1,2,3]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>数组的 MEX (minimum excluded) 是指其中数组中缺失的最小非负整数。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>[-1,2,3]</code> 的 MEX 是 <code>0</code> ,而 <code>[1,0,3]</code> 的 MEX 是 <code>2</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回在执行上述操作 <strong>任意次</strong> 后,<code>nums</code><em> </em>的最大 MEX <em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,-10,7,13,6,8], value = 5
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>执行下述操作可以得到这一结果:
|
||||
- nums[1] 加上 value 两次,nums = [1,<em><strong>0</strong></em>,7,13,6,8]
|
||||
- nums[2] 减去 value 一次,nums = [1,0,<em><strong>2</strong></em>,13,6,8]
|
||||
- nums[3] 减去 value 两次,nums = [1,0,2,<em><strong>3</strong></em>,6,8]
|
||||
nums 的 MEX 是 4 。可以证明 4 是可以取到的最大 MEX 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,-10,7,13,6,8], value = 7
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>执行下述操作可以得到这一结果:
|
||||
- nums[2] 减去 value 一次,nums = [1,-10,<em><strong>0</strong></em>,13,6,8]
|
||||
nums 的 MEX 是 2 。可以证明 2 是可以取到的最大 MEX 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, value <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,31 @@
|
||||
<p>给你一个下标从 0 开始的整数数组 <code>nums</code> 。你需要将 <code>nums</code> 重新排列成一个新的数组 <code>perm</code> 。</p>
|
||||
|
||||
<p>定义 <code>nums</code> 的 <strong>伟大值</strong> 为满足 <code>0 <= i < nums.length</code> 且 <code>perm[i] > nums[i]</code> 的下标数目。</p>
|
||||
|
||||
<p>请你返回重新排列 <code>nums</code> 后的 <strong>最大</strong> 伟大值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,3,5,2,1,3,1]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>一个最优安排方案为 perm = [2,5,1,3,3,1,1] 。
|
||||
在下标为 0, 1, 3 和 4 处,都有 perm[i] > nums[i] 。因此我们返回 4 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,4]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>最优排列为 [2,3,4,1] 。
|
||||
在下标为 0, 1 和 2 处,都有 perm[i] > nums[i] 。因此我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个正整数 <code>num</code> ,请你将它分割成两个非负整数 <code>num1</code> 和 <code>num2</code> ,满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1</code> 和 <code>num2</code> 直接连起来,得到 <code>num</code> 各数位的一个排列。
|
||||
|
||||
<ul>
|
||||
<li>换句话说,<code>num1</code> 和 <code>num2</code> 中所有数字出现的次数之和等于 <code>num</code> 中所有数字出现的次数。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>num1</code> 和 <code>num2</code> 可以包含前导 0 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>num1</code> 和 <code>num2</code> 可以得到的和的 <strong>最小</strong> 值。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>num</code> 保证没有前导 0 。</li>
|
||||
<li><code>num1</code> 和 <code>num2</code> 中数位顺序可以与 <code>num</code> 中数位顺序不同。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>num = 4325
|
||||
<b>输出:</b>59
|
||||
<b>解释:</b>我们可以将 4325 分割成 <code>num1 </code>= 24 和 num2<code> = </code>35 ,和为 59 ,59 是最小和。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>num = 687
|
||||
<b>输出:</b>75
|
||||
<b>解释:</b>我们可以将 687 分割成 <code>num1</code> = 68 和 <code>num2 </code>= 7 ,和为最优值 75 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>10 <= num <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
45
leetcode-cn/problem (Chinese)/标记所有元素后数组的分数 [find-score-of-an-array-after-marking-all-elements].html
Normal file
45
leetcode-cn/problem (Chinese)/标记所有元素后数组的分数 [find-score-of-an-array-after-marking-all-elements].html
Normal file
@ -0,0 +1,45 @@
|
||||
<p>给你一个数组 <code>nums</code> ,它包含若干正整数。</p>
|
||||
|
||||
<p>一开始分数 <code>score = 0</code> ,请你按照下面算法求出最后分数:</p>
|
||||
|
||||
<ul>
|
||||
<li>从数组中选择最小且没有被标记的整数。如果有相等元素,选择下标最小的一个。</li>
|
||||
<li>将选中的整数加到 <code>score</code> 中。</li>
|
||||
<li>标记 <strong>被选中元素</strong>,如果有相邻元素,则同时标记 <strong>与它相邻的两个元素</strong> 。</li>
|
||||
<li>重复此过程直到数组中所有元素都被标记。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回执行上述算法后最后的分数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [2,1,3,4,5,2]
|
||||
<b>输出:</b>7
|
||||
<b>解释:</b>我们按照如下步骤标记元素:
|
||||
- 1 是最小未标记元素,所以标记它和相邻两个元素:[<em><strong>2</strong></em>,<em><strong>1</strong></em>,<em><strong>3</strong></em>,4,5,2] 。
|
||||
- 2 是最小未标记元素,所以标记它和左边相邻元素:[<em><strong>2</strong></em>,<em><strong>1</strong></em>,<em><strong>3</strong></em>,4,<em><strong>5</strong></em>,<em><strong>2</strong></em>] 。
|
||||
- 4 是仅剩唯一未标记的元素,所以我们标记它:[<em><strong>2</strong></em>,<em><strong>1</strong></em>,<em><strong>3</strong></em>,<em><strong>4</strong></em>,<em><strong>5</strong></em>,<em><strong>2</strong></em>] 。
|
||||
总得分为 1 + 2 + 4 = 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [2,3,5,1,3,2]
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>我们按照如下步骤标记元素:
|
||||
- 1 是最小未标记元素,所以标记它和相邻两个元素:[2,3,<em><strong>5</strong></em>,<em><strong>1</strong></em>,<em><strong>3</strong></em>,2] 。
|
||||
- 2 是最小未标记元素,由于有两个 2 ,我们选择最左边的一个 2 ,也就是下标为 0 处的 2 ,以及它右边相邻的元素:[<em><strong>2</strong></em>,<em><strong>3</strong></em>,<em><strong>5</strong></em>,<em><strong>1</strong></em>,<em><strong>3</strong></em>,2] 。
|
||||
- 2 是仅剩唯一未标记的元素,所以我们标记它:[<em><strong>2</strong></em>,<em><strong>3</strong></em>,<em><strong>5</strong></em>,<em><strong>1</strong></em>,<em><strong>3</strong></em>,<em><strong>2</strong></em>] 。
|
||||
总得分为 1 + 2 + 2 = 5 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>骑士在一张 <code>n x n</code> 的棋盘上巡视。在有效的巡视方案中,骑士会从棋盘的 <strong>左上角</strong> 出发,并且访问棋盘上的每个格子 <strong>恰好一次</strong> 。</p>
|
||||
|
||||
<p>给你一个 <code>n x n</code> 的整数矩阵 <code>grid</code> ,由范围 <code>[0, n * n - 1]</code> 内的不同整数组成,其中 <code>grid[row][col]</code> 表示单元格 <code>(row, col)</code> 是骑士访问的第 <code>grid[row][col]</code> 个单元格。骑士的行动是从下标 <strong>0</strong> 开始的。</p>
|
||||
|
||||
<p>如果 <code>grid</code> 表示了骑士的有效巡视方案,返回 <code>true</code>;否则返回 <code>false</code>。</p>
|
||||
|
||||
<p><strong>注意</strong>,骑士行动时可以垂直移动两个格子且水平移动一个格子,或水平移动两个格子且垂直移动一个格子。下图展示了骑士从某个格子出发可能的八种行动路线。<br>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/knight.png" style="width: 300px; height: 300px;"></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png" style="width: 251px; height: 251px;">
|
||||
<pre><strong>输入:</strong>grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>grid 如上图所示,可以证明这是一个有效的巡视方案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png" style="width: 151px; height: 151px;">
|
||||
<pre><strong>输入:</strong>grid = [[0,3,6],[5,8,1],[2,7,4]]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>grid 如上图所示,考虑到骑士第 7 次行动后的位置,第 8 次行动是无效的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 7</code></li>
|
||||
<li><code>0 <= grid[row][col] < n * n</code></li>
|
||||
<li><code>grid</code> 中的所有整数 <strong>互不相同</strong></li>
|
||||
</ul>
|
@ -0,0 +1,65 @@
|
||||
<p>Alice 有一棵 <code>n</code> 个节点的树,节点编号为 <code>0</code> 到 <code>n - 1</code> 。树用一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> 表示,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> ,表示树中节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条边。</p>
|
||||
|
||||
<p>Alice 想要 Bob 找到这棵树的根。她允许 Bob 对这棵树进行若干次 <strong>猜测</strong> 。每一次猜测,Bob 做如下事情:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择两个 <strong>不相等</strong> 的整数 <code>u</code> 和 <code>v</code> ,且树中必须存在边 <code>[u, v]</code> 。</li>
|
||||
<li>Bob 猜测树中 <code>u</code> 是 <code>v</code> 的 <strong>父节点</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>Bob 的猜测用二维整数数组 <code>guesses</code> 表示,其中 <code>guesses[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> 表示 Bob 猜 <code>u<sub>j</sub></code> 是 <code>v<sub>j</sub></code> 的父节点。</p>
|
||||
|
||||
<p>Alice 非常懒,她不想逐个回答 Bob 的猜测,只告诉 Bob 这些猜测里面 <strong>至少</strong> 有 <code>k</code> 个猜测的结果为 <code>true</code> 。</p>
|
||||
|
||||
<p>给你二维整数数组 <code>edges</code> ,Bob 的所有猜测和整数 <code>k</code> ,请你返回可能成为树根的 <strong>节点数目</strong> 。如果没有这样的树,则返回 <code>0</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>
|
||||
根为节点 0 ,正确的猜测为 [1,3], [0,1], [2,4]
|
||||
根为节点 1 ,正确的猜测为 [1,3], [1,0], [2,4]
|
||||
根为节点 2 ,正确的猜测为 [1,3], [1,0], [2,4]
|
||||
根为节点 3 ,正确的猜测为 [1,0], [2,4]
|
||||
根为节点 4 ,正确的猜测为 [1,3], [1,0]
|
||||
节点 0 ,1 或 2 为根时,可以得到 3 个正确的猜测。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>
|
||||
根为节点 0 ,正确的猜测为 [3,4]
|
||||
根为节点 1 ,正确的猜测为 [1,0], [3,4]
|
||||
根为节点 2 ,正确的猜测为 [1,0], [2,1], [3,4]
|
||||
根为节点 3 ,正确的猜测为 [1,0], [2,1], [3,2], [3,4]
|
||||
根为节点 4 ,正确的猜测为 [1,0], [2,1], [3,2]
|
||||
任何节点为根,都至少有 1 个正确的猜测。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= guesses.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
|
||||
<li><code>edges</code> 表示一棵有效的树。</li>
|
||||
<li><code>guesses[j]</code> 是树中的一条边。</li>
|
||||
<li><code>guesses</code> 是唯一的。</li>
|
||||
<li><code>0 <= k <= guesses.length</code></li>
|
||||
</ul>
|
53
leetcode-cn/problem (Chinese)/统计将重叠区间合并成组的方案数 [count-ways-to-group-overlapping-ranges].html
Normal file
53
leetcode-cn/problem (Chinese)/统计将重叠区间合并成组的方案数 [count-ways-to-group-overlapping-ranges].html
Normal file
@ -0,0 +1,53 @@
|
||||
<p>给你一个二维整数数组 <code>ranges</code> ,其中 <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 表示 <code>start<sub>i</sub></code> 到 <code>end<sub>i</sub></code> 之间(包括二者)的所有整数都包含在第 <code>i</code> 个区间中。</p>
|
||||
|
||||
<p>你需要将 <code>ranges</code> 分成 <strong>两个</strong> 组(可以为空),满足:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个区间只属于一个组。</li>
|
||||
<li>两个有 <strong>交集</strong> 的区间必须在 <strong>同一个 </strong>组内。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果两个区间有至少 <strong>一个</strong> 公共整数,那么这两个区间是 <b>有交集</b> 的。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,区间 <code>[1, 3]</code> 和 <code>[2, 5]</code> 有交集,因为 <code>2</code> 和 <code>3</code> 在两个区间中都被包含。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回将 <code>ranges</code> 划分成两个组的 <strong>总方案数</strong> 。由于答案可能很大,将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>ranges = [[6,10],[5,15]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>
|
||||
两个区间有交集,所以它们必须在同一个组内。
|
||||
所以有两种方案:
|
||||
- 将两个区间都放在第 1 个组中。
|
||||
- 将两个区间都放在第 2 个组中。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>ranges = [[1,3],[10,20],[2,5],[4,8]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>
|
||||
区间 [1,3] 和 [2,5] 有交集,所以它们必须在同一个组中。
|
||||
同理,区间 [2,5] 和 [4,8] 也有交集,所以它们也必须在同一个组中。
|
||||
所以总共有 4 种分组方案:
|
||||
- 所有区间都在第 1 组。
|
||||
- 所有区间都在第 2 组。
|
||||
- 区间 [1,3] ,[2,5] 和 [4,8] 在第 1 个组中,[10,20] 在第 2 个组中。
|
||||
- 区间 [1,3] ,[2,5] 和 [4,8] 在第 2 个组中,[10,20] 在第 1 个组中。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>ranges[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>有一个无穷大的二维网格图,一开始所有格子都未染色。给你一个正整数 <code>n</code> ,表示你需要执行以下步骤 <code>n</code> 分钟:</p>
|
||||
|
||||
<ul>
|
||||
<li>第一分钟,将 <strong>任一</strong> 格子染成蓝色。</li>
|
||||
<li>之后的每一分钟,将与蓝色格子相邻的 <strong>所有</strong> 未染色格子染成蓝色。</li>
|
||||
</ul>
|
||||
|
||||
<p>下图分别是 1、2、3 分钟后的网格图。</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png" style="width: 500px; height: 279px;">
|
||||
<p>请你返回 <code>n</code> 分钟之后 <strong>被染色的格子 </strong>数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<pre><b>输入:</b>n = 1
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>1 分钟后,只有 1 个蓝色的格子,所以返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>n = 2
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>2 分钟后,有 4 个在边缘的蓝色格子和 1 个在中间的蓝色格子,所以返回 5 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
47
leetcode-cn/problem (Chinese)/统计美丽子数组数目 [count-the-number-of-beautiful-subarrays].html
Normal file
47
leetcode-cn/problem (Chinese)/统计美丽子数组数目 [count-the-number-of-beautiful-subarrays].html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组<code>nums</code> 。每次操作中,你可以:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择两个满足 <code>0 <= i, j < nums.length</code> 的不同下标 <code>i</code> 和 <code>j</code> 。</li>
|
||||
<li>选择一个非负整数 <code>k</code> ,满足 <code>nums[i]</code> 和 <code>nums[j]</code> 在二进制下的第 <code>k</code> 位(下标编号从 <strong>0</strong> 开始)是 <code>1</code> 。</li>
|
||||
<li>将 <code>nums[i]</code> 和 <code>nums[j]</code> 都减去 <code>2<sup>k</sup></code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果一个子数组内执行上述操作若干次后,该子数组可以变成一个全为 <code>0</code> 的数组,那么我们称它是一个 <strong>美丽</strong> 的子数组。</p>
|
||||
|
||||
<p>请你返回数组 <code>nums</code> 中 <strong>美丽子数组</strong> 的数目。</p>
|
||||
|
||||
<p>子数组是一个数组中一段连续 <strong>非空</strong> 的元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,3,1,2,4]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>nums 中有 2 个美丽子数组:[4,<em><strong>3,1,2</strong></em>,4] 和 [<em><strong>4,3,1,2,4</strong></em>] 。
|
||||
- 按照下述步骤,我们可以将子数组 [3,1,2] 中所有元素变成 0 :
|
||||
- 选择 [<em><strong>3</strong></em>, 1, <em><strong>2</strong></em>] 和 k = 1 。将 2 个数字都减去 2<sup>1</sup> ,子数组变成 [1, 1, 0] 。
|
||||
- 选择 [<em><strong>1</strong></em>, <em><strong>1</strong></em>, 0] 和 k = 0 。将 2 个数字都减去 2<sup>0</sup> ,子数组变成 [0, 0, 0] 。
|
||||
- 按照下述步骤,我们可以将子数组 [4,3,1,2,4] 中所有元素变成 0 :
|
||||
- 选择 [<em><strong>4</strong></em>, 3, 1, 2, <em><strong>4</strong></em>] 和 k = 2 。将 2 个数字都减去 2<sup>2</sup> ,子数组变成 [0, 3, 1, 2, 0] 。
|
||||
- 选择 [0, <em><strong>3</strong></em>, <em><strong>1</strong></em>, 2, 0] 和 k = 0 。将 2 个数字都减去 2<sup>0</sup> ,子数组变成 [0, 2, 0, 2, 0] 。
|
||||
- 选择 [0, <em><strong>2</strong></em>, 0, <em><strong>2</strong></em>, 0] 和 k = 1 。将 2 个数字都减去 2<sup>1</sup> ,子数组变成 [0, 0, 0, 0, 0] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,10,4]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>nums 中没有任何美丽子数组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
43
leetcode-cn/problem (Chinese)/统计范围内的元音字符串数 [count-the-number-of-vowel-strings-in-range].html
Normal file
43
leetcode-cn/problem (Chinese)/统计范围内的元音字符串数 [count-the-number-of-vowel-strings-in-range].html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 和两个整数:<code>left</code> 和 <code>right</code> 。</p>
|
||||
|
||||
<p>如果字符串以元音字母开头并以元音字母结尾,那么该字符串就是一个 <strong>元音字符串</strong> ,其中元音字母是 <code>'a'</code>、<code>'e'</code>、<code>'i'</code>、<code>'o'</code>、<code>'u'</code> 。</p>
|
||||
|
||||
<p>返回<em> </em><code>words[i]</code> 是元音字符串的数目,其中<em> </em><code>i</code> 在闭区间 <code>[left, right]</code> 内。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["are","amy","u"], left = 0, right = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
- "are" 是一个元音字符串,因为它以 'a' 开头并以 'e' 结尾。
|
||||
- "amy" 不是元音字符串,因为它没有以元音字母结尾。
|
||||
- "u" 是一个元音字符串,因为它以 'u' 开头并以 'u' 结尾。
|
||||
在上述范围中的元音字符串数目为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
- "aeo" 是一个元音字符串,因为它以 'a' 开头并以 'o' 结尾。
|
||||
- "mu" 不是元音字符串,因为它没有以元音字母开头。
|
||||
- "ooo" 是一个元音字符串,因为它以 'o' 开头并以 'o' 结尾。
|
||||
- "artro" 是一个元音字符串,因为它以 'a' 开头并以 'o' 结尾。
|
||||
在上述范围中的元音字符串数目为 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> 仅由小写英文字母组成</li>
|
||||
<li><code>0 <= left <= right < words.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个由正整数组成的数组 <code>nums</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果 <code>nums</code> 的子集中,任意两个整数的绝对差均不等于 <code>k</code> ,则认为该子数组是一个 <strong>美丽</strong> 子集。</p>
|
||||
|
||||
<p>返回数组 <code>nums</code> 中 <strong>非空</strong> 且 <strong>美丽</strong> 的子集数目。</p>
|
||||
|
||||
<p><code>nums</code> 的子集定义为:可以经由 <code>nums</code> 删除某些元素(也可能不删除)得到的一个数组。只有在删除元素时选择的索引不同的情况下,两个子集才会被视作是不同的子集。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [2,4,6], k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>数组 nums 中的美丽子集有:[2], [4], [6], [2, 6] 。
|
||||
可以证明数组 [2,4,6] 中只存在 4 个美丽子集。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1], k = 1
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>数组 nums 中的美丽数组有:[1] 。
|
||||
可以证明数组 [1] 中只存在 1 个美丽子集。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,55 @@
|
||||
<p>考试中有 <code>n</code> 种类型的题目。给你一个整数 <code>target</code> 和一个下标从 <strong>0</strong> 开始的二维整数数组 <code>types</code> ,其中 <code>types[i] = [count<sub>i</sub>, marks<sub>i</sub>] </code>表示第 <code>i</code> 种类型的题目有 <code>count<sub>i</sub></code> 道,每道题目对应 <code>marks<sub>i</sub></code> 分。</p>
|
||||
|
||||
<p>返回你在考试中恰好得到 <code>target</code> 分的方法数。由于答案可能很大,结果需要对 <code>10<sup>9</sup> +7</code> 取余。</p>
|
||||
|
||||
<p><strong>注意</strong>,同类型题目无法区分。</p>
|
||||
|
||||
<ul>
|
||||
<li>比如说,如果有 <code>3</code> 道同类型题目,那么解答第 <code>1</code> 和第 <code>2</code> 道题目与解答第 <code>1</code> 和第 <code>3</code> 道题目或者第 <code>2</code> 和第 <code>3</code> 道题目是相同的。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>target = 6, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>要获得 6 分,你可以选择以下七种方法之一:
|
||||
- 解决 6 道第 0 种类型的题目:1 + 1 + 1 + 1 + 1 + 1 = 6
|
||||
- 解决 4 道第 0 种类型的题目和 1 道第 1 种类型的题目:1 + 1 + 1 + 1 + 2 = 6
|
||||
- 解决 2 道第 0 种类型的题目和 2 道第 1 种类型的题目:1 + 1 + 2 + 2 = 6
|
||||
- 解决 3 道第 0 种类型的题目和 1 道第 2 种类型的题目:1 + 1 + 1 + 3 = 6
|
||||
- 解决 1 道第 0 种类型的题目、1 道第 1 种类型的题目和 1 道第 2 种类型的题目:1 + 2 + 3 = 6
|
||||
- 解决 3 道第 1 种类型的题目:2 + 2 + 2 = 6
|
||||
- 解决 2 道第 2 种类型的题目:3 + 3 = 6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>target = 5, types = [[50,1],[50,2],[50,5]]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>要获得 5 分,你可以选择以下四种方法之一:
|
||||
- 解决 5 道第 0 种类型的题目:1 + 1 + 1 + 1 + 1 = 5
|
||||
- 解决 3 道第 0 种类型的题目和 1 道第 1 种类型的题目:1 + 1 + 1 + 2 = 5
|
||||
- 解决 1 道第 0 种类型的题目和 2 道第 1 种类型的题目:1 + 2 + 2 = 5
|
||||
- 解决 1 道第 2 种类型的题目:5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>target = 18, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>只有回答所有题目才能获得 18 分。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 1000</code></li>
|
||||
<li><code>n == types.length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>types[i].length == 2</code></li>
|
||||
<li><code>1 <= count<sub>i</sub>, marks<sub>i</sub> <= 50</code></li>
|
||||
</ul>
|
38
leetcode-cn/problem (Chinese)/递枕头 [pass-the-pillow].html
Normal file
38
leetcode-cn/problem (Chinese)/递枕头 [pass-the-pillow].html
Normal file
@ -0,0 +1,38 @@
|
||||
<p><code>n</code> 个人站成一排,按从 <code>1</code> 到 <code>n</code> 编号。</p>
|
||||
|
||||
<p>最初,排在队首的第一个人拿着一个枕头。每秒钟,拿着枕头的人会将枕头传递给队伍中的下一个人。一旦枕头到达队首或队尾,传递方向就会改变,队伍会继续沿相反方向传递枕头。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,当枕头到达第 <code>n</code> 个人时,TA 会将枕头传递给第 <code>n - 1</code> 个人,然后传递给第 <code>n - 2</code> 个人,依此类推。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你两个正整数 <code>n</code> 和 <code>time</code> ,返回 <code>time</code> 秒后拿着枕头的人的编号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, time = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>队伍中枕头的传递情况为:1 -> 2 -> 3 -> 4 -> 3 -> 2 。
|
||||
5 秒后,枕头传递到第 2 个人手中。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, time = 2
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>队伍中枕头的传递情况为:1 -> 2 -> 3 。
|
||||
2 秒后,枕头传递到第 3 个人手中。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>1 <= time <= 1000</code></li>
|
||||
</ul>
|
32
leetcode-cn/problem (Chinese)/重排数组以得到最大前缀分数 [rearrange-array-to-maximize-prefix-score].html
Normal file
32
leetcode-cn/problem (Chinese)/重排数组以得到最大前缀分数 [rearrange-array-to-maximize-prefix-score].html
Normal file
@ -0,0 +1,32 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。你可以将 <code>nums</code> 中的元素按 <strong>任意顺序</strong> 重排(包括给定顺序)。</p>
|
||||
|
||||
<p>令 <code>prefix</code> 为一个数组,它包含了 <code>nums</code> 重新排列后的前缀和。换句话说,<code>prefix[i]</code> 是 <code>nums</code> 重新排列后下标从 <code>0</code> 到 <code>i</code> 的元素之和。<code>nums</code> 的 <strong>分数</strong> 是 <code>prefix</code> 数组中正整数的个数。</p>
|
||||
|
||||
<p>返回可以得到的最大分数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [2,-1,0,1,-3,3,-3]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>数组重排为 nums = [2,3,1,-1,-3,0,-3] 。
|
||||
prefix = [2,5,6,5,2,2,-1] ,分数为 6 。
|
||||
可以证明 6 是能够得到的最大分数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [-2,-3,0]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不管怎么重排数组得到的分数都是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
39
leetcode-cn/problem (English)/二叉树中的第 K 大层和(English) [kth-largest-sum-in-a-binary-tree].html
Normal file
39
leetcode-cn/problem (English)/二叉树中的第 K 大层和(English) [kth-largest-sum-in-a-binary-tree].html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given the <code>root</code> of a binary tree and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>level sum</strong> in the tree is the sum of the values of the nodes that are on the <strong>same</strong> level.</p>
|
||||
|
||||
<p>Return<em> the </em><code>k<sup>th</sup></code><em> <strong>largest</strong> level sum in the tree (not necessarily distinct)</em>. If there are fewer than <code>k</code> levels in the tree, return <code>-1</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that two nodes are on the same level if they have the same distance from the root.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], k = 2
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> The level sums are the following:
|
||||
- Level 1: 5.
|
||||
- Level 2: 8 + 9 = 17.
|
||||
- Level 3: 2 + 1 + 3 + 7 = 13.
|
||||
- Level 4: 4 + 6 = 10.
|
||||
The 2<sup>nd</sup> largest level sum is 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" style="width: 181px; height: 181px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,null,3], k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The largest level sum is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is <code>n</code>.</li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>You are given an integer array <code>ranks</code> representing the <strong>ranks</strong> of some mechanics. <font face="monospace">ranks<sub>i</sub></font> is the rank of the <font face="monospace">i<sup>th</sup></font> mechanic<font face="monospace">.</font> A mechanic with a rank <code>r</code> can repair <font face="monospace">n</font> cars in <code>r * n<sup>2</sup></code> minutes.</p>
|
||||
|
||||
<p>You are also given an integer <code>cars</code> representing the total number of cars waiting in the garage to be repaired.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time taken to repair all the cars.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> All the mechanics can repair the cars simultaneously.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [4,2,3,1], cars = 10
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
|
||||
- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
|
||||
- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
|
||||
- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
|
||||
It can be proved that the cars cannot be repaired in less than 16 minutes.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [5,1,8], cars = 6
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
|
||||
- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
|
||||
- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
|
||||
It can be proved that the cars cannot be repaired in less than 16 minutes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= ranks[i] <= 100</code></li>
|
||||
<li><code>1 <= cars <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
39
leetcode-cn/problem (English)/分割数组使乘积互质(English) [split-the-array-to-make-coprime-products].html
Normal file
39
leetcode-cn/problem (English)/分割数组使乘积互质(English) [split-the-array-to-make-coprime-products].html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>A <strong>split</strong> at an index <code>i</code> where <code>0 <= i <= n - 2</code> is called <strong>valid</strong> if the product of the first <code>i + 1</code> elements and the product of the remaining elements are coprime.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums = [2, 3, 3]</code>, then a split at the index <code>i = 0</code> is valid because <code>2</code> and <code>9</code> are coprime, while a split at the index <code>i = 1</code> is not valid because <code>6</code> and <code>3</code> are not coprime. A split at the index <code>i = 2</code> is not valid because <code>i == n - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the smallest index </em><code>i</code><em> at which the array can be split validly or </em><code>-1</code><em> if there is no such split</em>.</p>
|
||||
|
||||
<p>Two values <code>val1</code> and <code>val2</code> are coprime if <code>gcd(val1, val2) == 1</code> where <code>gcd(val1, val2)</code> is the greatest common divisor of <code>val1</code> and <code>val2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/second.PNG" style="width: 450px; height: 211px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,7,8,15,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.
|
||||
The only valid split is at index 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/capture.PNG" style="width: 450px; height: 215px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,7,15,8,3,5]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.
|
||||
There is no valid split.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
|
||||
|
||||
<p>Let <code>even</code> denote the number of even indices in the binary representation of <code>n</code> (<strong>0-indexed</strong>) with value <code>1</code>.</p>
|
||||
|
||||
<p>Let <code>odd</code> denote the number of odd indices in the binary representation of <code>n</code> (<strong>0-indexed</strong>) with value <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> where </em><code>answer = [even, odd]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 17
|
||||
<strong>Output:</strong> [2,0]
|
||||
<strong>Explanation:</strong> The binary representation of 17 is 10001.
|
||||
It contains 1 on the 0<sup>th</sup> and 4<sup>th</sup> indices.
|
||||
There are 2 even and 0 odd indices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> The binary representation of 2 is 10.
|
||||
It contains 1 on the 1<sup>st</sup> index.
|
||||
There are 0 even and 1 odd indices.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/完成所有任务的最少时间(English) [minimum-time-to-complete-all-tasks].html
Normal file
40
leetcode-cn/problem (English)/完成所有任务的最少时间(English) [minimum-time-to-complete-all-tasks].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>There is a computer that can run an unlimited number of tasks <strong>at the same time</strong>. You are given a 2D integer array <code>tasks</code> where <code>tasks[i] = [start<sub>i</sub>, end<sub>i</sub>, duration<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> task should run for a total of <code>duration<sub>i</sub></code> seconds (not necessarily continuous) within the <strong>inclusive</strong> time range <code>[start<sub>i</sub>, end<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.</p>
|
||||
|
||||
<p>Return <em>the minimum time during which the computer should be turned on to complete all tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[2,3,1],[4,5,1],[1,5,2]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- The first task can be run in the inclusive time range [2, 2].
|
||||
- The second task can be run in the inclusive time range [5, 5].
|
||||
- The third task can be run in the two inclusive time ranges [2, 2] and [5, 5].
|
||||
The computer will be on for a total of 2 seconds.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,3,2],[2,5,3],[5,6,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- The first task can be run in the inclusive time range [2, 3].
|
||||
- The second task can be run in the inclusive time ranges [2, 3] and [5, 5].
|
||||
- The third task can be run in the two inclusive time range [5, 6].
|
||||
The computer will be on for a total of 4 seconds.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 2000</code></li>
|
||||
<li><code>tasks[i].length == 3</code></li>
|
||||
<li><code>1 <= start<sub>i</sub>, end<sub>i</sub> <= 2000</code></li>
|
||||
<li><code>1 <= duration<sub>i</sub> <= end<sub>i</sub> - start<sub>i</sub> + 1 </code></li>
|
||||
</ul>
|
41
leetcode-cn/problem (English)/将钱分给最多的儿童(English) [distribute-money-to-maximum-children].html
Normal file
41
leetcode-cn/problem (English)/将钱分给最多的儿童(English) [distribute-money-to-maximum-children].html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p>
|
||||
|
||||
<p>You have to distribute the money according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>All money must be distributed.</li>
|
||||
<li>Everyone must receive at least <code>1</code> dollar.</li>
|
||||
<li>Nobody receives <code>4</code> dollars.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> money = 20, children = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
|
||||
- 8 dollars to the first child.
|
||||
- 9 dollars to the second child.
|
||||
- 3 dollars to the third child.
|
||||
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> money = 16, children = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Each child can be given 8 dollars.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= money <= 200</code></li>
|
||||
<li><code>2 <= children <= 30</code></li>
|
||||
</ul>
|
46
leetcode-cn/problem (English)/执行操作后的最大 MEX(English) [smallest-missing-non-negative-integer-after-operations].html
Normal file
46
leetcode-cn/problem (English)/执行操作后的最大 MEX(English) [smallest-missing-non-negative-integer-after-operations].html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>value</code>.</p>
|
||||
|
||||
<p>In one operation, you can add or subtract <code>value</code> from any element of <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums = [1,2,3]</code> and <code>value = 2</code>, you can choose to subtract <code>value</code> from <code>nums[0]</code> to make <code>nums = [-1,2,3]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The MEX (minimum excluded) of an array is the smallest missing <strong>non-negative</strong> integer in it.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the MEX of <code>[-1,2,3]</code> is <code>0</code> while the MEX of <code>[1,0,3]</code> is <code>2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum MEX of </em><code>nums</code><em> after applying the mentioned operation <strong>any number of times</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-10,7,13,6,8], value = 5
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One can achieve this result by applying the following operations:
|
||||
- Add value to nums[1] twice to make nums = [1,<strong><u>0</u></strong>,7,13,6,8]
|
||||
- Subtract value from nums[2] once to make nums = [1,0,<strong><u>2</u></strong>,13,6,8]
|
||||
- Subtract value from nums[3] twice to make nums = [1,0,2,<strong><u>3</u></strong>,6,8]
|
||||
The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-10,7,13,6,8], value = 7
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One can achieve this result by applying the following operation:
|
||||
- subtract value from nums[2] once to make nums = [1,-10,<u><strong>0</strong></u>,13,6,8]
|
||||
The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, value <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
31
leetcode-cn/problem (English)/最大化数组的伟大值(English) [maximize-greatness-of-an-array].html
Normal file
31
leetcode-cn/problem (English)/最大化数组的伟大值(English) [maximize-greatness-of-an-array].html
Normal file
@ -0,0 +1,31 @@
|
||||
<p>You are given a 0-indexed integer array <code>nums</code>. You are allowed to permute <code>nums</code> into a new array <code>perm</code> of your choosing.</p>
|
||||
|
||||
<p>We define the <strong>greatness</strong> of <code>nums</code> be the number of indices <code>0 <= i < nums.length</code> for which <code>perm[i] > nums[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible greatness you can achieve after permuting</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,1,3,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
|
||||
At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can prove the optimal perm is [2,3,4,1].
|
||||
At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>Given a positive integer <code>num</code>, split it into two non-negative integers <code>num1</code> and <code>num2</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The concatenation of <code>num1</code> and <code>num2</code> is a permutation of <code>num</code>.
|
||||
|
||||
<ul>
|
||||
<li>In other words, the sum of the number of occurrences of each digit in <code>num1</code> and <code>num2</code> is equal to the number of occurrences of that digit in <code>num</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>num1</code> and <code>num2</code> can contain leading zeros.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible sum of</em> <code>num1</code> <em>and</em> <code>num2</code>.</p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>It is guaranteed that <code>num</code> does not contain any leading zeros.</li>
|
||||
<li>The order of occurrence of the digits in <code>num1</code> and <code>num2</code> may differ from the order of occurrence of <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 4325
|
||||
<strong>Output:</strong> 59
|
||||
<strong>Explanation:</strong> We can split 4325 so that <code>num1 </code>is 24 and num2<code> is </code>35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 687
|
||||
<strong>Output:</strong> 75
|
||||
<strong>Explanation:</strong> We can split 687 so that <code>num1</code> is 68 and <code>num2 </code>is 7, which would give an optimal sum of 75.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>10 <= num <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
45
leetcode-cn/problem (English)/标记所有元素后数组的分数(English) [find-score-of-an-array-after-marking-all-elements].html
Normal file
45
leetcode-cn/problem (English)/标记所有元素后数组的分数(English) [find-score-of-an-array-after-marking-all-elements].html
Normal file
@ -0,0 +1,45 @@
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>Starting with <code>score = 0</code>, apply the following algorithm:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li>
|
||||
<li>Add the value of the chosen integer to <code>score</code>.</li>
|
||||
<li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li>
|
||||
<li>Repeat until all the array elements are marked.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the score you get after applying the above algorithm</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,4,5,2]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> We mark the elements as follows:
|
||||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [<u>2</u>,<u>1</u>,<u>3</u>,4,5,2].
|
||||
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [<u>2</u>,<u>1</u>,<u>3</u>,4,<u>5</u>,<u>2</u>].
|
||||
- 4 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>1</u>,<u>3</u>,<u>4</u>,<u>5</u>,<u>2</u>].
|
||||
Our score is 1 + 2 + 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,5,1,3,2]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We mark the elements as follows:
|
||||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,<u>5</u>,<u>1</u>,<u>3</u>,2].
|
||||
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,2].
|
||||
- 2 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,<u>2</u>].
|
||||
Our score is 1 + 2 + 2 = 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
34
leetcode-cn/problem (English)/检查骑士巡视方案(English) [check-knight-tour-configuration].html
Normal file
34
leetcode-cn/problem (English)/检查骑士巡视方案(English) [check-knight-tour-configuration].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>There is a knight on an <code>n x n</code> chessboard. In a valid configuration, the knight starts <strong>at the top-left cell</strong> of the board and visits every cell on the board <strong>exactly once</strong>.</p>
|
||||
|
||||
<p>You are given an <code>n x n</code> integer matrix <code>grid</code> consisting of distinct integers from the range <code>[0, n * n - 1]</code> where <code>grid[row][col]</code> indicates that the cell <code>(row, col)</code> is the <code>grid[row][col]<sup>th</sup></code> cell that the knight visited. The moves are <strong>0-indexed</strong>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if</em> <code>grid</code> <em>represents a valid configuration of the knight's movements or</em> <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/knight.png" style="width: 300px; height: 300px;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png" style="width: 251px; height: 251px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram represents the grid. It can be shown that it is a valid configuration.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png" style="width: 151px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,3,6],[5,8,1],[2,7,4]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The above diagram represents the grid. The 8<sup>th</sup> move of the knight is not valid considering its position after the 7<sup>th</sup> move.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 7</code></li>
|
||||
<li><code>0 <= grid[row][col] < n * n</code></li>
|
||||
<li>All integers in <code>grid</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
65
leetcode-cn/problem (English)/统计可能的树根数目(English) [count-number-of-possible-root-nodes].html
Normal file
65
leetcode-cn/problem (English)/统计可能的树根数目(English) [count-number-of-possible-root-nodes].html
Normal file
@ -0,0 +1,65 @@
|
||||
<p>Alice has an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. The tree is represented as 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>Alice wants Bob to find the root of the tree. She allows Bob to make several <strong>guesses</strong> about her tree. In one guess, he does the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Chooses two <strong>distinct</strong> integers <code>u</code> and <code>v</code> such that there exists an edge <code>[u, v]</code> in the tree.</li>
|
||||
<li>He tells Alice that <code>u</code> is the <strong>parent</strong> of <code>v</code> in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p>Bob's guesses are represented by a 2D integer array <code>guesses</code> where <code>guesses[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates Bob guessed <code>u<sub>j</sub></code> to be the parent of <code>v<sub>j</sub></code>.</p>
|
||||
|
||||
<p>Alice being lazy, does not reply to each of Bob's guesses, but just says that <strong>at least</strong> <code>k</code> of his guesses are <code>true</code>.</p>
|
||||
|
||||
<p>Given the 2D integer arrays <code>edges</code>, <code>guesses</code> and the integer <code>k</code>, return <em>the <strong>number of possible nodes</strong> that can be the root of Alice's tree</em>. If there is no such tree, return <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
Root = 0, correct guesses = [1,3], [0,1], [2,4]
|
||||
Root = 1, correct guesses = [1,3], [1,0], [2,4]
|
||||
Root = 2, correct guesses = [1,3], [1,0], [2,4]
|
||||
Root = 3, correct guesses = [1,0], [2,4]
|
||||
Root = 4, correct guesses = [1,3], [1,0]
|
||||
Considering 0, 1, or 2 as root node leads to 3 correct guesses.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Root = 0, correct guesses = [3,4]
|
||||
Root = 1, correct guesses = [1,0], [3,4]
|
||||
Root = 2, correct guesses = [1,0], [2,1], [3,4]
|
||||
Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]
|
||||
Root = 4, correct guesses = [1,0], [2,1], [3,2]
|
||||
Considering any node as root will give at least 1 correct guess.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= guesses.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>guesses[j]</code> is an edge of the tree.</li>
|
||||
<li><code>guesses</code> is unique.</li>
|
||||
<li><code>0 <= k <= guesses.length</code></li>
|
||||
</ul>
|
53
leetcode-cn/problem (English)/统计将重叠区间合并成组的方案数(English) [count-ways-to-group-overlapping-ranges].html
Normal file
53
leetcode-cn/problem (English)/统计将重叠区间合并成组的方案数(English) [count-ways-to-group-overlapping-ranges].html
Normal file
@ -0,0 +1,53 @@
|
||||
<p>You are given a 2D integer array <code>ranges</code> where <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> denotes that all integers between <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> (both <strong>inclusive</strong>) are contained in the <code>i<sup>th</sup></code> range.</p>
|
||||
|
||||
<p>You are to split <code>ranges</code> into <strong>two</strong> (possibly empty) groups such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each range belongs to exactly one group.</li>
|
||||
<li>Any two <strong>overlapping</strong> ranges must belong to the <strong>same</strong> group.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two ranges are said to be <strong>overlapping</strong> if there exists at least <strong>one</strong> integer that is present in both ranges.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>[1, 3]</code> and <code>[2, 5]</code> are overlapping because <code>2</code> and <code>3</code> occur in both ranges.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to split</em> <code>ranges</code> <em>into two groups</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranges = [[6,10],[5,15]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The two ranges are overlapping, so they must be in the same group.
|
||||
Thus, there are two possible ways:
|
||||
- Put both the ranges together in group 1.
|
||||
- Put both the ranges together in group 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranges = [[1,3],[10,20],[2,5],[4,8]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group.
|
||||
Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group.
|
||||
Thus, there are four possible ways to group them:
|
||||
- All the ranges in group 1.
|
||||
- All the ranges in group 2.
|
||||
- Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2.
|
||||
- Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>ranges[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
34
leetcode-cn/problem (English)/统计染色格子数(English) [count-total-number-of-colored-cells].html
Normal file
34
leetcode-cn/problem (English)/统计染色格子数(English) [count-total-number-of-colored-cells].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer <code>n</code>, indicating that you must do the following routine for <code>n</code> minutes:</p>
|
||||
|
||||
<ul>
|
||||
<li>At the first minute, color <strong>any</strong> arbitrary unit cell blue.</li>
|
||||
<li>Every minute thereafter, color blue <strong>every</strong> uncolored cell that touches a blue cell.</li>
|
||||
</ul>
|
||||
|
||||
<p>Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png" style="width: 500px; height: 279px;" />
|
||||
<p>Return <em>the number of <strong>colored cells</strong> at the end of </em><code>n</code> <em>minutes</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After 1 minute, there is only 1 blue cell, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
45
leetcode-cn/problem (English)/统计美丽子数组数目(English) [count-the-number-of-beautiful-subarrays].html
Normal file
45
leetcode-cn/problem (English)/统计美丽子数组数目(English) [count-the-number-of-beautiful-subarrays].html
Normal file
@ -0,0 +1,45 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two different indices <code>i</code> and <code>j</code> such that <code>0 <= i, j < nums.length</code>.</li>
|
||||
<li>Choose a non-negative integer <code>k</code> such that the <code>k<sup>th</sup></code> bit (<strong>0-indexed</strong>) in the binary representation of <code>nums[i]</code> and <code>nums[j]</code> is <code>1</code>.</li>
|
||||
<li>Subtract <code>2<sup>k</sup></code> from <code>nums[i]</code> and <code>nums[j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A subarray is <strong>beautiful</strong> if it is possible to make all of its elements equal to <code>0</code> after applying the above operation any number of times.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>beautiful subarrays</strong> in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,1,2,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful subarrays in nums: [4,<u>3,1,2</u>,4] and [<u>4,3,1,2,4</u>].
|
||||
- We can make all elements in the subarray [3,1,2] equal to 0 in the following way:
|
||||
- Choose [<u>3</u>, 1, <u>2</u>] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [1, 1, 0].
|
||||
- Choose [<u>1</u>, <u>1</u>, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 0, 0].
|
||||
- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:
|
||||
- Choose [<u>4</u>, 3, 1, 2, <u>4</u>] and k = 2. Subtract 2<sup>2</sup> from both numbers. The subarray becomes [0, 3, 1, 2, 0].
|
||||
- Choose [0, <u>3</u>, <u>1</u>, 2, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 2, 0, 2, 0].
|
||||
- Choose [0, <u>2</u>, 0, <u>2</u>, 0] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [0, 0, 0, 0, 0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,10,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no beautiful subarrays in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
41
leetcode-cn/problem (English)/统计范围内的元音字符串数(English) [count-the-number-of-vowel-strings-in-range].html
Normal file
41
leetcode-cn/problem (English)/统计范围内的元音字符串数(English) [count-the-number-of-vowel-strings-in-range].html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of string <code>words</code> and two integers <code>left</code> and <code>right</code>.</p>
|
||||
|
||||
<p>A string is called a <strong>vowel string</strong> if it starts with a vowel character and ends with a vowel character where vowel characters are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p>Return <em>the number of vowel strings </em><code>words[i]</code><em> where </em><code>i</code><em> belongs to the inclusive range </em><code>[left, right]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["are","amy","u"], left = 0, right = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- "are" is a vowel string because it starts with 'a' and ends with 'e'.
|
||||
- "amy" is not a vowel string because it does not end with a vowel.
|
||||
- "u" is a vowel string because it starts with 'u' and ends with 'u'.
|
||||
The number of vowel strings in the mentioned range is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- "aeo" is a vowel string because it starts with 'a' and ends with 'o'.
|
||||
- "mu" is not a vowel string because it does not start with a vowel.
|
||||
- "ooo" is a vowel string because it starts with 'o' and ends with 'o'.
|
||||
- "artro" is a vowel string because it starts with 'a' and ends with 'o'.
|
||||
The number of vowel strings in the mentioned range is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists of only lowercase English letters.</li>
|
||||
<li><code>0 <= left <= right < words.length</code></li>
|
||||
</ul>
|
34
leetcode-cn/problem (English)/美丽子集的数目(English) [the-number-of-beautiful-subsets].html
Normal file
34
leetcode-cn/problem (English)/美丽子集的数目(English) [the-number-of-beautiful-subsets].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,6], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
|
||||
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1], k = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
|
||||
It can be proved that there is only 1 beautiful subset in the array [1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,59 @@
|
||||
<p>There is a test that has <code>n</code> types of questions. You are given an integer <code>target</code> and a <strong>0-indexed</strong> 2D integer array <code>types</code> where <code>types[i] = [count<sub>i</sub>, marks<sub>i</sub>]</code> indicates that there are <code>count<sub>i</sub></code> questions of the <code>i<sup>th</sup></code> type, and each one of them is worth <code>marks<sub>i</sub></code> points.</p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of ways you can earn <strong>exactly</strong> </em><code>target</code><em> points in the exam</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that questions of the same type are indistinguishable.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if there are <code>3</code> questions of the same type, then solving the <code>1<sup>st</sup></code> and <code>2<sup>nd</sup></code> questions is the same as solving the <code>1<sup>st</sup></code> and <code>3<sup>rd</sup></code> questions, or the <code>2<sup>nd</sup></code> and <code>3<sup>rd</sup></code> questions.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 6, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can earn 6 points in one of the seven ways:
|
||||
- Solve 6 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 + 1 = 6
|
||||
- Solve 4 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 1 + 2 = 6
|
||||
- Solve 2 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 1 + 2 + 2 = 6
|
||||
- Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 1 + 1 + 3 = 6
|
||||
- Solve 1 question of the 0<sup>th</sup> type, 1 question of the 1<sup>st</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 2 + 3 = 6
|
||||
- Solve 3 questions of the 1<sup>st</sup> type: 2 + 2 + 2 = 6
|
||||
- Solve 2 questions of the 2<sup>nd</sup> type: 3 + 3 = 6
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 5, types = [[50,1],[50,2],[50,5]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> You can earn 5 points in one of the four ways:
|
||||
- Solve 5 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 = 5
|
||||
- Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 2 = 5
|
||||
- Solve 1 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 2 + 2 = 5
|
||||
- Solve 1 question of the 2<sup>nd</sup> type: 5
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 18, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You can only earn 18 points by answering all questions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 1000</code></li>
|
||||
<li><code>n == types.length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>types[i].length == 2</code></li>
|
||||
<li><code>1 <= count<sub>i</sub>, marks<sub>i</sub> <= 50</code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, once the pillow reaches the <code>n<sup>th</sup></code> person they pass it to the <code>n - 1<sup>th</sup></code> person, then to the <code>n - 2<sup>th</sup></code> person and so on.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given the two positive integers <code>n</code> and <code>time</code>, return <em>the index of the person holding the pillow after </em><code>time</code><em> seconds</em>.</p>
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, time = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
|
||||
Afer five seconds, the pillow is given to the 2<sup>nd</sup> person.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, time = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3.
|
||||
Afer two seconds, the pillow is given to the 3<sup>r</sup><sup>d</sup> person.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>1 <= time <= 1000</code></li>
|
||||
</ul>
|
32
leetcode-cn/problem (English)/重排数组以得到最大前缀分数(English) [rearrange-array-to-maximize-prefix-score].html
Normal file
32
leetcode-cn/problem (English)/重排数组以得到最大前缀分数(English) [rearrange-array-to-maximize-prefix-score].html
Normal file
@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You can rearrange the elements of <code>nums</code> to <strong>any order</strong> (including the given order).</p>
|
||||
|
||||
<p>Let <code>prefix</code> be the array containing the prefix sums of <code>nums</code> after rearranging it. In other words, <code>prefix[i]</code> is the sum of the elements from <code>0</code> to <code>i</code> in <code>nums</code> after rearranging it. The <strong>score</strong> of <code>nums</code> is the number of positive integers in the array <code>prefix</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum score you can achieve</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-1,0,1,-3,3,-3]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We can rearrange the array into nums = [2,3,1,-1,-3,0,-3].
|
||||
prefix = [2,5,6,5,2,2,-1], so the score is 6.
|
||||
It can be shown that 6 is the maximum score we can obtain.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,-3,0]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Any rearrangement of the array will result in a score of 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
74
leetcode/originData/[no content]design-a-todo-list.json
Normal file
74
leetcode/originData/[no content]design-a-todo-list.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
192
leetcode/originData/check-knight-tour-configuration.json
Normal file
192
leetcode/originData/check-knight-tour-configuration.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/count-number-of-possible-root-nodes.json
Normal file
187
leetcode/originData/count-number-of-possible-root-nodes.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/count-the-number-of-beautiful-subarrays.json
Normal file
187
leetcode/originData/count-the-number-of-beautiful-subarrays.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
167
leetcode/originData/count-total-number-of-colored-cells.json
Normal file
167
leetcode/originData/count-total-number-of-colored-cells.json
Normal file
File diff suppressed because one or more lines are too long
175
leetcode/originData/count-ways-to-group-overlapping-ranges.json
Normal file
175
leetcode/originData/count-ways-to-group-overlapping-ranges.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/distribute-money-to-maximum-children.json
Normal file
174
leetcode/originData/distribute-money-to-maximum-children.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/kth-largest-sum-in-a-binary-tree.json
Normal file
180
leetcode/originData/kth-largest-sum-in-a-binary-tree.json
Normal file
File diff suppressed because one or more lines are too long
186
leetcode/originData/maximize-greatness-of-an-array.json
Normal file
186
leetcode/originData/maximize-greatness-of-an-array.json
Normal file
File diff suppressed because one or more lines are too long
193
leetcode/originData/minimum-time-to-complete-all-tasks.json
Normal file
193
leetcode/originData/minimum-time-to-complete-all-tasks.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/minimum-time-to-repair-cars.json
Normal file
174
leetcode/originData/minimum-time-to-repair-cars.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode/originData/number-of-even-and-odd-bits.json
Normal file
168
leetcode/originData/number-of-even-and-odd-bits.json
Normal file
File diff suppressed because one or more lines are too long
175
leetcode/originData/number-of-ways-to-earn-points.json
Normal file
175
leetcode/originData/number-of-ways-to-earn-points.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/pass-the-pillow.json
Normal file
174
leetcode/originData/pass-the-pillow.json
Normal file
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
180
leetcode/originData/split-with-minimum-sum.json
Normal file
180
leetcode/originData/split-with-minimum-sum.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/the-number-of-beautiful-subsets.json
Normal file
180
leetcode/originData/the-number-of-beautiful-subsets.json
Normal file
File diff suppressed because one or more lines are too long
34
leetcode/problem/check-knight-tour-configuration.html
Normal file
34
leetcode/problem/check-knight-tour-configuration.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>There is a knight on an <code>n x n</code> chessboard. In a valid configuration, the knight starts <strong>at the top-left cell</strong> of the board and visits every cell on the board <strong>exactly once</strong>.</p>
|
||||
|
||||
<p>You are given an <code>n x n</code> integer matrix <code>grid</code> consisting of distinct integers from the range <code>[0, n * n - 1]</code> where <code>grid[row][col]</code> indicates that the cell <code>(row, col)</code> is the <code>grid[row][col]<sup>th</sup></code> cell that the knight visited. The moves are <strong>0-indexed</strong>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if</em> <code>grid</code> <em>represents a valid configuration of the knight's movements or</em> <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/knight.png" style="width: 300px; height: 300px;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png" style="width: 251px; height: 251px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram represents the grid. It can be shown that it is a valid configuration.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png" style="width: 151px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,3,6],[5,8,1],[2,7,4]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The above diagram represents the grid. The 8<sup>th</sup> move of the knight is not valid considering its position after the 7<sup>th</sup> move.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 7</code></li>
|
||||
<li><code>0 <= grid[row][col] < n * n</code></li>
|
||||
<li>All integers in <code>grid</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
65
leetcode/problem/count-number-of-possible-root-nodes.html
Normal file
65
leetcode/problem/count-number-of-possible-root-nodes.html
Normal file
@ -0,0 +1,65 @@
|
||||
<p>Alice has an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. The tree is represented as 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>Alice wants Bob to find the root of the tree. She allows Bob to make several <strong>guesses</strong> about her tree. In one guess, he does the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Chooses two <strong>distinct</strong> integers <code>u</code> and <code>v</code> such that there exists an edge <code>[u, v]</code> in the tree.</li>
|
||||
<li>He tells Alice that <code>u</code> is the <strong>parent</strong> of <code>v</code> in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p>Bob's guesses are represented by a 2D integer array <code>guesses</code> where <code>guesses[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates Bob guessed <code>u<sub>j</sub></code> to be the parent of <code>v<sub>j</sub></code>.</p>
|
||||
|
||||
<p>Alice being lazy, does not reply to each of Bob's guesses, but just says that <strong>at least</strong> <code>k</code> of his guesses are <code>true</code>.</p>
|
||||
|
||||
<p>Given the 2D integer arrays <code>edges</code>, <code>guesses</code> and the integer <code>k</code>, return <em>the <strong>number of possible nodes</strong> that can be the root of Alice's tree</em>. If there is no such tree, return <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
Root = 0, correct guesses = [1,3], [0,1], [2,4]
|
||||
Root = 1, correct guesses = [1,3], [1,0], [2,4]
|
||||
Root = 2, correct guesses = [1,3], [1,0], [2,4]
|
||||
Root = 3, correct guesses = [1,0], [2,4]
|
||||
Root = 4, correct guesses = [1,3], [1,0]
|
||||
Considering 0, 1, or 2 as root node leads to 3 correct guesses.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Root = 0, correct guesses = [3,4]
|
||||
Root = 1, correct guesses = [1,0], [3,4]
|
||||
Root = 2, correct guesses = [1,0], [2,1], [3,4]
|
||||
Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]
|
||||
Root = 4, correct guesses = [1,0], [2,1], [3,2]
|
||||
Considering any node as root will give at least 1 correct guess.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= guesses.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>guesses[j]</code> is an edge of the tree.</li>
|
||||
<li><code>guesses</code> is unique.</li>
|
||||
<li><code>0 <= k <= guesses.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two different indices <code>i</code> and <code>j</code> such that <code>0 <= i, j < nums.length</code>.</li>
|
||||
<li>Choose a non-negative integer <code>k</code> such that the <code>k<sup>th</sup></code> bit (<strong>0-indexed</strong>) in the binary representation of <code>nums[i]</code> and <code>nums[j]</code> is <code>1</code>.</li>
|
||||
<li>Subtract <code>2<sup>k</sup></code> from <code>nums[i]</code> and <code>nums[j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A subarray is <strong>beautiful</strong> if it is possible to make all of its elements equal to <code>0</code> after applying the above operation any number of times.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>beautiful subarrays</strong> in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,1,2,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful subarrays in nums: [4,<u>3,1,2</u>,4] and [<u>4,3,1,2,4</u>].
|
||||
- We can make all elements in the subarray [3,1,2] equal to 0 in the following way:
|
||||
- Choose [<u>3</u>, 1, <u>2</u>] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [1, 1, 0].
|
||||
- Choose [<u>1</u>, <u>1</u>, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 0, 0].
|
||||
- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:
|
||||
- Choose [<u>4</u>, 3, 1, 2, <u>4</u>] and k = 2. Subtract 2<sup>2</sup> from both numbers. The subarray becomes [0, 3, 1, 2, 0].
|
||||
- Choose [0, <u>3</u>, <u>1</u>, 2, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 2, 0, 2, 0].
|
||||
- Choose [0, <u>2</u>, 0, <u>2</u>, 0] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [0, 0, 0, 0, 0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,10,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no beautiful subarrays in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of string <code>words</code> and two integers <code>left</code> and <code>right</code>.</p>
|
||||
|
||||
<p>A string is called a <strong>vowel string</strong> if it starts with a vowel character and ends with a vowel character where vowel characters are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p>Return <em>the number of vowel strings </em><code>words[i]</code><em> where </em><code>i</code><em> belongs to the inclusive range </em><code>[left, right]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["are","amy","u"], left = 0, right = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- "are" is a vowel string because it starts with 'a' and ends with 'e'.
|
||||
- "amy" is not a vowel string because it does not end with a vowel.
|
||||
- "u" is a vowel string because it starts with 'u' and ends with 'u'.
|
||||
The number of vowel strings in the mentioned range is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- "aeo" is a vowel string because it starts with 'a' and ends with 'o'.
|
||||
- "mu" is not a vowel string because it does not start with a vowel.
|
||||
- "ooo" is a vowel string because it starts with 'o' and ends with 'o'.
|
||||
- "artro" is a vowel string because it starts with 'a' and ends with 'o'.
|
||||
The number of vowel strings in the mentioned range is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists of only lowercase English letters.</li>
|
||||
<li><code>0 <= left <= right < words.length</code></li>
|
||||
</ul>
|
34
leetcode/problem/count-total-number-of-colored-cells.html
Normal file
34
leetcode/problem/count-total-number-of-colored-cells.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer <code>n</code>, indicating that you must do the following routine for <code>n</code> minutes:</p>
|
||||
|
||||
<ul>
|
||||
<li>At the first minute, color <strong>any</strong> arbitrary unit cell blue.</li>
|
||||
<li>Every minute thereafter, color blue <strong>every</strong> uncolored cell that touches a blue cell.</li>
|
||||
</ul>
|
||||
|
||||
<p>Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png" style="width: 500px; height: 279px;" />
|
||||
<p>Return <em>the number of <strong>colored cells</strong> at the end of </em><code>n</code> <em>minutes</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After 1 minute, there is only 1 blue cell, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
53
leetcode/problem/count-ways-to-group-overlapping-ranges.html
Normal file
53
leetcode/problem/count-ways-to-group-overlapping-ranges.html
Normal file
@ -0,0 +1,53 @@
|
||||
<p>You are given a 2D integer array <code>ranges</code> where <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> denotes that all integers between <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> (both <strong>inclusive</strong>) are contained in the <code>i<sup>th</sup></code> range.</p>
|
||||
|
||||
<p>You are to split <code>ranges</code> into <strong>two</strong> (possibly empty) groups such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each range belongs to exactly one group.</li>
|
||||
<li>Any two <strong>overlapping</strong> ranges must belong to the <strong>same</strong> group.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two ranges are said to be <strong>overlapping</strong> if there exists at least <strong>one</strong> integer that is present in both ranges.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>[1, 3]</code> and <code>[2, 5]</code> are overlapping because <code>2</code> and <code>3</code> occur in both ranges.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to split</em> <code>ranges</code> <em>into two groups</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranges = [[6,10],[5,15]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The two ranges are overlapping, so they must be in the same group.
|
||||
Thus, there are two possible ways:
|
||||
- Put both the ranges together in group 1.
|
||||
- Put both the ranges together in group 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranges = [[1,3],[10,20],[2,5],[4,8]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group.
|
||||
Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group.
|
||||
Thus, there are four possible ways to group them:
|
||||
- All the ranges in group 1.
|
||||
- All the ranges in group 2.
|
||||
- Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2.
|
||||
- Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>ranges[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
41
leetcode/problem/distribute-money-to-maximum-children.html
Normal file
41
leetcode/problem/distribute-money-to-maximum-children.html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p>
|
||||
|
||||
<p>You have to distribute the money according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>All money must be distributed.</li>
|
||||
<li>Everyone must receive at least <code>1</code> dollar.</li>
|
||||
<li>Nobody receives <code>4</code> dollars.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> money = 20, children = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
|
||||
- 8 dollars to the first child.
|
||||
- 9 dollars to the second child.
|
||||
- 3 dollars to the third child.
|
||||
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> money = 16, children = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Each child can be given 8 dollars.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= money <= 200</code></li>
|
||||
<li><code>2 <= children <= 30</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>Starting with <code>score = 0</code>, apply the following algorithm:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li>
|
||||
<li>Add the value of the chosen integer to <code>score</code>.</li>
|
||||
<li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li>
|
||||
<li>Repeat until all the array elements are marked.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the score you get after applying the above algorithm</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,4,5,2]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> We mark the elements as follows:
|
||||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [<u>2</u>,<u>1</u>,<u>3</u>,4,5,2].
|
||||
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [<u>2</u>,<u>1</u>,<u>3</u>,4,<u>5</u>,<u>2</u>].
|
||||
- 4 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>1</u>,<u>3</u>,<u>4</u>,<u>5</u>,<u>2</u>].
|
||||
Our score is 1 + 2 + 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,5,1,3,2]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We mark the elements as follows:
|
||||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,<u>5</u>,<u>1</u>,<u>3</u>,2].
|
||||
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,2].
|
||||
- 2 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,<u>2</u>].
|
||||
Our score is 1 + 2 + 2 = 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
39
leetcode/problem/kth-largest-sum-in-a-binary-tree.html
Normal file
39
leetcode/problem/kth-largest-sum-in-a-binary-tree.html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given the <code>root</code> of a binary tree and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>level sum</strong> in the tree is the sum of the values of the nodes that are on the <strong>same</strong> level.</p>
|
||||
|
||||
<p>Return<em> the </em><code>k<sup>th</sup></code><em> <strong>largest</strong> level sum in the tree (not necessarily distinct)</em>. If there are fewer than <code>k</code> levels in the tree, return <code>-1</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that two nodes are on the same level if they have the same distance from the root.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], k = 2
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> The level sums are the following:
|
||||
- Level 1: 5.
|
||||
- Level 2: 8 + 9 = 17.
|
||||
- Level 3: 2 + 1 + 3 + 7 = 13.
|
||||
- Level 4: 4 + 6 = 10.
|
||||
The 2<sup>nd</sup> largest level sum is 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" style="width: 181px; height: 181px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,null,3], k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The largest level sum is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is <code>n</code>.</li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
31
leetcode/problem/maximize-greatness-of-an-array.html
Normal file
31
leetcode/problem/maximize-greatness-of-an-array.html
Normal file
@ -0,0 +1,31 @@
|
||||
<p>You are given a 0-indexed integer array <code>nums</code>. You are allowed to permute <code>nums</code> into a new array <code>perm</code> of your choosing.</p>
|
||||
|
||||
<p>We define the <strong>greatness</strong> of <code>nums</code> be the number of indices <code>0 <= i < nums.length</code> for which <code>perm[i] > nums[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible greatness you can achieve after permuting</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,1,3,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
|
||||
At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can prove the optimal perm is [2,3,4,1].
|
||||
At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
40
leetcode/problem/minimum-time-to-complete-all-tasks.html
Normal file
40
leetcode/problem/minimum-time-to-complete-all-tasks.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>There is a computer that can run an unlimited number of tasks <strong>at the same time</strong>. You are given a 2D integer array <code>tasks</code> where <code>tasks[i] = [start<sub>i</sub>, end<sub>i</sub>, duration<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> task should run for a total of <code>duration<sub>i</sub></code> seconds (not necessarily continuous) within the <strong>inclusive</strong> time range <code>[start<sub>i</sub>, end<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.</p>
|
||||
|
||||
<p>Return <em>the minimum time during which the computer should be turned on to complete all tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[2,3,1],[4,5,1],[1,5,2]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- The first task can be run in the inclusive time range [2, 2].
|
||||
- The second task can be run in the inclusive time range [5, 5].
|
||||
- The third task can be run in the two inclusive time ranges [2, 2] and [5, 5].
|
||||
The computer will be on for a total of 2 seconds.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,3,2],[2,5,3],[5,6,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- The first task can be run in the inclusive time range [2, 3].
|
||||
- The second task can be run in the inclusive time ranges [2, 3] and [5, 5].
|
||||
- The third task can be run in the two inclusive time range [5, 6].
|
||||
The computer will be on for a total of 4 seconds.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 2000</code></li>
|
||||
<li><code>tasks[i].length == 3</code></li>
|
||||
<li><code>1 <= start<sub>i</sub>, end<sub>i</sub> <= 2000</code></li>
|
||||
<li><code>1 <= duration<sub>i</sub> <= end<sub>i</sub> - start<sub>i</sub> + 1 </code></li>
|
||||
</ul>
|
42
leetcode/problem/minimum-time-to-repair-cars.html
Normal file
42
leetcode/problem/minimum-time-to-repair-cars.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>You are given an integer array <code>ranks</code> representing the <strong>ranks</strong> of some mechanics. <font face="monospace">ranks<sub>i</sub></font> is the rank of the <font face="monospace">i<sup>th</sup></font> mechanic<font face="monospace">.</font> A mechanic with a rank <code>r</code> can repair <font face="monospace">n</font> cars in <code>r * n<sup>2</sup></code> minutes.</p>
|
||||
|
||||
<p>You are also given an integer <code>cars</code> representing the total number of cars waiting in the garage to be repaired.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time taken to repair all the cars.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> All the mechanics can repair the cars simultaneously.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [4,2,3,1], cars = 10
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
|
||||
- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
|
||||
- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
|
||||
- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
|
||||
It can be proved that the cars cannot be repaired in less than 16 minutes.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [5,1,8], cars = 6
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
|
||||
- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
|
||||
- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
|
||||
It can be proved that the cars cannot be repaired in less than 16 minutes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= ranks[i] <= 100</code></li>
|
||||
<li><code>1 <= cars <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
35
leetcode/problem/number-of-even-and-odd-bits.html
Normal file
35
leetcode/problem/number-of-even-and-odd-bits.html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
|
||||
|
||||
<p>Let <code>even</code> denote the number of even indices in the binary representation of <code>n</code> (<strong>0-indexed</strong>) with value <code>1</code>.</p>
|
||||
|
||||
<p>Let <code>odd</code> denote the number of odd indices in the binary representation of <code>n</code> (<strong>0-indexed</strong>) with value <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> where </em><code>answer = [even, odd]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 17
|
||||
<strong>Output:</strong> [2,0]
|
||||
<strong>Explanation:</strong> The binary representation of 17 is 10001.
|
||||
It contains 1 on the 0<sup>th</sup> and 4<sup>th</sup> indices.
|
||||
There are 2 even and 0 odd indices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> The binary representation of 2 is 10.
|
||||
It contains 1 on the 1<sup>st</sup> index.
|
||||
There are 0 even and 1 odd indices.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
59
leetcode/problem/number-of-ways-to-earn-points.html
Normal file
59
leetcode/problem/number-of-ways-to-earn-points.html
Normal file
@ -0,0 +1,59 @@
|
||||
<p>There is a test that has <code>n</code> types of questions. You are given an integer <code>target</code> and a <strong>0-indexed</strong> 2D integer array <code>types</code> where <code>types[i] = [count<sub>i</sub>, marks<sub>i</sub>]</code> indicates that there are <code>count<sub>i</sub></code> questions of the <code>i<sup>th</sup></code> type, and each one of them is worth <code>marks<sub>i</sub></code> points.</p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of ways you can earn <strong>exactly</strong> </em><code>target</code><em> points in the exam</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that questions of the same type are indistinguishable.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if there are <code>3</code> questions of the same type, then solving the <code>1<sup>st</sup></code> and <code>2<sup>nd</sup></code> questions is the same as solving the <code>1<sup>st</sup></code> and <code>3<sup>rd</sup></code> questions, or the <code>2<sup>nd</sup></code> and <code>3<sup>rd</sup></code> questions.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 6, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can earn 6 points in one of the seven ways:
|
||||
- Solve 6 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 + 1 = 6
|
||||
- Solve 4 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 1 + 2 = 6
|
||||
- Solve 2 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 1 + 2 + 2 = 6
|
||||
- Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 1 + 1 + 3 = 6
|
||||
- Solve 1 question of the 0<sup>th</sup> type, 1 question of the 1<sup>st</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 2 + 3 = 6
|
||||
- Solve 3 questions of the 1<sup>st</sup> type: 2 + 2 + 2 = 6
|
||||
- Solve 2 questions of the 2<sup>nd</sup> type: 3 + 3 = 6
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 5, types = [[50,1],[50,2],[50,5]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> You can earn 5 points in one of the four ways:
|
||||
- Solve 5 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 = 5
|
||||
- Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 2 = 5
|
||||
- Solve 1 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 2 + 2 = 5
|
||||
- Solve 1 question of the 2<sup>nd</sup> type: 5
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 18, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You can only earn 18 points by answering all questions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 1000</code></li>
|
||||
<li><code>n == types.length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>types[i].length == 2</code></li>
|
||||
<li><code>1 <= count<sub>i</sub>, marks<sub>i</sub> <= 50</code></li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user