mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 07:51:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的正整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>你可以对数组执行以下两种操作 <strong>任意次</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>从数组中选择 <strong>两个</strong> 值 <strong>相等</strong> 的元素,并将它们从数组中 <strong>删除</strong> 。</li>
|
||||
<li>从数组中选择 <strong>三个</strong> 值 <strong>相等</strong> 的元素,并将它们从数组中 <strong>删除</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回使数组为空的 <strong>最少</strong> 操作次数,如果无法达成,请返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,3,3,2,2,4,2,3,4]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>我们可以执行以下操作使数组为空:
|
||||
- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。
|
||||
- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。
|
||||
- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。
|
||||
- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。
|
||||
至少需要 4 步操作使数组为空。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,1,2,2,3,3]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>无法使数组为空。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一棵 <code>n</code> 个节点的无向树,节点编号为 <code>0</code> 到 <code>n - 1</code> 。给你整数 <code>n</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>同时给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的整数数组 <code>values</code> ,其中 <code>values[i]</code> 是第 <code>i</code> 个节点的 <strong>值</strong> 。再给你一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>你可以从树中删除一些边,也可以一条边也不删,得到若干连通块。一个 <strong>连通块的值</strong> 定义为连通块中所有节点值之和。如果所有连通块的值都可以被 <code>k</code> 整除,那么我们说这是一个 <strong>合法分割</strong> 。</p>
|
||||
|
||||
<p>请你返回所有合法分割中,<b>连通块数目的最大值</b> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg" style="width: 1024px; height: 453px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们删除节点 1 和 2 之间的边。这是一个合法分割,因为:
|
||||
- 节点 1 和 3 所在连通块的值为 values[1] + values[3] = 12 。
|
||||
- 节点 0 ,2 和 4 所在连通块的值为 values[0] + values[2] + values[4] = 6 。
|
||||
最多可以得到 2 个连通块的合法分割。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg" style="width: 999px; height: 338px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>我们删除节点 0 和 2 ,以及节点 0 和 1 之间的边。这是一个合法分割,因为:
|
||||
- 节点 0 的连通块的值为 values[0] = 3 。
|
||||
- 节点 2 ,5 和 6 所在连通块的值为 values[2] + values[5] + values[6] = 9 。
|
||||
- 节点 1 ,3 和 4 的连通块的值为 values[1] + values[3] + values[4] = 6 。
|
||||
最多可以得到 3 个连通块的合法分割。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>values.length == n</code></li>
|
||||
<li><code>0 <= values[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
<li><code>values</code> 之和可以被 <code>k</code> 整除。</li>
|
||||
<li>输入保证 <code>edges</code> 是一棵无向树。</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一个只包含 <strong>非负</strong> 整数的数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>我们定义满足 <code>l <= r</code> 的子数组 <code>nums[l..r]</code> 的分数为 <code>nums[l] AND nums[l + 1] AND ... AND nums[r]</code> ,其中 <strong>AND</strong> 是按位与运算。</p>
|
||||
|
||||
<p>请你将数组分割成一个或者更多子数组,满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>每个</strong> 元素都 <strong>只</strong> 属于一个子数组。</li>
|
||||
<li>子数组分数之和尽可能<strong> 小</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你在满足以上要求的条件下,返回<strong> 最多</strong> 可以得到多少个子数组。</p>
|
||||
|
||||
<p>一个 <strong>子数组</strong> 是一个数组中一段连续的元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,0,2,0,1,2]
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>我们可以将数组分割成以下子数组:
|
||||
- [1,0] 。子数组分数为 1 AND 0 = 0 。
|
||||
- [2,0] 。子数组分数为 2 AND 0 = 0 。
|
||||
- [1,2] 。子数组分数为 1 AND 2 = 0 。
|
||||
分数之和为 0 + 0 + 0 = 0 ,是我们可以得到的最小分数之和。
|
||||
在分数之和为 0 的前提下,最多可以将数组分割成 3 个子数组。所以返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [5,7,1,3]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们可以将数组分割成一个子数组:[5,7,1,3] ,分数为 1 ,这是可以得到的最小总分数。
|
||||
在总分数为 1 的前提下,最多可以将数组分割成 1 个子数组。所以返回 1 。
|
||||
</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>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一次操作中,你可以将数组的最后一个元素删除,将该元素添加到一个集合中。</p>
|
||||
|
||||
<p>请你返回收集元素 <code>1, 2, ..., k</code> 需要的 <strong>最少操作次数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,1,5,4,2], k = 2
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>4 次操作后,集合中的元素依次添加了 2 ,4 ,5 和 1 。此时集合中包含元素 1 和 2 ,所以答案为 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,1,5,4,2], k = 5
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>5 次操作后,集合中的元素依次添加了 2 ,4 ,5 ,1 和 3 。此时集合中包含元素 1 到 5 ,所以答案为 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,5,3,1], k = 3
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>4 次操作后,集合中的元素依次添加了 1 ,3 ,5 和 2 。此时集合中包含元素 1 到 3 ,所以答案为 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= nums.length</code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
<li>输入保证你可以收集到元素 <code>1, 2, ..., k</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> 和一个整数 <code>target</code> 。</p>
|
||||
|
||||
<p>下标从 <strong>0</strong> 开始的数组 <code>infinite_nums</code> 是通过无限地将 nums 的元素追加到自己之后生成的。</p>
|
||||
|
||||
<p>请你从 <code>infinite_nums</code> 中找出满足 <strong>元素和</strong> 等于 <code>target</code> 的 <strong>最短</strong> 子数组,并返回该子数组的长度。如果不存在满足条件的子数组,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3], target = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在这个例子中 infinite_nums = [1,2,3,1,2,3,1,2,...] 。
|
||||
区间 [1,2] 内的子数组的元素和等于 target = 5 ,且长度 length = 2 。
|
||||
可以证明,当元素和等于目标值 target = 5 时,2 是子数组的最短长度。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1,2,3], target = 4
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在这个例子中 infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
|
||||
区间 [4,5] 内的子数组的元素和等于 target = 4 ,且长度 length = 2 。
|
||||
可以证明,当元素和等于目标值 target = 4 时,2 是子数组的最短长度。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,4,6,8], target = 3
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>在这个例子中 infinite_nums = [2,4,6,8,2,4,6,8,...] 。
|
||||
可以证明,不存在元素和等于目标值 target = 3 的子数组。
|
||||
</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>5</sup></code></li>
|
||||
<li><code>1 <= target <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>现有一个有向图,其中包含 <code>n</code> 个节点,节点编号从 <code>0</code> 到 <code>n - 1</code> 。此外,该图还包含了 <code>n</code> 条有向边。</p>
|
||||
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>edges</code> ,其中 <code>edges[i]</code> 表示存在一条从节点 <code>i</code> 到节点 <code>edges[i]</code> 的边。</p>
|
||||
|
||||
<p>想象在图上发生以下过程:</p>
|
||||
|
||||
<ul>
|
||||
<li>你从节点 <code>x</code> 开始,通过边访问其他节点,直到你在<strong> 此过程 </strong>中再次访问到之前已经访问过的节点。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回数组 <code>answer</code> 作为答案,其中 <code>answer[i]</code> 表示如果从节点 <code>i</code> 开始执行该过程,你可以访问到的不同节点数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png" />
|
||||
<pre>
|
||||
<strong>输入:</strong>edges = [1,2,0,0]
|
||||
<strong>输出:</strong>[3,3,3,4]
|
||||
<strong>解释:</strong>从每个节点开始执行该过程,记录如下:
|
||||
- 从节点 0 开始,访问节点 0 -> 1 -> 2 -> 0 。访问的不同节点数是 3 。
|
||||
- 从节点 1 开始,访问节点 1 -> 2 -> 0 -> 1 。访问的不同节点数是 3 。
|
||||
- 从节点 2 开始,访问节点 2 -> 0 -> 1 -> 2 。访问的不同节点数是 3 。
|
||||
- 从节点 3 开始,访问节点 3 -> 0 -> 1 -> 2 -> 0 。访问的不同节点数是 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png" style="width: 191px; height: 251px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>edges = [1,2,3,4,0]
|
||||
<strong>输出:</strong>[5,5,5,5,5]
|
||||
<strong>解释:</strong>无论从哪个节点开始,在这个过程中,都可以访问到图中的每一个节点。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges[i] <= n - 1</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>请你从所有满足 <code>i < j < k</code> 的下标三元组 <code>(i, j, k)</code> 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 <code>0</code> 。</p>
|
||||
|
||||
<p><strong>下标三元组</strong> <code>(i, j, k)</code> 的值等于 <code>(nums[i] - nums[j]) * nums[k]</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [12,6,1,2,7]
|
||||
<strong>输出:</strong>77
|
||||
<strong>解释:</strong>下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。
|
||||
可以证明不存在值大于 77 的有序下标三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,10,3,4,19]
|
||||
<strong>输出:</strong>133
|
||||
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
|
||||
可以证明不存在值大于 133 的有序下标三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>请你从所有满足 <code>i < j < k</code> 的下标三元组 <code>(i, j, k)</code> 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 <code>0</code> 。</p>
|
||||
|
||||
<p><strong>下标三元组</strong> <code>(i, j, k)</code> 的值等于 <code>(nums[i] - nums[j]) * nums[k]</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [12,6,1,2,7]
|
||||
<strong>输出:</strong>77
|
||||
<strong>解释:</strong>下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。
|
||||
可以证明不存在值大于 77 的有序下标三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,10,3,4,19]
|
||||
<strong>输出:</strong>133
|
||||
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
|
||||
可以证明不存在值大于 133 的有序下标三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user