mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
update
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<p>有一棵 <code>n</code> 个节点的无向树,节点编号为 <code>0</code> 到 <code>n - 1</code> ,根节点编号为 <code>0</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>同时给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>values</code> ,其中 <code>values[i]</code> 表示第 <code>i</code> 个节点的值。</p>
|
||||
|
||||
<p>一开始你的分数为 <code>0</code> ,每次操作中,你将执行:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择节点 <code>i</code> 。</li>
|
||||
<li>将 <code>values[i]</code> 加入你的分数。</li>
|
||||
<li>将 <code>values[i]</code> 变为 <code>0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果从根节点出发,到任意叶子节点经过的路径上的节点值之和都不等于 0 ,那么我们称这棵树是 <strong>健康的</strong> 。</p>
|
||||
|
||||
<p>你可以对这棵树执行任意次操作,但要求执行完所有操作以后树是 <strong>健康的</strong> ,请你返回你可以获得的 <strong>最大分数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png" style="width: 515px; height: 443px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>我们可以选择节点 1 ,2 ,3 ,4 和 5 。根节点的值是非 0 的。所以从根出发到任意叶子节点路径上节点值之和都不为 0 。所以树是健康的。你的得分之和为 values[1] + values[2] + values[3] + values[4] + values[5] = 11 。
|
||||
11 是你对树执行任意次操作以后可以获得的最大得分之和。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png" style="width: 522px; height: 245px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
|
||||
<b>输出:</b>40
|
||||
<b>解释:</b>我们选择节点 0 ,2 ,3 和 4 。
|
||||
- 从 0 到 4 的节点值之和为 10 。
|
||||
- 从 0 到 3 的节点值之和为 10 。
|
||||
- 从 0 到 5 的节点值之和为 3 。
|
||||
- 从 0 到 6 的节点值之和为 5 。
|
||||
所以树是健康的。你的得分之和为 values[0] + values[2] + values[3] + values[4] = 40 。
|
||||
40 是你对树执行任意次操作以后可以获得的最大得分之和。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 2 * 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>1 <= values[i] <= 10<sup>9</sup></code></li>
|
||||
<li>输入保证 <code>edges</code> 构成一棵合法的树。</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p><code>nums</code> 一个长度为 <code>k</code> 的 <strong>子序列</strong> 指的是选出 <code>k</code> 个 <strong>下标</strong> <code>i<sub>0</sub> < i<sub>1</sub> < ... < i<sub>k-1</sub></code> ,如果这个子序列满足以下条件,我们说它是 <strong>平衡的</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>对于范围 <code>[1, k - 1]</code> 内的所有 <code>j</code> ,<code>nums[i<sub>j</sub>] - nums[i<sub>j-1</sub>] >= i<sub>j</sub> - i<sub>j-1</sub></code> 都成立。</li>
|
||||
</ul>
|
||||
|
||||
<p><code>nums</code> 长度为 <code>1</code> 的 <strong>子序列</strong> 是平衡的。</p>
|
||||
|
||||
<p>请你返回一个整数,表示 <code>nums</code> <strong>平衡</strong> 子序列里面的 <strong>最大元素和</strong> 。</p>
|
||||
|
||||
<p>一个数组的 <strong>子序列</strong> 指的是从原数组中删除一些元素(<strong>也可能一个元素也不删除</strong>)后,剩余元素保持相对顺序得到的 <strong>非空</strong> 新数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,3,5,6]
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>这个例子中,选择子序列 [3,5,6] ,下标为 0 ,2 和 3 的元素被选中。
|
||||
nums[2] - nums[0] >= 2 - 0 。
|
||||
nums[3] - nums[2] >= 3 - 2 。
|
||||
所以,这是一个平衡子序列,且它的和是所有平衡子序列里最大的。
|
||||
包含下标 1 ,2 和 3 的子序列也是一个平衡的子序列。
|
||||
最大平衡子序列和为 14 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [5,-1,-3,8]
|
||||
<b>输出:</b>13
|
||||
<b>解释:</b>这个例子中,选择子序列 [5,8] ,下标为 0 和 3 的元素被选中。
|
||||
nums[3] - nums[0] >= 3 - 0 。
|
||||
所以,这是一个平衡子序列,且它的和是所有平衡子序列里最大的。
|
||||
最大平衡子序列和为 13 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [-2,-1]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>这个例子中,选择子序列 [-1] 。
|
||||
这是一个平衡子序列,而且它的和是 nums 所有平衡子序列里最大的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。如果一对整数 <code>x</code> 和 <code>y</code> 满足以下条件,则称其为 <strong>强数对</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>你需要从 <code>nums</code> 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(<code>XOR</code>)值是在该数组所有强数对中的<strong> 最大值 </strong>。</p>
|
||||
|
||||
<p>返回数组 <code>nums</code> 所有可能的强数对中的<strong> 最大 </strong>异或值。</p>
|
||||
|
||||
<p><strong>注意</strong>,你可以选择同一个整数两次来形成一个强数对。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4,5]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>数组<code> nums </code>中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) 和 (5, 5) 。
|
||||
这些强数对中的最大异或值是 3 XOR 4 = 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [10,100]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>数组<code> nums </code>中有 2 个强数对:(10, 10) 和 (100, 100) 。
|
||||
这些强数对中的最大异或值是 10 XOR 10 = 0 ,数对 (100, 100) 的异或值也是 100 XOR 100 = 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,6,25,30]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>数组<code> nums </code>中有 6 个强数对:(5, 5), (5, 6), (6, 6), (25, 25), (25, 30) 和 (30, 30) 。
|
||||
这些强数对中的最大异或值是 25 XOR 30 = 7 ;另一个异或值非零的数对是 (5, 6) ,其异或值是 5 XOR 6 = 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。如果一对整数 <code>x</code> 和 <code>y</code> 满足以下条件,则称其为 <strong>强数对</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>你需要从 <code>nums</code> 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(<code>XOR</code>)值是在该数组所有强数对中的<strong> 最大值 </strong>。</p>
|
||||
|
||||
<p>返回数组 <code>nums</code> 所有可能的强数对中的<strong> 最大 </strong>异或值。</p>
|
||||
|
||||
<p><strong>注意</strong>,你可以选择同一个整数两次来形成一个强数对。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4,5]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>数组<code> nums </code>中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) 和 (5, 5) 。
|
||||
这些强数对中的最大异或值是 3 XOR 4 = 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [10,100]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>数组<code> nums </code>中有 2 个强数对:(10, 10) 和 (100, 100) 。
|
||||
这些强数对中的最大异或值是 10 XOR 10 = 0 ,数对 (100, 100) 的异或值也是 100 XOR 100 = 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [500,520,2500,3000]
|
||||
<strong>输出:</strong>1020
|
||||
<strong>解释:</strong>数组<code> nums </code>中有 6 个强数对:(500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) 和 (3000, 3000) 。
|
||||
这些强数对中的最大异或值是 500 XOR 520 = 1020 ;另一个异或值非零的数对是 (5, 6) ,其异或值是 2500 XOR 3000 = 636 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 2<sup>20</sup> - 1</code></li>
|
||||
</ul>
|
43
leetcode-cn/problem (Chinese)/找到冠军 I [find-champion-i].html
Normal file
43
leetcode-cn/problem (Chinese)/找到冠军 I [find-champion-i].html
Normal file
@@ -0,0 +1,43 @@
|
||||
<p>一场比赛中共有 <code>n</code> 支队伍,按从 <code>0</code> 到 <code>n - 1</code> 编号。</p>
|
||||
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>n * n</code> 的二维布尔矩阵 <code>grid</code> 。对于满足 <code>0 <= i, j <= n - 1</code> 且 <code>i != j</code> 的所有 <code>i, j</code> :如果 <code>grid[i][j] == 1</code>,那么 <code>i</code> 队比 <code>j</code> 队 <strong>强</strong> ;否则,<code>j</code> 队比 <code>i</code> 队 <strong>强</strong> 。</p>
|
||||
|
||||
<p>在这场比赛中,如果不存在某支强于 <code>a</code> 队的队伍,则认为 <code>a</code> 队将会是 <strong>冠军</strong> 。</p>
|
||||
|
||||
<p>返回这场比赛中将会成为冠军的队伍。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1],[0,0]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>比赛中有两支队伍。
|
||||
grid[0][1] == 1 表示 0 队比 1 队强。所以 0 队是冠军。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,0,1],[1,0,1],[0,0,0]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>比赛中有三支队伍。
|
||||
grid[1][0] == 1 表示 1 队比 0 队强。
|
||||
grid[1][2] == 1 表示 1 队比 2 队强。
|
||||
所以 1 队是冠军。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> 的值为 <code>0</code> 或 <code>1</code><meta charset="UTF-8" /></li>
|
||||
<li>对于所有 <code>i</code>,<code> grid[i][i]</code> 等于 <code>0.</code></li>
|
||||
<li>对于满足 <code>i != j</code> 的所有 <code>i, j</code> ,<code>grid[i][j] != grid[j][i]</code> 均成立</li>
|
||||
<li>生成的输入满足:如果 <code>a</code> 队比 <code>b</code> 队强,<code>b</code> 队比 <code>c</code> 队强,那么 <code>a</code> 队比 <code>c</code> 队强</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>一场比赛中共有 <code>n</code> 支队伍,按从 <code>0</code> 到 <code>n - 1</code> 编号。每支队伍也是 <strong>有向无环图(DAG)</strong> 上的一个节点。</p>
|
||||
|
||||
<p>给你一个整数 <code>n</code> 和一个下标从 <strong>0</strong> 开始、长度为 <code>m</code> 的二维整数数组 <code>edges</code> 表示这个有向无环图,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示图中存在一条从 <code>u<sub>i</sub></code> 队到 <code>v<sub>i</sub></code> 队的有向边。</p>
|
||||
|
||||
<p>从 <code>a</code> 队到 <code>b</code> 队的有向边意味着 <code>a</code> 队比 <code>b</code> 队 <strong>强</strong> ,也就是 <code>b</code> 队比 <code>a</code> 队 <strong>弱</strong> 。</p>
|
||||
|
||||
<p>在这场比赛中,如果不存在某支强于 <code>a</code> 队的队伍,则认为 <code>a</code> 队将会是 <strong>冠军</strong> 。</p>
|
||||
|
||||
<p>如果这场比赛存在 <strong>唯一</strong> 一个冠军,则返回将会成为冠军的队伍。否则,返回<em> </em><code>-1</code><em> 。</em></p>
|
||||
|
||||
<p><strong>注意</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>环</strong> 是形如 <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, a<sub>n+1</sub></code> 的一个序列,且满足:节点 <code>a<sub>1</sub></code> 与节点 <code>a<sub>n+1</sub></code> 是同一个节点;节点 <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> 互不相同;对于范围 <code>[1, n]</code> 中的每个 <code>i</code> ,均存在一条从节点 <code>a<sub>i</sub></code> 到节点 <code>a<sub>i+1</sub></code> 的有向边。</li>
|
||||
<li><strong>有向无环图</strong> 是不存在任何环的有向图。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img height="300" src="https://assets.leetcode.com/uploads/2023/10/19/graph-3.png" width="300" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, edges = [[0,1],[1,2]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>1 队比 0 队弱。2 队比 1 队弱。所以冠军是 0 队。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img height="300" src="https://assets.leetcode.com/uploads/2023/10/19/graph-4.png" width="300" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, edges = [[0,2],[1,3],[1,2]]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>2 队比 0 队和 1 队弱。3 队比 1 队弱。但是 1 队和 0 队之间不存在强弱对比。所以答案是 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>m == edges.length</code></li>
|
||||
<li><code>0 <= m <= n * (n - 1) / 2</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= edge[i][j] <= n - 1</code></li>
|
||||
<li><code>edges[i][0] != edges[i][1]</code></li>
|
||||
<li>生成的输入满足:如果 <code>a</code> 队比 <code>b</code> 队强,就不存在 <code>b</code> 队比 <code>a</code> 队强</li>
|
||||
<li>生成的输入满足:如果 <code>a</code> 队比 <code>b</code> 队强,<code>b</code> 队比 <code>c</code> 队强,那么 <code>a</code> 队比 <code>c</code> 队强</li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ,这两个数组的长度都是 <code>n</code> 。</p>
|
||||
|
||||
<p>你可以执行一系列<strong> 操作(可能不执行)</strong>。</p>
|
||||
|
||||
<p>在每次操作中,你可以选择一个在范围 <code>[0, n - 1]</code> 内的下标 <code>i</code> ,并交换 <code>nums1[i]</code> 和 <code>nums2[i]</code> 的值。</p>
|
||||
|
||||
<p>你的任务是找到满足以下条件所需的 <strong>最小</strong> 操作次数:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1[n - 1]</code> 等于 <code>nums1</code> 中所有元素的 <strong>最大值</strong> ,即 <code>nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])</code> 。</li>
|
||||
<li><code>nums2[n - 1]</code> 等于 <code>nums2</code> 中所有元素的 <strong>最大值</strong> ,即 <code>nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>以整数形式,表示并返回满足上述 <strong>全部</strong> 条件所需的 <strong>最小</strong> 操作次数,如果无法同时满足两个条件,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [1,2,7],nums2 = [4,5,3]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>在这个示例中,可以选择下标 i = 2 执行一次操作。
|
||||
交换 nums1[2] 和 nums2[2] 的值,nums1 变为 [1,2,3] ,nums2 变为 [4,5,7] 。
|
||||
同时满足两个条件。
|
||||
可以证明,需要执行的最小操作次数为 1 。
|
||||
因此,答案是 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [2,3,4,5,9],nums2 = [8,8,4,4,4]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>在这个示例中,可以执行以下操作:
|
||||
首先,选择下标 i = 4 执行操作。
|
||||
交换 nums1[4] 和 nums2[4] 的值,nums1 变为 [2,3,4,5,4] ,nums2 变为 [8,8,4,4,9] 。
|
||||
然后,选择下标 i = 3 执行操作。
|
||||
交换 nums1[3] 和 nums2[3] 的值,nums1 变为 [2,3,4,4,4] ,nums2 变为 [8,8,4,5,9] 。
|
||||
同时满足两个条件。
|
||||
可以证明,需要执行的最小操作次数为 2 。
|
||||
因此,答案是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [1,5,4],nums2 = [2,5,3]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>在这个示例中,无法同时满足两个条件。
|
||||
因此,答案是 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums1.length == nums2.length <= 1000</code></li>
|
||||
<li><code>1 <= nums1[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= nums2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你两个正整数 <code>n</code> 和 <code>limit</code> 。</p>
|
||||
|
||||
<p>请你将 <code>n</code> 颗糖果分给 <code>3</code> 位小朋友,确保没有任何小朋友得到超过 <code>limit</code> 颗糖果,请你返回满足此条件下的 <strong>总方案数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, limit = 2
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, limit = 3
|
||||
<b>输出:</b>10
|
||||
<b>解释:</b>总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>1 <= limit <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你两个正整数 <code>n</code> 和 <code>limit</code> 。</p>
|
||||
|
||||
<p>请你将 <code>n</code> 颗糖果分给 <code>3</code> 位小朋友,确保没有任何小朋友得到超过 <code>limit</code> 颗糖果,请你返回满足此条件下的 <strong>总方案数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, limit = 2
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, limit = 3
|
||||
<b>输出:</b>10
|
||||
<b>解释:</b>总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= limit <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始大小为 <code>m * n</code> 的整数矩阵 <code>values</code> ,表示 <code>m</code> 个不同商店里 <code>m * n</code> 件不同的物品。每个商店有 <code>n</code> 件物品,第 <code>i</code> 个商店的第 <code>j</code> 件物品的价值为 <code>values[i][j]</code> 。除此以外,第 <code>i</code> 个商店的物品已经按照价值非递增排好序了,也就是说对于所有 <code>0 <= j < n - 1</code> 都有 <code>values[i][j] >= values[i][j + 1]</code> 。</p>
|
||||
|
||||
<p>每一天,你可以在一个商店里购买一件物品。具体来说,在第 <code>d</code> 天,你可以:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择商店 <code>i</code> 。</li>
|
||||
<li>购买数组中最右边的物品 <code>j</code> ,开销为 <code>values[i][j] * d</code> 。换句话说,选择该商店中还没购买过的物品中最大的下标 <code>j</code> ,并且花费 <code>values[i][j] * d</code> 去购买。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意</strong>,所有物品都视为不同的物品。比方说如果你已经从商店 <code>1</code> 购买了物品 <code>0</code> ,你还可以在别的商店里购买其他商店的物品 <code>0</code> 。</p>
|
||||
|
||||
<p>请你返回购买所有 <code>m * n</code> 件物品需要的 <strong>最大开销</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>values = [[8,5,2],[6,4,1],[9,7,3]]
|
||||
<b>输出:</b>285
|
||||
<b>解释:</b>第一天,从商店 1 购买物品 2 ,开销为 values[1][2] * 1 = 1 。
|
||||
第二天,从商店 0 购买物品 2 ,开销为 values[0][2] * 2 = 4 。
|
||||
第三天,从商店 2 购买物品 2 ,开销为 values[2][2] * 3 = 9 。
|
||||
第四天,从商店 1 购买物品 1 ,开销为 values[1][1] * 4 = 16 。
|
||||
第五天,从商店 0 购买物品 1 ,开销为 values[0][1] * 5 = 25 。
|
||||
第六天,从商店 1 购买物品 0 ,开销为 values[1][0] * 6 = 36 。
|
||||
第七天,从商店 2 购买物品 1 ,开销为 values[2][1] * 7 = 49 。
|
||||
第八天,从商店 0 购买物品 0 ,开销为 values[0][0] * 8 = 64 。
|
||||
第九天,从商店 2 购买物品 0 ,开销为 values[2][0] * 9 = 81 。
|
||||
所以总开销为 285 。
|
||||
285 是购买所有 m * n 件物品的最大总开销。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>values = [[10,8,6,4,2],[9,7,5,3,2]]
|
||||
<b>输出:</b>386
|
||||
<b>解释:</b>第一天,从商店 0 购买物品 4 ,开销为 values[0][4] * 1 = 2 。
|
||||
第二天,从商店 1 购买物品 4 ,开销为 values[1][4] * 2 = 4 。
|
||||
第三天,从商店 1 购买物品 3 ,开销为 values[1][3] * 3 = 9 。
|
||||
第四天,从商店 0 购买物品 3 ,开销为 values[0][3] * 4 = 16 。
|
||||
第五天,从商店 1 购买物品 2 ,开销为 values[1][2] * 5 = 25 。
|
||||
第六天,从商店 0 购买物品 2 ,开销为 values[0][2] * 6 = 36 。
|
||||
第七天,从商店 1 购买物品 1 ,开销为 values[1][1] * 7 = 49 。
|
||||
第八天,从商店 0 购买物品 1 ,开销为 values[0][1] * 8 = 64 。
|
||||
第九天,从商店 1 购买物品 0 ,开销为 values[1][0] * 9 = 81 。
|
||||
第十天,从商店 0 购买物品 0 ,开销为 values[0][0] * 10 = 100 。
|
||||
所以总开销为 386 。
|
||||
386 是购买所有 m * n 件物品的最大总开销。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m == values.length <= 10</code></li>
|
||||
<li><code>1 <= n == values[i].length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= values[i][j] <= 10<sup>6</sup></code></li>
|
||||
<li><code>values[i]</code> 按照非递增顺序排序。</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个整数 <code>n</code> 。</p>
|
||||
|
||||
<p>如果一个字符串 <code>s</code> 只包含小写英文字母,<strong>且</strong> 将 <code>s</code> 的字符重新排列后,新字符串包含 <strong>子字符串</strong> <code>"leet"</code> ,那么我们称字符串 <code>s</code> 是一个 <strong>好</strong> 字符串。</p>
|
||||
|
||||
<p>比方说:</p>
|
||||
|
||||
<ul>
|
||||
<li>字符串 <code>"lteer"</code> 是好字符串,因为重新排列后可以得到 <code>"leetr"</code> 。</li>
|
||||
<li><code>"letl"</code> 不是好字符串,因为无法重新排列并得到子字符串 <code>"leet"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回长度为 <code>n</code> 的好字符串 <strong>总</strong> 数目。</p>
|
||||
|
||||
<p>由于答案可能很大,将答案对<strong> </strong><code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p><strong>子字符串</strong> 是一个字符串中一段连续的字符序列。</p>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 4
|
||||
<b>输出:</b>12
|
||||
<b>解释:</b>总共有 12 个字符串重新排列后包含子字符串 "leet" :"eelt" ,"eetl" ,"elet" ,"elte" ,"etel" ,"etle" ,"leet" ,"lete" ,"ltee" ,"teel" ,"tele" 和 "tlee" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 10
|
||||
<b>输出:</b>83943898
|
||||
<b>解释:</b>长度为 10 的字符串重新排列后包含子字符串 "leet" 的方案数为 526083947580 。所以答案为 526083947580 % (10<sup>9</sup> + 7) = 83943898 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>给你一个长度为 <code>n</code> 、下标从 <strong>0</strong> 开始的二维字符串数组 <code>access_times</code> 。对于每个 <code>i</code>(<code>0 <= i <= n - 1</code> ),<code>access_times[i][0]</code> 表示某位员工的姓名,<code>access_times[i][1]</code> 表示该员工的访问时间。<code>access_times</code> 中的所有条目都发生在同一天内。</p>
|
||||
|
||||
<p>访问时间用 <strong>四位</strong> 数字表示, 符合 <strong>24 小时制</strong> ,例如 <code>"0800"</code> 或 <code>"2250"</code> 。</p>
|
||||
|
||||
<p>如果员工在 <strong>同一小时内</strong> 访问系统 <strong>三次或更多</strong> ,则称其为 <strong>高访问</strong> 员工。</p>
|
||||
|
||||
<p>时间间隔正好相差一小时的时间 <strong>不</strong> 被视为同一小时内。例如,<code>"0815"</code> 和 <code>"0915"</code> 不属于同一小时内。</p>
|
||||
|
||||
<p>一天开始和结束时的访问时间不被计算为同一小时内。例如,<code>"0005"</code> 和 <code>"2350"</code> 不属于同一小时内。</p>
|
||||
|
||||
<p>以列表形式,按任意顺序,返回所有 <strong>高访问</strong> 员工的姓名。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
|
||||
<strong>输出:</strong>["a"]
|
||||
<strong>解释:</strong>"a" 在时间段 [05:32, 06:31] 内有三条访问记录,时间分别为 05:32 、05:49 和 06:21 。
|
||||
但是 "b" 的访问记录只有两条。
|
||||
因此,答案是 ["a"] 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
|
||||
<strong>输出:</strong>["c","d"]
|
||||
<strong>解释:</strong>"c" 在时间段 [08:08, 09:07] 内有三条访问记录,时间分别为 08:08 、08:09 和 08:29 。
|
||||
"d" 在时间段 [14:10, 15:09] 内有三条访问记录,时间分别为 14:10 、14:44 和 15:08 。
|
||||
然而,"e" 只有一条访问记录,因此不能包含在答案中,最终答案是 ["c","d"] 。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
|
||||
<strong>输出:</strong>["ab","cd"]
|
||||
<strong>解释:</strong>"ab"在时间段 [10:25, 11:24] 内有三条访问记录,时间分别为 10:25 、11:20 和 11:24 。
|
||||
"cd" 在时间段 [10:25, 11:24] 内有三条访问记录,时间分别为 10:25 、10:46 和 10:55 。
|
||||
因此,答案是 ["ab","cd"] 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= access_times.length <= 100</code></li>
|
||||
<li><code>access_times[i].length == 2</code></li>
|
||||
<li><code>1 <= access_times[i][0].length <= 10</code></li>
|
||||
<li><code>access_times[i][0]</code> 仅由小写英文字母组成。</li>
|
||||
<li><code>access_times[i][1].length == 4</code></li>
|
||||
<li><code>access_times[i][1]</code> 采用24小时制表示时间。</li>
|
||||
<li><code>access_times[i][1]</code> 仅由数字 <code>'0'</code> 到 <code>'9'</code> 组成。</li>
|
||||
</ul>
|
Reference in New Issue
Block a user