mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-19 20:16:48 +08:00
update
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<p>给你一个二进制字符串 <code>s</code> 。在一秒之中,<strong>所有</strong> 子字符串 <code>"01"</code> <strong>同时</strong> 被替换成 <code>"10"</code> 。这个过程持续进行到没有 <code>"01"</code> 存在。</p>
|
||||
|
||||
<p>请你返回完成这个过程所需要的秒数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "0110101"
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>
|
||||
一秒后,s 变成 "1011010" 。
|
||||
再过 1 秒后,s 变成 "1101100" 。
|
||||
第三秒过后,s 变成 "1110100" 。
|
||||
第四秒后,s 变成 "1111000" 。
|
||||
此时没有 "01" 存在,整个过程花费 4 秒。
|
||||
所以我们返回 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "11100"
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
s 中没有 "01" 存在,整个过程花费 0 秒。
|
||||
所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong></p>
|
||||
|
||||
<p>你能以 O(n) 的时间复杂度解决这个问题吗?</p>
|
@@ -0,0 +1,57 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的正整数数组 <code>tasks</code> ,表示需要 <strong>按顺序</strong> 完成的任务,其中 <code>tasks[i]</code> 表示第 <code>i</code> 件任务的 <strong>类型</strong> 。</p>
|
||||
|
||||
<p>同时给你一个正整数 <code>space</code> ,表示一个任务完成 <strong>后</strong> ,另一个 <strong>相同</strong> 类型任务完成前需要间隔的 <strong>最少</strong> 天数。</p>
|
||||
|
||||
<p>在所有任务完成前的每一天,你都必须进行以下两种操作中的一种:</p>
|
||||
|
||||
<ul>
|
||||
<li>完成 <code>tasks</code> 中的下一个任务</li>
|
||||
<li>休息一天</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回完成所有任务所需的 <strong>最少</strong> 天数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [1,2,1,2,3,1], space = 3
|
||||
<b>输出:</b>9
|
||||
<strong>解释:</strong>
|
||||
9 天完成所有任务的一种方法是:
|
||||
第 1 天:完成任务 0 。
|
||||
第 2 天:完成任务 1 。
|
||||
第 3 天:休息。
|
||||
第 4 天:休息。
|
||||
第 5 天:完成任务 2 。
|
||||
第 6 天:完成任务 3 。
|
||||
第 7 天:休息。
|
||||
第 8 天:完成任务 4 。
|
||||
第 9 天:完成任务 5 。
|
||||
可以证明无法少于 9 天完成所有任务。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>tasks = [5,8,8,5], space = 2
|
||||
<b>输出:</b>6
|
||||
<strong>解释:</strong>
|
||||
6 天完成所有任务的一种方法是:
|
||||
第 1 天:完成任务 0 。
|
||||
第 2 天:完成任务 1 。
|
||||
第 3 天:休息。
|
||||
第 4 天:休息。
|
||||
第 5 天:完成任务 2 。
|
||||
第 6 天:完成任务 3 。
|
||||
可以证明无法少于 6 天完成所有任务。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= tasks[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= tasks.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个非负整数数组 <code>nums</code> 。在一步操作中,你必须:</p>
|
||||
|
||||
<ul>
|
||||
<li>选出一个正整数 <code>x</code> ,<code>x</code> 需要小于或等于 <code>nums</code> 中 <strong>最小</strong> 的 <strong>非零</strong> 元素。</li>
|
||||
<li><code>nums</code> 中的每个正整数都减去 <code>x</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回使 <code>nums</code> 中所有元素都等于<em> </em><code>0</code> 需要的 <strong>最少</strong> 操作数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,5,0,3,5]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
第一步操作:选出 x = 1 ,之后 nums = [0,4,0,2,4] 。
|
||||
第二步操作:选出 x = 2 ,之后 nums = [0,2,0,0,2] 。
|
||||
第三步操作:选出 x = 2 ,之后 nums = [0,0,0,0,0] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums 中的每个元素都已经是 0 ,所以不需要执行任何操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个正整数数组 <code>grades</code> ,表示大学中一些学生的成绩。你打算将 <strong>所有</strong> 学生分为一些 <strong>有序</strong> 的非空分组,其中分组间的顺序满足以下全部条件:</p>
|
||||
|
||||
<ul>
|
||||
<li>第 <code>i</code> 个分组中的学生总成绩 <strong>小于</strong> 第 <code>(i + 1)</code> 个分组中的学生总成绩,对所有组均成立(除了最后一组)。</li>
|
||||
<li>第 <code>i</code> 个分组中的学生总数 <strong>小于</strong> 第 <code>(i + 1)</code> 个分组中的学生总数,对所有组均成立(除了最后一组)。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回可以形成的 <strong>最大</strong> 组数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grades = [10,6,12,7,3,5]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>下面是形成 3 个分组的一种可行方法:
|
||||
- 第 1 个分组的学生成绩为 grades = [12] ,总成绩:12 ,学生数:1
|
||||
- 第 2 个分组的学生成绩为 grades = [6,7] ,总成绩:6 + 7 = 13 ,学生数:2
|
||||
- 第 3 个分组的学生成绩为 grades = [10,3,5] ,总成绩:10 + 3 + 5 = 18 ,学生数:3
|
||||
可以证明无法形成超过 3 个分组。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grades = [8,8]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>只能形成 1 个分组,因为如果要形成 2 个分组的话,会导致每个分组中的学生数目相等。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grades.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= grades[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和 <code>removeQueries</code> ,两者长度都为 <code>n</code> 。对于第 <code>i</code> 个查询,<code>nums</code> 中位于下标 <code>removeQueries[i]</code> 处的元素被删除,将 <code>nums</code> 分割成更小的子段。</p>
|
||||
|
||||
<p>一个 <strong>子段</strong> 是 <code>nums</code> 中连续 <strong>正</strong> 整数形成的序列。<strong>子段和</strong> 是子段中所有元素的和。</p>
|
||||
|
||||
<p>请你返回一个长度为 <code>n</code> 的整数数组<em> </em><code>answer</code> ,其中<em> </em><code>answer[i]</code>是第 <code>i</code> 次删除操作以后的 <strong>最大</strong> 子段和。</p>
|
||||
|
||||
<p><strong>注意:</strong>一个下标至多只会被删除一次。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,5,6,1], removeQueries = [0,3,2,4,1]
|
||||
<b>输出:</b>[14,7,2,2,0]
|
||||
<b>解释:</b>用 0 表示被删除的元素,答案如下所示:
|
||||
查询 1 :删除第 0 个元素,nums 变成 [0,2,5,6,1] ,最大子段和为子段 [2,5,6,1] 的和 14 。
|
||||
查询 2 :删除第 3 个元素,nums 变成 [0,2,5,0,1] ,最大子段和为子段 [2,5] 的和 7 。
|
||||
查询 3 :删除第 2 个元素,nums 变成 [0,2,0,0,1] ,最大子段和为子段 [2] 的和 2 。
|
||||
查询 4 :删除第 4 个元素,nums 变成 [0,2,0,0,0] ,最大子段和为子段 [2] 的和 2 。
|
||||
查询 5 :删除第 1 个元素,nums 变成 [0,0,0,0,0] ,最大子段和为 0 ,因为没有任何子段存在。
|
||||
所以,我们返回 [14,7,2,2,0] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,2,11,1], removeQueries = [3,2,1,0]
|
||||
<b>输出:</b>[16,5,3,0]
|
||||
<b>解释:</b>用 0 表示被删除的元素,答案如下所示:
|
||||
查询 1 :删除第 3 个元素,nums 变成 [3,2,11,0] ,最大子段和为子段 [3,2,11] 的和 16 。
|
||||
查询 2 :删除第 2 个元素,nums 变成 [3,2,0,0] ,最大子段和为子段 [3,2] 的和 5 。
|
||||
查询 3 :删除第 1 个元素,nums 变成 [3,0,0,0] ,最大子段和为子段 [3] 的和 3 。
|
||||
查询 5 :删除第 0 个元素,nums 变成 [0,0,0,0] ,最大子段和为 0 ,因为没有任何子段存在。
|
||||
所以,我们返回 [16,5,3,0] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length == removeQueries.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= removeQueries[i] < n</code></li>
|
||||
<li><code>removeQueries</code> 中所有数字 <strong>互不相同</strong> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>现有一棵由 <code>n</code> 个节点组成的无向树,节点编号从 <code>0</code> 到 <code>n - 1</code> ,共有 <code>n - 1</code> 条边。</p>
|
||||
|
||||
<p>给你一个二维整数数组 <code>edges</code> ,长度为 <code>n - 1</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> 之间存在一条边。另给你一个整数数组 <code>restricted</code> 表示 <strong>受限</strong> 节点。</p>
|
||||
|
||||
<p>在不访问受限节点的前提下,返回你可以从节点<em> </em><code>0</code><em> </em>到达的 <strong>最多</strong> 节点数目<em>。</em></p>
|
||||
|
||||
<p>注意,节点 <code>0</code> <strong>不</strong> 会标记为受限节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;">
|
||||
<pre><strong>输入:</strong>n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>上图所示正是这棵树。
|
||||
在不访问受限节点的前提下,只有节点 [0,1,2,3] 可以从节点 0 到达。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;">
|
||||
<pre><strong>输入:</strong>n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>上图所示正是这棵树。
|
||||
在不访问受限节点的前提下,只有节点 [0,5,6] 可以从节点 0 到达。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> 表示一棵有效的树</li>
|
||||
<li><code>1 <= restricted.length < n</code></li>
|
||||
<li><code>1 <= restricted[i] < n</code></li>
|
||||
<li><code>restricted</code> 中的所有值 <strong>互不相同</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>给你两个二维整数数组 <code>items1</code> 和 <code>items2</code> ,表示两个物品集合。每个数组 <code>items</code> 有以下特质:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>items[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code> 其中 <code>value<sub>i</sub></code> 表示第 <code>i</code> 件物品的 <strong>价值</strong> ,<code>weight<sub>i</sub></code> 表示第 <code>i</code> 件物品的 <strong>重量</strong> 。</li>
|
||||
<li><code>items</code> 中每件物品的价值都是 <strong>唯一的</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个二维数组 <code>ret</code>,其中 <code>ret[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code>, <code>weight<sub>i</sub></code> 是所有价值为 <code>value<sub>i</sub></code><sub> </sub>物品的 <strong>重量之和</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong><code>ret</code> 应该按价值 <strong>升序</strong> 排序后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
|
||||
<b>输出:</b>[[1,6],[3,9],[4,5]]
|
||||
<b>解释:</b>
|
||||
value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 5 ,总重量为 1 + 5 = 6 。
|
||||
value = 3 的物品再 items1 中 weight = 8 ,在 items2 中 weight = 1 ,总重量为 8 + 1 = 9 。
|
||||
value = 4 的物品在 items1 中 weight = 5 ,总重量为 5 。
|
||||
所以,我们返回 [[1,6],[3,9],[4,5]] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
|
||||
<b>输出:</b>[[1,4],[2,4],[3,4]]
|
||||
<b>解释:</b>
|
||||
value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 3 ,总重量为 1 + 3 = 4 。
|
||||
value = 2 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 1 ,总重量为 3 + 1 = 4 。
|
||||
value = 3 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
|
||||
所以,我们返回 [[1,4],[2,4],[3,4]] 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
|
||||
<b>输出:</b>[[1,7],[2,4],[7,1]]
|
||||
<strong>解释:
|
||||
</strong>value = 1 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 4 ,总重量为 3 + 4 = 7 。
|
||||
value = 2 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
|
||||
value = 7 的物品在 items2 中 weight = 1 ,总重量为 1 。
|
||||
所以,我们返回 [[1,7],[2,4],[7,1]] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= items1.length, items2.length <= 1000</code></li>
|
||||
<li><code>items1[i].length == items2[i].length == 2</code></li>
|
||||
<li><code>1 <= value<sub>i</sub>, weight<sub>i</sub> <= 1000</code></li>
|
||||
<li><code>items1</code> 中每个 <code>value<sub>i</sub></code> 都是 <b>唯一的</b> 。</li>
|
||||
<li><code>items2</code> 中每个 <code>value<sub>i</sub></code> 都是 <b>唯一的</b> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个 <code>n</code> 个节点的 <b>有向图</b> ,节点编号为 <code>0</code> 到 <code>n - 1</code> ,其中每个节点 <strong>至多</strong> 有一条出边。</p>
|
||||
|
||||
<p>图用一个大小为 <code>n</code> 下标从<strong> 0</strong> 开始的数组 <code>edges</code> 表示,节点 <code>i</code> 到节点 <code>edges[i]</code> 之间有一条有向边。如果节点 <code>i</code> 没有出边,那么 <code>edges[i] == -1</code> 。</p>
|
||||
|
||||
<p>请你返回图中的 <strong>最长</strong> 环,如果没有任何环,请返回 <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/06/08/graph4drawio-5.png" style="width: 335px; height: 191px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [3,3,4,2,3]
|
||||
<b>输出去:</b>3
|
||||
<b>解释:</b>图中的最长环是:2 -> 4 -> 3 -> 2 。
|
||||
这个环的长度为 3 ,所以返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-1.png" style="width: 171px; height: 161px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [2,-1,3,1]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>图中没有任何环。
|
||||
</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>-1 <= edges[i] < n</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给你一个小写英文字母组成的字符串 <code>s</code> 和一个二维整数数组 <code>shifts</code> ,其中 <code>shifts[i] = [start<sub>i</sub>, end<sub>i</sub>, direction<sub>i</sub>]</code> 。对于每个 <code>i</code> ,将 <code>s</code> 中从下标 <code>start<sub>i</sub></code> 到下标 <code>end<sub>i</sub></code> (两者都包含)所有字符都进行移位运算,如果 <code>direction<sub>i</sub> = 1</code> 将字符向后移位,如果 <code>direction<sub>i</sub> = 0</code> 将字符向前移位。</p>
|
||||
|
||||
<p>将一个字符 <strong>向后</strong> 移位的意思是将这个字符用字母表中 <strong>下一个</strong> 字母替换(字母表视为环绕的,所以 <code>'z'</code> 变成 <code>'a'</code>)。类似的,将一个字符 <strong>向前</strong> 移位的意思是将这个字符用字母表中 <strong>前一个</strong> 字母替换(字母表是环绕的,所以 <code>'a'</code> 变成 <code>'z'</code> )。</p>
|
||||
|
||||
<p>请你返回对 <code>s</code> 进行所有移位操作以后得到的最终字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
|
||||
<b>输出:</b>"ace"
|
||||
<b>解释:</b>首先,将下标从 0 到 1 的字母向前移位,得到 s = "zac" 。
|
||||
然后,将下标从 1 到 2 的字母向后移位,得到 s = "zbd" 。
|
||||
最后,将下标从 0 到 2 的字符向后移位,得到 s = "ace" 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "dztz", shifts = [[0,0,0],[1,1,1]]
|
||||
<b>输出:</b>"catz"
|
||||
<b>解释:</b>首先,将下标从 0 到 0 的字母向前移位,得到 s = "cztz" 。
|
||||
最后,将下标从 1 到 1 的字符向后移位,得到 s = "catz" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, shifts.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>shifts[i].length == 3</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < s.length</code></li>
|
||||
<li><code>0 <= direction<sub>i</sub> <= 1</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个下表从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。每次操作中,你可以将数组中任何一个元素替换为 <strong>任意两个</strong> 和为该元素的数字。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>nums = [5,6,7]</code> 。一次操作中,我们可以将 <code>nums[1]</code> 替换成 <code>2</code> 和 <code>4</code> ,将 <code>nums</code> 转变成 <code>[5,2,4,7]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你执行上述操作,将数组变成元素按 <strong>非递减</strong> 顺序排列的数组,并返回所需的最少操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,9,3]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>以下是将数组变成非递减顺序的步骤:
|
||||
- [3,9,3] ,将9 变成 3 和 6 ,得到数组 [3,3,6,3]
|
||||
- [3,3,6,3] ,将 6 变成 3 和 3 ,得到数组 [3,3,3,3,3]
|
||||
总共需要 2 步将数组变成非递减有序,所以我们返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>数组已经是非递减顺序,所以我们返回 0 。
|
||||
</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>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的字符串 <code>blocks</code> ,<code>blocks[i]</code> 要么是 <code>'W'</code> 要么是 <code>'B'</code> ,表示第 <code>i</code> 块的颜色。字符 <code>'W'</code> 和 <code>'B'</code> 分别表示白色和黑色。</p>
|
||||
|
||||
<p>给你一个整数 <code>k</code> ,表示想要 <strong>连续</strong> 黑色块的数目。</p>
|
||||
|
||||
<p>每一次操作中,你可以选择一个白色块将它 <strong>涂成</strong> 黑色块。</p>
|
||||
|
||||
<p>请你返回至少出现 <strong>一次</strong> 连续 <code>k</code> 个黑色块的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>blocks = "WBBWWBBWBW", k = 7
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>
|
||||
一种得到 7 个连续黑色块的方法是把第 0 ,3 和 4 个块涂成黑色。
|
||||
得到 blocks = "BBBBBBBWBW" 。
|
||||
可以证明无法用少于 3 次操作得到 7 个连续的黑块。
|
||||
所以我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>blocks = "WBWBBBW", k = 2
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
不需要任何操作,因为已经有 2 个连续的黑块。
|
||||
所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == blocks.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>blocks[i]</code> 要么是 <code>'W'</code> ,要么是 <code>'B'</code> 。</li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一棵二叉树的根节点 <code>root</code> ,二叉树中节点的值 <strong>互不相同</strong> 。另给你一个整数 <code>start</code> 。在第 <code>0</code> 分钟,<strong>感染</strong> 将会从值为 <code>start</code> 的节点开始爆发。</p>
|
||||
|
||||
<p>每分钟,如果节点满足以下全部条件,就会被感染:</p>
|
||||
|
||||
<ul>
|
||||
<li>节点此前还没有感染。</li>
|
||||
<li>节点与一个已感染节点相邻。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回感染整棵树需要的分钟数<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;">
|
||||
<pre><strong>输入:</strong>root = [1,5,3,null,4,10,6,9,2], start = 3
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>节点按以下过程被感染:
|
||||
- 第 0 分钟:节点 3
|
||||
- 第 1 分钟:节点 1、10、6
|
||||
- 第 2 分钟:节点5
|
||||
- 第 3 分钟:节点 4
|
||||
- 第 4 分钟:节点 9 和 2
|
||||
感染整棵树需要 4 分钟,所以返回 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;">
|
||||
<pre><strong>输入:</strong>root = [1], start = 1
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>第 0 分钟,树中唯一一个节点处于感染状态,返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目在范围 <code>[1, 10<sup>5</sup>]</code> 内</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>每个节点的值 <strong>互不相同</strong></li>
|
||||
<li>树中必定存在值为 <code>start</code> 的节点</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。你可以选择数组的任一 <strong>子序列</strong> 并且对其全部元素求和。</p>
|
||||
|
||||
<p>数组的 <strong>第 k 大和</strong> 定义为:可以获得的第 <code>k</code> 个 <strong>最大</strong> 子序列和(子序列和允许出现重复)</p>
|
||||
|
||||
<p>返回数组的 <strong>第 k 大和</strong> 。</p>
|
||||
|
||||
<p>子序列是一个可以由其他数组删除某些或不删除元素排生而来的数组,且派生过程不改变剩余元素的顺序。</p>
|
||||
|
||||
<p><strong>注意:</strong>空子序列的和视作 <code>0</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [2,4,-2], k = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>所有可能获得的子序列和列出如下,按递减顺序排列:
|
||||
- 6、4、4、2、<strong><em>2</em></strong>、0、0、-2
|
||||
数组的第 5 大和是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,-2,3,4,-10,12], k = 16
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>数组的第 16 大和是 10 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= min(2000, 2<sup>n</sup>)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一个 <code>n</code> 个节点的 <strong>有向图</strong> ,节点编号为 <code>0</code> 到 <code>n - 1</code> ,每个节点 <strong>至多</strong> 有一条出边。</p>
|
||||
|
||||
<p>有向图用大小为 <code>n</code> 下标从 <strong>0</strong> 开始的数组 <code>edges</code> 表示,表示节点 <code>i</code> 有一条有向边指向 <code>edges[i]</code> 。如果节点 <code>i</code> 没有出边,那么 <code>edges[i] == -1</code> 。</p>
|
||||
|
||||
<p>同时给你两个节点 <code>node1</code> 和 <code>node2</code> 。</p>
|
||||
|
||||
<p>请你返回一个从 <code>node1</code> 和 <code>node2</code> 都能到达节点的编号,使节点 <code>node1</code> 和节点 <code>node2</code> 到这个节点的距离 <b>较大值最小化</b>。如果有多个答案,请返回 <strong>最小</strong> 的节点编号。如果答案不存在,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>注意 <code>edges</code> 可能包含环。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png" style="width: 321px; height: 161px;"></p>
|
||||
|
||||
<pre><b>输入:</b>edges = [2,2,3,-1], node1 = 0, node2 = 1
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>从节点 0 到节点 2 的距离为 1 ,从节点 1 到节点 2 的距离为 1 。
|
||||
两个距离的较大值为 1 。我们无法得到一个比 1 更小的较大值,所以我们返回节点 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png" style="width: 195px; height: 161px;"></p>
|
||||
|
||||
<pre><b>输入:</b>edges = [1,2,-1], node1 = 0, node2 = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>节点 0 到节点 2 的距离为 2 ,节点 2 到它自己的距离为 0 。
|
||||
两个距离的较大值为 2 。我们无法得到一个比 2 更小的较大值,所以我们返回节点 2 。
|
||||
</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>-1 <= edges[i] < n</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
<li><code>0 <= node1, node2 < n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个仅由数字(<code>0 - 9</code>)组成的字符串 <code>num</code> 。</p>
|
||||
|
||||
<p>请你找出能够使用 <code>num</code> 中数字形成的 <strong>最大回文</strong> 整数,并以字符串形式返回。该整数不含 <strong>前导零</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>你 <strong>无需</strong> 使用 <code>num</code> 中的所有数字,但你必须使用 <strong>至少</strong> 一个数字。</li>
|
||||
<li>数字可以重新排序。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "444947137"
|
||||
<strong>输出:</strong>"7449447"
|
||||
<strong>解释:</strong>
|
||||
从 "<em><strong>44494</strong></em><em><strong>7</strong></em>13<em><strong>7</strong></em>" 中选用数字 "4449477",可以形成回文整数 "7449447" 。
|
||||
可以证明 "7449447" 是能够形成的最大回文整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = "00009"
|
||||
<strong>输出:</strong>"9"
|
||||
<strong>解释:</strong>
|
||||
可以证明 "9" 能够形成的最大回文整数。
|
||||
注意返回的整数不应含前导零。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>num</code> 由数字(<code>0 - 9</code>)组成</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个由小写字母组成的字符串 <code>s</code> ,和一个整数 <code>k</code> 。如果满足下述条件,则可以将字符串 <code>t</code> 视作是 <strong>理想字符串</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>t</code> 是字符串 <code>s</code> 的一个子序列。</li>
|
||||
<li><code>t</code> 中每两个 <strong>相邻</strong> 字母在字母表中位次的绝对差值小于或等于 <code>k</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <strong>最长</strong> 理想字符串的长度。</p>
|
||||
|
||||
<p>字符串的子序列同样是一个字符串,并且子序列还满足:可以经由其他字符串删除某些字符(也可以不删除)但不改变剩余字符的顺序得到。</p>
|
||||
|
||||
<p><strong>注意:</strong>字母表顺序不会循环。例如,<code>'a'</code> 和 <code>'z'</code> 在字母表中位次的绝对差值是 <code>25</code> ,而不是 <code>1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "acfgbd", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>最长理想字符串是 "acbd" 。该字符串长度为 4 ,所以返回 4 。
|
||||
注意 "acfgbd" 不是理想字符串,因为 'c' 和 'f' 的字母表位次差值为 3 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcd", k = 3
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>最长理想字符串是 "abcd" ,该字符串长度为 4 ,所以返回 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 25</code></li>
|
||||
<li><code>s</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的字符串 <code>pattern</code> ,它包含两种字符,<code>'I'</code> 表示 <strong>上升</strong> ,<code>'D'</code> 表示 <strong>下降</strong> 。</p>
|
||||
|
||||
<p>你需要构造一个下标从 <strong>0</strong> 开始长度为 <code>n + 1</code> 的字符串,且它要满足以下条件:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num</code> 包含数字 <code>'1'</code> 到 <code>'9'</code> ,其中每个数字 <strong>至多</strong> 使用一次。</li>
|
||||
<li>如果 <code>pattern[i] == 'I'</code> ,那么 <code>num[i] < num[i + 1]</code> 。</li>
|
||||
<li>如果 <code>pattern[i] == 'D'</code> ,那么 <code>num[i] > num[i + 1]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足上述条件字典序 <strong>最小</strong> 的字符串<em> </em><code>num</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>pattern = "IIIDIDDD"
|
||||
<b>输出:</b>"123549876"
|
||||
<strong>解释:
|
||||
</strong>下标 0 ,1 ,2 和 4 处,我们需要使 num[i] < num[i+1] 。
|
||||
下标 3 ,5 ,6 和 7 处,我们需要使 num[i] > num[i+1] 。
|
||||
一些可能的 num 的值为 "245639871" ,"135749862" 和 "123849765" 。
|
||||
"123549876" 是满足条件最小的数字。
|
||||
注意,"123414321" 不是可行解因为数字 '1' 使用次数超过 1 次。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>pattern = "DDD"
|
||||
<b>输出:</b>"4321"
|
||||
<strong>解释:</strong>
|
||||
一些可能的 num 的值为 "9876" ,"7321" 和 "8742" 。
|
||||
"4321" 是满足条件最小的数字。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pattern.length <= 8</code></li>
|
||||
<li><code>pattern</code> 只包含字符 <code>'I'</code> 和 <code>'D'</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,你必须将数组划分为一个或多个 <strong>连续</strong> 子数组。</p>
|
||||
|
||||
<p>如果获得的这些子数组中每个都能满足下述条件<strong> 之一</strong> ,则可以称其为数组的一种 <strong>有效</strong> 划分:</p>
|
||||
|
||||
<ol>
|
||||
<li>子数组 <strong>恰</strong> 由 <code>2</code> 个相等元素组成,例如,子数组 <code>[2,2]</code> 。</li>
|
||||
<li>子数组 <strong>恰</strong> 由 <code>3</code> 个相等元素组成,例如,子数组 <code>[4,4,4]</code> 。</li>
|
||||
<li>子数组 <strong>恰</strong> 由 <code>3</code> 个连续递增元素组成,并且相邻元素之间的差值为 <code>1</code> 。例如,子数组 <code>[3,4,5]</code> ,但是子数组 <code>[1,3,5]</code> 不符合要求。</li>
|
||||
</ol>
|
||||
|
||||
<p>如果数组 <strong>至少</strong> 存在一种有效划分,返回 <code>true</code><em> </em>,否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,4,4,5,6]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>数组可以划分成子数组 [4,4] 和 [4,5,6] 。
|
||||
这是一种有效划分,所以返回 true 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1,2]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>该数组不存在有效划分。
|
||||
</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,43 @@
|
||||
<p>给你一个大小为 <code>n x n</code> 的整数矩阵 <code>grid</code> 。</p>
|
||||
|
||||
<p>生成一个大小为 <code>(n - 2) x (n - 2)</code> 的整数矩阵 <code>maxLocal</code> ,并满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>maxLocal[i][j]</code> 等于 <code>grid</code> 中以 <code>i + 1</code> 行和 <code>j + 1</code> 列为中心的 <code>3 x 3</code> 矩阵中的 <strong>最大值</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>换句话说,我们希望找出 <code>grid</code> 中每个 <code>3 x 3</code> 矩阵中的最大值。</p>
|
||||
|
||||
<p>返回生成的矩阵。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/06/21/ex1.png" style="width: 371px; height: 210px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
|
||||
<strong>输出:</strong>[[9,9],[8,6]]
|
||||
<strong>解释:</strong>原矩阵和生成的矩阵如上图所示。
|
||||
注意,生成的矩阵中,每个值都对应 grid 中一个相接的 3 x 3 矩阵的最大值。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png" style="width: 436px; height: 240px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
|
||||
<strong>输出:</strong>[[2,2,2],[2,2,2],[2,2,2]]
|
||||
<strong>解释:</strong>注意,2 包含在 grid 中每个 3 x 3 的矩阵中。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 100</code></li>
|
||||
<li><code>1 <= grid[i][j] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、<strong>严格递增</strong> 的整数数组 <code>nums</code> 和一个正整数 <code>diff</code> 。如果满足下述全部条件,则三元组 <code>(i, j, k)</code> 就是一个 <strong>算术三元组</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code> ,</li>
|
||||
<li><code>nums[j] - nums[i] == diff</code> 且</li>
|
||||
<li><code>nums[k] - nums[j] == diff</code></li>
|
||||
</ul>
|
||||
|
||||
<p>返回不同 <strong>算术三元组</strong> 的数目<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [0,1,4,6,7,10], diff = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
(1, 2, 4) 是算术三元组:7 - 4 == 3 且 4 - 1 == 3 。
|
||||
(2, 4, 5) 是算术三元组:10 - 7 == 3 且 7 - 4 == 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [4,5,6,7,8,9], diff = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
(0, 2, 4) 是算术三元组:8 - 6 == 2 且 6 - 4 == 2 。
|
||||
(1, 3, 5) 是算术三元组:9 - 7 == 2 且 7 - 5 == 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 200</code></li>
|
||||
<li><code>0 <= nums[i] <= 200</code></li>
|
||||
<li><code>1 <= diff <= 50</code></li>
|
||||
<li><code>nums</code> <strong>严格</strong> 递增</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个下标从<strong> 0</strong> 开始的整数数组 <code>nums</code> 。如果 <code>i < j</code> 且 <code>j - i != nums[j] - nums[i]</code> ,那么我们称 <code>(i, j)</code> 是一个 <strong>坏</strong><strong>数对</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中 <strong>坏数对</strong> 的总数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [4,1,3,3]
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>数对 (0, 1) 是坏数对,因为 1 - 0 != 1 - 4 。
|
||||
数对 (0, 2) 是坏数对,因为 2 - 0 != 3 - 4, 2 != -1 。
|
||||
数对 (0, 3) 是坏数对,因为 3 - 0 != 3 - 4, 3 != -1 。
|
||||
数对 (1, 2) 是坏数对,因为 2 - 1 != 3 - 1, 1 != 2 。
|
||||
数对 (2, 3) 是坏数对,因为 3 - 2 != 3 - 3, 1 != 0 。
|
||||
总共有 5 个坏数对,所以我们返回 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,4,5]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>没有坏数对。
|
||||
</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>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>如果一个正整数每一个数位都是 <strong>互不相同</strong> 的,我们称它是 <strong>特殊整数</strong> 。</p>
|
||||
|
||||
<p>给你一个 <strong>正</strong> 整数 <code>n</code> ,请你返回区间<em> </em><code>[1, n]</code> 之间特殊整数的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 20
|
||||
<b>输出:</b>19
|
||||
<b>解释:</b>1 到 20 之间所有整数除了 11 以外都是特殊整数。所以总共有 19 个特殊整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>1 到 5 所有整数都是特殊整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 135
|
||||
<b>输出:</b>110
|
||||
<b>解释:</b>从 1 到 135 总共有 110 个整数是特殊整数。
|
||||
不特殊的部分数字为:22 ,114 和 131 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>你正在参加一场比赛,给你两个 <strong>正</strong> 整数 <code>initialEnergy</code> 和 <code>initialExperience</code> 分别表示你的初始精力和初始经验。</p>
|
||||
|
||||
<p>另给你两个下标从 <strong>0</strong> 开始的整数数组 <code>energy</code> 和 <code>experience</code>,长度均为 <code>n</code> 。</p>
|
||||
|
||||
<p>你将会 <strong>依次</strong> 对上 <code>n</code> 个对手。第 <code>i</code> 个对手的精力和经验分别用 <code>energy[i]</code> 和 <code>experience[i]</code> 表示。当你对上对手时,需要在经验和精力上都 <strong>严格</strong> 超过对手才能击败他们,然后在可能的情况下继续对上下一个对手。</p>
|
||||
|
||||
<p>击败第 <code>i</code> 个对手会使你的经验 <strong>增加</strong> <code>experience[i]</code>,但会将你的精力 <strong>减少</strong> <code>energy[i]</code> 。</p>
|
||||
|
||||
<p>在开始比赛前,你可以训练几个小时。每训练一个小时,你可以选择将增加经验增加 1 <strong>或者</strong> 将精力增加 1 。</p>
|
||||
|
||||
<p>返回击败全部 <code>n</code> 个对手需要训练的 <strong>最少</strong> 小时数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>在 6 小时训练后,你可以将精力提高到 11 ,并且再训练 2 个小时将经验提高到 5 。
|
||||
按以下顺序与对手比赛:
|
||||
- 你的精力与经验都超过第 0 个对手,所以获胜。
|
||||
精力变为:11 - 1 = 10 ,经验变为:5 + 2 = 7 。
|
||||
- 你的精力与经验都超过第 1 个对手,所以获胜。
|
||||
精力变为:10 - 4 = 6 ,经验变为:7 + 6 = 13 。
|
||||
- 你的精力与经验都超过第 2 个对手,所以获胜。
|
||||
精力变为:6 - 3 = 3 ,经验变为:13 + 3 = 16 。
|
||||
- 你的精力与经验都超过第 3 个对手,所以获胜。
|
||||
精力变为:3 - 2 = 1 ,经验变为:16 + 1 = 17 。
|
||||
在比赛前进行了 8 小时训练,所以返回 8 。
|
||||
可以证明不存在更小的答案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>你不需要额外的精力和经验就可以赢得比赛,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == energy.length == experience.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个有向图,图中有 <code>n</code> 个节点,节点编号从 <code>0</code> 到 <code>n - 1</code> ,其中每个节点都 <strong>恰有一条</strong> 出边。</p>
|
||||
|
||||
<p>图由一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的整数数组 <code>edges</code> 表示,其中 <code>edges[i]</code> 表示存在一条从节点 <code>i</code> 到节点 <code>edges[i]</code> 的 <strong>有向</strong> 边。</p>
|
||||
|
||||
<p>节点 <code>i</code> 的 <strong>边积分</strong> 定义为:所有存在一条指向节点 <code>i</code> 的边的节点的 <strong>编号</strong> 总和。</p>
|
||||
|
||||
<p>返回 <strong>边积分</strong> 最高的节点。如果多个节点的 <strong>边积分</strong> 相同,返回编号 <strong>最小</strong> 的那个。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620195403-1.png" style="width: 450px; height: 260px;">
|
||||
<pre><strong>输入:</strong>edges = [1,0,0,0,0,7,7,5]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>
|
||||
- 节点 1、2、3 和 4 都有指向节点 0 的边,节点 0 的边积分等于 1 + 2 + 3 + 4 = 10 。
|
||||
- 节点 0 有一条指向节点 1 的边,节点 1 的边积分等于 0 。
|
||||
- 节点 7 有一条指向节点 5 的边,节点 5 的边积分等于 7 。
|
||||
- 节点 5 和 6 都有指向节点 7 的边,节点 7 的边积分等于 5 + 6 = 11 。
|
||||
节点 7 的边积分最高,所以返回 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620200212-3.png" style="width: 150px; height: 155px;">
|
||||
<pre><strong>输入:</strong>edges = [2,0,0,2]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:
|
||||
</strong>- 节点 1 和 2 都有指向节点 0 的边,节点 0 的边积分等于 1 + 2 = 3 。
|
||||
- 节点 0 和 3 都有指向节点 2 的边,节点 2 的边积分等于 0 + 3 = 3 。
|
||||
节点 0 和 2 的边积分都是 3 。由于节点 0 的编号更小,返回 0 。
|
||||
</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</code></li>
|
||||
<li><code>edges[i] != i</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user