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
4cea624fbf
commit
7d652958ba
README.md
leetcode-cn
origin-data.json
originData
[no content]distribute-candies-among-children-iii.jsondistribute-candies-among-children-i.jsondistribute-candies-among-children-ii.jsonfind-champion-i.jsonfind-champion-ii.jsonhigh-access-employees.jsonmaximum-balanced-subsequence-sum.jsonmaximum-score-after-applying-operations-on-a-tree.jsonmaximum-spending-after-buying-items.jsonmaximum-strong-pair-xor-i.jsonmaximum-strong-pair-xor-ii.jsonminimum-operations-to-maximize-last-elements-in-arrays.jsonnumber-of-strings-which-can-be-rearranged-to-contain-substring.json
problem (Chinese)
在树上执行操作以后得到的最大分数 [maximum-score-after-applying-operations-on-a-tree].html平衡子序列的最大和 [maximum-balanced-subsequence-sum].html找出强数对的最大异或值 I [maximum-strong-pair-xor-i].html找出强数对的最大异或值 II [maximum-strong-pair-xor-ii].html找到冠军 I [find-champion-i].html找到冠军 II [find-champion-ii].html最大化数组末位元素的最少操作次数 [minimum-operations-to-maximize-last-elements-in-arrays].html给小朋友们分糖果 I [distribute-candies-among-children-i].html给小朋友们分糖果 II [distribute-candies-among-children-ii].html购买物品的最大开销 [maximum-spending-after-buying-items].html重新排列后包含指定子字符串的字符串数目 [number-of-strings-which-can-be-rearranged-to-contain-substring].html高访问员工 [high-access-employees].html
problem (English)
在树上执行操作以后得到的最大分数(English) [maximum-score-after-applying-operations-on-a-tree].html平衡子序列的最大和(English) [maximum-balanced-subsequence-sum].html找出强数对的最大异或值 I(English) [maximum-strong-pair-xor-i].html找出强数对的最大异或值 II(English) [maximum-strong-pair-xor-ii].html找到冠军 I(English) [find-champion-i].html找到冠军 II(English) [find-champion-ii].html最大化数组末位元素的最少操作次数(English) [minimum-operations-to-maximize-last-elements-in-arrays].html给小朋友们分糖果 I(English) [distribute-candies-among-children-i].html给小朋友们分糖果 II(English) [distribute-candies-among-children-ii].html购买物品的最大开销(English) [maximum-spending-after-buying-items].html重新排列后包含指定子字符串的字符串数目(English) [number-of-strings-which-can-be-rearranged-to-contain-substring].html高访问员工(English) [high-access-employees].html
leetcode
origin-data.json
originData
[no content]distribute-candies-among-children-iii.jsondistribute-candies-among-children-i.jsondistribute-candies-among-children-ii.jsonfind-champion-i.jsonfind-champion-ii.jsonhigh-access-employees.jsonmaximum-balanced-subsequence-sum.jsonmaximum-score-after-applying-operations-on-a-tree.jsonmaximum-spending-after-buying-items.jsonmaximum-strong-pair-xor-i.jsonmaximum-strong-pair-xor-ii.jsonminimum-operations-to-maximize-last-elements-in-arrays.jsonnumber-of-strings-which-can-be-rearranged-to-contain-substring.json
problem
distribute-candies-among-children-i.htmldistribute-candies-among-children-ii.htmlfind-champion-i.htmlfind-champion-ii.htmlhigh-access-employees.htmlmaximum-balanced-subsequence-sum.htmlmaximum-score-after-applying-operations-on-a-tree.htmlmaximum-spending-after-buying-items.htmlmaximum-strong-pair-xor-i.htmlmaximum-strong-pair-xor-ii.htmlminimum-operations-to-maximize-last-elements-in-arrays.htmlnumber-of-strings-which-can-be-rearranged-to-contain-substring.html
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.11.03**
|
||||
> 最后更新日期: **2023.11.13**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/distribute-candies-among-children-i.json
Normal file
163
leetcode-cn/originData/distribute-candies-among-children-i.json
Normal file
File diff suppressed because one or more lines are too long
166
leetcode-cn/originData/distribute-candies-among-children-ii.json
Normal file
166
leetcode-cn/originData/distribute-candies-among-children-ii.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/find-champion-i.json
Normal file
163
leetcode-cn/originData/find-champion-i.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/find-champion-ii.json
Normal file
163
leetcode-cn/originData/find-champion-ii.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/high-access-employees.json
Normal file
164
leetcode-cn/originData/high-access-employees.json
Normal file
File diff suppressed because one or more lines are too long
170
leetcode-cn/originData/maximum-balanced-subsequence-sum.json
Normal file
170
leetcode-cn/originData/maximum-balanced-subsequence-sum.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
164
leetcode-cn/originData/maximum-spending-after-buying-items.json
Normal file
164
leetcode-cn/originData/maximum-spending-after-buying-items.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/maximum-strong-pair-xor-i.json
Normal file
163
leetcode-cn/originData/maximum-strong-pair-xor-i.json
Normal file
File diff suppressed because one or more lines are too long
166
leetcode-cn/originData/maximum-strong-pair-xor-ii.json
Normal file
166
leetcode-cn/originData/maximum-strong-pair-xor-ii.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
164
leetcode-cn/originData/number-of-strings-which-can-be-rearranged-to-contain-substring.json
Normal file
164
leetcode-cn/originData/number-of-strings-which-can-be-rearranged-to-contain-substring.json
Normal file
File diff suppressed because one or more lines are too long
58
leetcode-cn/problem (Chinese)/在树上执行操作以后得到的最大分数 [maximum-score-after-applying-operations-on-a-tree].html
Normal file
58
leetcode-cn/problem (Chinese)/在树上执行操作以后得到的最大分数 [maximum-score-after-applying-operations-on-a-tree].html
Normal file
@ -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>
|
62
leetcode-cn/problem (Chinese)/最大化数组末位元素的最少操作次数 [minimum-operations-to-maximize-last-elements-in-arrays].html
Normal file
62
leetcode-cn/problem (Chinese)/最大化数组末位元素的最少操作次数 [minimum-operations-to-maximize-last-elements-in-arrays].html
Normal file
@ -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>
|
30
leetcode-cn/problem (Chinese)/给小朋友们分糖果 II [distribute-candies-among-children-ii].html
Normal file
30
leetcode-cn/problem (Chinese)/给小朋友们分糖果 II [distribute-candies-among-children-ii].html
Normal file
@ -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>
|
42
leetcode-cn/problem (Chinese)/重新排列后包含指定子字符串的字符串数目 [number-of-strings-which-can-be-rearranged-to-contain-substring].html
Normal file
42
leetcode-cn/problem (Chinese)/重新排列后包含指定子字符串的字符串数目 [number-of-strings-which-can-be-rearranged-to-contain-substring].html
Normal file
@ -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>
|
52
leetcode-cn/problem (English)/在树上执行操作以后得到的最大分数(English) [maximum-score-after-applying-operations-on-a-tree].html
Normal file
52
leetcode-cn/problem (English)/在树上执行操作以后得到的最大分数(English) [maximum-score-after-applying-operations-on-a-tree].html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> integer array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> associated with the <code>i<sup>th</sup></code> node.</p>
|
||||
|
||||
<p>You start with a score of <code>0</code>. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any node <code>i</code>.</li>
|
||||
<li>Add <code>values[i]</code> to your score.</li>
|
||||
<li>Set <code>values[i]</code> to <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A tree is <strong>healthy</strong> if the sum of values on the path from the root to any leaf node is different than zero.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum score</strong> you can obtain after performing these operations on the tree any number of times so that it remains <strong>healthy</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png" style="width: 515px; height: 443px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11.
|
||||
It can be shown that 11 is the maximum score obtainable after any number of operations on the tree.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png" style="width: 522px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
|
||||
<strong>Output:</strong> 40
|
||||
<strong>Explanation:</strong> We can choose nodes 0, 2, 3, and 4.
|
||||
- The sum of values on the path from 0 to 4 is equal to 10.
|
||||
- The sum of values on the path from 0 to 3 is equal to 10.
|
||||
- The sum of values on the path from 0 to 5 is equal to 3.
|
||||
- The sum of values on the path from 0 to 6 is equal to 5.
|
||||
Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40.
|
||||
It can be shown that 40 is the maximum score obtainable after any number of operations on the tree.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
54
leetcode-cn/problem (English)/平衡子序列的最大和(English) [maximum-balanced-subsequence-sum].html
Normal file
54
leetcode-cn/problem (English)/平衡子序列的最大和(English) [maximum-balanced-subsequence-sum].html
Normal file
@ -0,0 +1,54 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of <code>nums</code> having length <code>k</code> and consisting of <strong>indices</strong> <code>i<sub>0</sub> < i<sub>1</sub> < ... < i<sub>k-1</sub></code> is <strong>balanced</strong> if the following holds:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i<sub>j</sub>] - nums[i<sub>j-1</sub>] >= i<sub>j</sub> - i<sub>j-1</sub></code>, for every <code>j</code> in the range <code>[1, k - 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>subsequence</strong> of <code>nums</code> having length <code>1</code> is considered balanced.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> possible <strong>sum of elements</strong> in a <strong>balanced</strong> subsequence of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new <strong>non-empty</strong> array that is formed from the original array by deleting some (<strong>possibly none</strong>) of the elements without disturbing the relative positions of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,5,6]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> In this example, the subsequence [3,5,6] consisting of indices 0, 2, and 3 can be selected.
|
||||
nums[2] - nums[0] >= 2 - 0.
|
||||
nums[3] - nums[2] >= 3 - 2.
|
||||
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
The subsequence consisting of indices 1, 2, and 3 is also valid.
|
||||
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 14.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,-1,-3,8]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> In this example, the subsequence [5,8] consisting of indices 0 and 3 can be selected.
|
||||
nums[3] - nums[0] >= 3 - 0.
|
||||
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,-1]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> In this example, the subsequence [-1] can be selected.
|
||||
It is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
47
leetcode-cn/problem (English)/找出强数对的最大异或值 I(English) [maximum-strong-pair-xor-i].html
Normal file
47
leetcode-cn/problem (English)/找出强数对的最大异或值 I(English) [maximum-strong-pair-xor-i].html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
|
||||
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,100]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 2 strong pairs in the array <code>nums</code>: (10, 10) and (100, 100).
|
||||
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,6,25,30]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 6 strong pairs in the array <code>nums</code>: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
|
||||
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
47
leetcode-cn/problem (English)/找出强数对的最大异或值 II(English) [maximum-strong-pair-xor-ii].html
Normal file
47
leetcode-cn/problem (English)/找出强数对的最大异或值 II(English) [maximum-strong-pair-xor-ii].html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
|
||||
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,100]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
|
||||
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [500,520,2500,3000]
|
||||
<strong>Output:</strong> 1020
|
||||
<strong>Explanation:</strong> There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
|
||||
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
@ -0,0 +1,41 @@
|
||||
<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> 2D boolean matrix <code>grid</code> of size <code>n * n</code>. For all <code>i, j</code> that <code>0 <= i, j <= n - 1</code> and <code>i != j</code> team <code>i</code> is <strong>stronger</strong> than team <code>j</code> if <code>grid[i][j] == 1</code>, otherwise, team <code>j</code> is <strong>stronger</strong> than team <code>i</code>.</p>
|
||||
|
||||
<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is stronger than team <code>a</code>.</p>
|
||||
|
||||
<p>Return <em>the team that will be the champion of the tournament.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1],[0,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are two teams in this tournament.
|
||||
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,1],[1,0,1],[0,0,0]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There are three teams in this tournament.
|
||||
grid[1][0] == 1 means that team 1 is stronger than team 0.
|
||||
grid[1][2] == 1 means that team 1 is stronger than team 2.
|
||||
So team 1 will be the champion.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li>For all <code>i grid[i][i]</code> is <code>0.</code></li>
|
||||
<li>For all <code>i, j</code> that <code>i != j</code>, <code>grid[i][j] != grid[j][i]</code>.</li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament; each team is also a node in a <strong>DAG</strong>.</p>
|
||||
|
||||
<p>You are given the integer <code>n</code> and a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code><font face="monospace">m</font></code> representing the <strong>DAG</strong>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a directed edge from team <code>u<sub>i</sub></code> to team <code>v<sub>i</sub></code> in the graph.</p>
|
||||
|
||||
<p>A directed edge from <code>a</code> to <code>b</code> in the graph means that team <code>a</code> is <strong>stronger</strong> than team <code>b</code> and team <code>b</code> is <strong>weaker</strong> than team <code>a</code>.</p>
|
||||
|
||||
<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is <strong>stronger</strong> than team <code>a</code>.</p>
|
||||
|
||||
<p>Return <em>the team that will be the <strong>champion</strong> of the tournament if there is a <strong>unique</strong> champion, otherwise, return </em><code>-1</code><em>.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>cycle</strong> is a series of nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, a<sub>n+1</sub></code> such that node <code>a<sub>1</sub></code> is the same node as node <code>a<sub>n+1</sub></code>, the nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> are distinct, and there is a directed edge from the node <code>a<sub>i</sub></code> to node <code>a<sub>i+1</sub></code> for every <code>i</code> in the range <code>[1, n]</code>.</li>
|
||||
<li>A <strong>DAG</strong> is a directed graph that does not have any <strong>cycle</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">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>Input:</strong> n = 3, edges = [[0,1],[1,2]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">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>Input:</strong> n = 4, edges = [[0,2],[1,3],[1,2]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>The input is generated such that if team <code>a</code> is stronger than team <code>b</code>, team <code>b</code> is not stronger than team <code>a</code>.</li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li>
|
||||
</ul>
|
60
leetcode-cn/problem (English)/最大化数组末位元素的最少操作次数(English) [minimum-operations-to-maximize-last-elements-in-arrays].html
Normal file
60
leetcode-cn/problem (English)/最大化数组末位元素的最少操作次数(English) [minimum-operations-to-maximize-last-elements-in-arrays].html
Normal file
@ -0,0 +1,60 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays, <code>nums1</code> and <code>nums2</code>, both having length <code>n</code>.</p>
|
||||
|
||||
<p>You are allowed to perform a series of <strong>operations</strong> (<strong>possibly none</strong>).</p>
|
||||
|
||||
<p>In an operation, you select an index <code>i</code> in the range <code>[0, n - 1]</code> and <strong>swap</strong> the values of <code>nums1[i]</code> and <code>nums2[i]</code>.</p>
|
||||
|
||||
<p>Your task is to find the <strong>minimum</strong> number of operations required to satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1[n - 1]</code> is equal to the <strong>maximum value</strong> among all elements of <code>nums1</code>, i.e., <code>nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])</code>.</li>
|
||||
<li><code>nums2[n - 1]</code> is equal to the <strong>maximum</strong> <strong>value</strong> among all elements of <code>nums2</code>, i.e., <code>nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of operations needed to meet <strong>both</strong> conditions</em>, <em>or </em><code>-1</code><em> if it is <strong>impossible</strong> to satisfy both conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,7], nums2 = [4,5,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, an operation can be performed using index i = 2.
|
||||
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
|
||||
Both conditions are now satisfied.
|
||||
It can be shown that the minimum number of operations needed to be performed is 1.
|
||||
So, the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the following operations can be performed:
|
||||
First operation using index i = 4.
|
||||
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
|
||||
Another operation using index i = 3.
|
||||
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
|
||||
Both conditions are now satisfied.
|
||||
It can be shown that the minimum number of operations needed to be performed is 2.
|
||||
So, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,5,4], nums2 = [2,5,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> In this example, it is not possible to satisfy both conditions.
|
||||
So, the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
28
leetcode-cn/problem (English)/给小朋友们分糖果 I(English) [distribute-candies-among-children-i].html
Normal file
28
leetcode-cn/problem (English)/给小朋友们分糖果 I(English) [distribute-candies-among-children-i].html
Normal file
@ -0,0 +1,28 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, limit = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, limit = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (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) and (3, 0, 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>1 <= limit <= 50</code></li>
|
||||
</ul>
|
28
leetcode-cn/problem (English)/给小朋友们分糖果 II(English) [distribute-candies-among-children-ii].html
Normal file
28
leetcode-cn/problem (English)/给小朋友们分糖果 II(English) [distribute-candies-among-children-ii].html
Normal file
@ -0,0 +1,28 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, limit = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, limit = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (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) and (3, 0, 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= limit <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
60
leetcode-cn/problem (English)/购买物品的最大开销(English) [maximum-spending-after-buying-items].html
Normal file
60
leetcode-cn/problem (English)/购买物品的最大开销(English) [maximum-spending-after-buying-items].html
Normal file
@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m * n</code> integer matrix <code>values</code>, representing the values of <code>m * n</code> different items in <code>m</code> different shops. Each shop has <code>n</code> items where the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> shop has a value of <code>values[i][j]</code>. Additionally, the items in the <code>i<sup>th</sup></code> shop are sorted in non-increasing order of value. That is, <code>values[i][j] >= values[i][j + 1]</code> for all <code>0 <= j < n - 1</code>.</p>
|
||||
|
||||
<p>On each day, you would like to buy a single item from one of the shops. Specifically, On the <code>d<sup>th</sup></code> day you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any shop <code>i</code>.</li>
|
||||
<li>Buy the rightmost available item <code>j</code> for the price of <code>values[i][j] * d</code>. That is, find the greatest index <code>j</code> such that item <code>j</code> was never bought before, and buy it for the price of <code>values[i][j] * d</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that all items are pairwise different. For example, if you have bought item <code>0</code> from shop <code>1</code>, you can still buy item <code>0</code> from any other shop.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum amount of money that can be spent</strong> on buying all </em> <code>m * n</code> <em>products</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> values = [[8,5,2],[6,4,1],[9,7,3]]
|
||||
<strong>Output:</strong> 285
|
||||
<strong>Explanation:</strong> On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1.
|
||||
On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4.
|
||||
On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9.
|
||||
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16.
|
||||
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25.
|
||||
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36.
|
||||
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49.
|
||||
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64.
|
||||
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81.
|
||||
Hence, our total spending is equal to 285.
|
||||
It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> values = [[10,8,6,4,2],[9,7,5,3,2]]
|
||||
<strong>Output:</strong> 386
|
||||
<strong>Explanation:</strong> On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2.
|
||||
On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4.
|
||||
On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9.
|
||||
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16.
|
||||
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25.
|
||||
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36.
|
||||
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49.
|
||||
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64
|
||||
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81.
|
||||
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100.
|
||||
Hence, our total spending is equal to 386.
|
||||
It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> are sorted in non-increasing order.</li>
|
||||
</ul>
|
42
leetcode-cn/problem (English)/重新排列后包含指定子字符串的字符串数目(English) [number-of-strings-which-can-be-rearranged-to-contain-substring].html
Normal file
42
leetcode-cn/problem (English)/重新排列后包含指定子字符串的字符串数目(English) [number-of-strings-which-can-be-rearranged-to-contain-substring].html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>You are given an integer <code>n</code>.</p>
|
||||
|
||||
<p>A string <code>s</code> is called <strong>good </strong>if it contains only lowercase English characters <strong>and</strong> it is possible to rearrange the characters of <code>s</code> such that the new string contains <code>"leet"</code> as a <strong>substring</strong>.</p>
|
||||
|
||||
<p>For example:</p>
|
||||
|
||||
<ul>
|
||||
<li>The string <code>"lteer"</code> is good because we can rearrange it to form <code>"leetr"</code> .</li>
|
||||
<li><code>"letl"</code> is not good because we cannot rearrange it to contain <code>"leet"</code> as a substring.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>total</strong> number of good strings of length </em><code>n</code>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it <strong>modulo </strong><code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 83943898
|
||||
<strong>Explanation:</strong> The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (10<sup>9</sup> + 7) = 83943898.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given a 2D <strong>0-indexed</strong> array of strings, <code>access_times</code>, with size <code>n</code>. For each <code>i</code> where <code>0 <= i <= n - 1</code>, <code>access_times[i][0]</code> represents the name of an employee, and <code>access_times[i][1]</code> represents the access time of that employee. All entries in <code>access_times</code> are within the same day.</p>
|
||||
|
||||
<p>The access time is represented as <strong>four digits</strong> using a <strong>24-hour</strong> time format, for example, <code>"0800"</code> or <code>"2250"</code>.</p>
|
||||
|
||||
<p>An employee is said to be <strong>high-access</strong> if he has accessed the system <strong>three or more</strong> times within a <strong>one-hour period</strong>.</p>
|
||||
|
||||
<p>Times with exactly one hour of difference are <strong>not</strong> considered part of the same one-hour period. For example, <code>"0815"</code> and <code>"0915"</code> are not part of the same one-hour period.</p>
|
||||
|
||||
<p>Access times at the start and end of the day are <strong>not</strong> counted within the same one-hour period. For example, <code>"0005"</code> and <code>"2350"</code> are not part of the same one-hour period.</p>
|
||||
|
||||
<p>Return <em>a list that contains the names of <strong>high-access</strong> employees with any order you want.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
|
||||
<strong>Output:</strong> ["a"]
|
||||
<strong>Explanation:</strong> "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
|
||||
But "b" does not have more than two access times at all.
|
||||
So the answer is ["a"].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
|
||||
<strong>Output:</strong> ["c","d"]
|
||||
<strong>Explanation:</strong> "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
|
||||
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
|
||||
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
|
||||
<strong>Output:</strong> ["ab","cd"]
|
||||
<strong>Explanation:</strong> "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
|
||||
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
|
||||
So the answer is ["ab","cd"].</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> consists only of English small letters.</li>
|
||||
<li><code>access_times[i][1].length == 4</code></li>
|
||||
<li><code>access_times[i][1]</code> is in 24-hour time format.</li>
|
||||
<li><code>access_times[i][1]</code> consists only of <code>'0'</code> to <code>'9'</code>.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
160
leetcode/originData/distribute-candies-among-children-i.json
Normal file
160
leetcode/originData/distribute-candies-among-children-i.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/distribute-candies-among-children-ii.json
Normal file
163
leetcode/originData/distribute-candies-among-children-ii.json
Normal file
File diff suppressed because one or more lines are too long
160
leetcode/originData/find-champion-i.json
Normal file
160
leetcode/originData/find-champion-i.json
Normal file
File diff suppressed because one or more lines are too long
160
leetcode/originData/find-champion-ii.json
Normal file
160
leetcode/originData/find-champion-ii.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/high-access-employees.json
Normal file
161
leetcode/originData/high-access-employees.json
Normal file
File diff suppressed because one or more lines are too long
167
leetcode/originData/maximum-balanced-subsequence-sum.json
Normal file
167
leetcode/originData/maximum-balanced-subsequence-sum.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
161
leetcode/originData/maximum-spending-after-buying-items.json
Normal file
161
leetcode/originData/maximum-spending-after-buying-items.json
Normal file
File diff suppressed because one or more lines are too long
160
leetcode/originData/maximum-strong-pair-xor-i.json
Normal file
160
leetcode/originData/maximum-strong-pair-xor-i.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/maximum-strong-pair-xor-ii.json
Normal file
163
leetcode/originData/maximum-strong-pair-xor-ii.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
161
leetcode/originData/number-of-strings-which-can-be-rearranged-to-contain-substring.json
Normal file
161
leetcode/originData/number-of-strings-which-can-be-rearranged-to-contain-substring.json
Normal file
File diff suppressed because one or more lines are too long
28
leetcode/problem/distribute-candies-among-children-i.html
Normal file
28
leetcode/problem/distribute-candies-among-children-i.html
Normal file
@ -0,0 +1,28 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, limit = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, limit = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (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) and (3, 0, 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>1 <= limit <= 50</code></li>
|
||||
</ul>
|
28
leetcode/problem/distribute-candies-among-children-ii.html
Normal file
28
leetcode/problem/distribute-candies-among-children-ii.html
Normal file
@ -0,0 +1,28 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, limit = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, limit = 3
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (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) and (3, 0, 0).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= limit <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
41
leetcode/problem/find-champion-i.html
Normal file
41
leetcode/problem/find-champion-i.html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> 2D boolean matrix <code>grid</code> of size <code>n * n</code>. For all <code>i, j</code> that <code>0 <= i, j <= n - 1</code> and <code>i != j</code> team <code>i</code> is <strong>stronger</strong> than team <code>j</code> if <code>grid[i][j] == 1</code>, otherwise, team <code>j</code> is <strong>stronger</strong> than team <code>i</code>.</p>
|
||||
|
||||
<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is stronger than team <code>a</code>.</p>
|
||||
|
||||
<p>Return <em>the team that will be the champion of the tournament.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1],[0,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are two teams in this tournament.
|
||||
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,0,1],[1,0,1],[0,0,0]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There are three teams in this tournament.
|
||||
grid[1][0] == 1 means that team 1 is stronger than team 0.
|
||||
grid[1][2] == 1 means that team 1 is stronger than team 2.
|
||||
So team 1 will be the champion.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li>For all <code>i grid[i][i]</code> is <code>0.</code></li>
|
||||
<li>For all <code>i, j</code> that <code>i != j</code>, <code>grid[i][j] != grid[j][i]</code>.</li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li>
|
||||
</ul>
|
51
leetcode/problem/find-champion-ii.html
Normal file
51
leetcode/problem/find-champion-ii.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament; each team is also a node in a <strong>DAG</strong>.</p>
|
||||
|
||||
<p>You are given the integer <code>n</code> and a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code><font face="monospace">m</font></code> representing the <strong>DAG</strong>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a directed edge from team <code>u<sub>i</sub></code> to team <code>v<sub>i</sub></code> in the graph.</p>
|
||||
|
||||
<p>A directed edge from <code>a</code> to <code>b</code> in the graph means that team <code>a</code> is <strong>stronger</strong> than team <code>b</code> and team <code>b</code> is <strong>weaker</strong> than team <code>a</code>.</p>
|
||||
|
||||
<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is <strong>stronger</strong> than team <code>a</code>.</p>
|
||||
|
||||
<p>Return <em>the team that will be the <strong>champion</strong> of the tournament if there is a <strong>unique</strong> champion, otherwise, return </em><code>-1</code><em>.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>cycle</strong> is a series of nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, a<sub>n+1</sub></code> such that node <code>a<sub>1</sub></code> is the same node as node <code>a<sub>n+1</sub></code>, the nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> are distinct, and there is a directed edge from the node <code>a<sub>i</sub></code> to node <code>a<sub>i+1</sub></code> for every <code>i</code> in the range <code>[1, n]</code>.</li>
|
||||
<li>A <strong>DAG</strong> is a directed graph that does not have any <strong>cycle</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">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>Input:</strong> n = 3, edges = [[0,1],[1,2]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">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>Input:</strong> n = 4, edges = [[0,2],[1,3],[1,2]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>The input is generated such that if team <code>a</code> is stronger than team <code>b</code>, team <code>b</code> is not stronger than team <code>a</code>.</li>
|
||||
<li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li>
|
||||
</ul>
|
52
leetcode/problem/high-access-employees.html
Normal file
52
leetcode/problem/high-access-employees.html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>You are given a 2D <strong>0-indexed</strong> array of strings, <code>access_times</code>, with size <code>n</code>. For each <code>i</code> where <code>0 <= i <= n - 1</code>, <code>access_times[i][0]</code> represents the name of an employee, and <code>access_times[i][1]</code> represents the access time of that employee. All entries in <code>access_times</code> are within the same day.</p>
|
||||
|
||||
<p>The access time is represented as <strong>four digits</strong> using a <strong>24-hour</strong> time format, for example, <code>"0800"</code> or <code>"2250"</code>.</p>
|
||||
|
||||
<p>An employee is said to be <strong>high-access</strong> if he has accessed the system <strong>three or more</strong> times within a <strong>one-hour period</strong>.</p>
|
||||
|
||||
<p>Times with exactly one hour of difference are <strong>not</strong> considered part of the same one-hour period. For example, <code>"0815"</code> and <code>"0915"</code> are not part of the same one-hour period.</p>
|
||||
|
||||
<p>Access times at the start and end of the day are <strong>not</strong> counted within the same one-hour period. For example, <code>"0005"</code> and <code>"2350"</code> are not part of the same one-hour period.</p>
|
||||
|
||||
<p>Return <em>a list that contains the names of <strong>high-access</strong> employees with any order you want.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
|
||||
<strong>Output:</strong> ["a"]
|
||||
<strong>Explanation:</strong> "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
|
||||
But "b" does not have more than two access times at all.
|
||||
So the answer is ["a"].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
|
||||
<strong>Output:</strong> ["c","d"]
|
||||
<strong>Explanation:</strong> "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
|
||||
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
|
||||
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
|
||||
<strong>Output:</strong> ["ab","cd"]
|
||||
<strong>Explanation:</strong> "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
|
||||
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
|
||||
So the answer is ["ab","cd"].</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> consists only of English small letters.</li>
|
||||
<li><code>access_times[i][1].length == 4</code></li>
|
||||
<li><code>access_times[i][1]</code> is in 24-hour time format.</li>
|
||||
<li><code>access_times[i][1]</code> consists only of <code>'0'</code> to <code>'9'</code>.</li>
|
||||
</ul>
|
54
leetcode/problem/maximum-balanced-subsequence-sum.html
Normal file
54
leetcode/problem/maximum-balanced-subsequence-sum.html
Normal file
@ -0,0 +1,54 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of <code>nums</code> having length <code>k</code> and consisting of <strong>indices</strong> <code>i<sub>0</sub> < i<sub>1</sub> < ... < i<sub>k-1</sub></code> is <strong>balanced</strong> if the following holds:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i<sub>j</sub>] - nums[i<sub>j-1</sub>] >= i<sub>j</sub> - i<sub>j-1</sub></code>, for every <code>j</code> in the range <code>[1, k - 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>subsequence</strong> of <code>nums</code> having length <code>1</code> is considered balanced.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> possible <strong>sum of elements</strong> in a <strong>balanced</strong> subsequence of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new <strong>non-empty</strong> array that is formed from the original array by deleting some (<strong>possibly none</strong>) of the elements without disturbing the relative positions of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,5,6]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> In this example, the subsequence [3,5,6] consisting of indices 0, 2, and 3 can be selected.
|
||||
nums[2] - nums[0] >= 2 - 0.
|
||||
nums[3] - nums[2] >= 3 - 2.
|
||||
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
The subsequence consisting of indices 1, 2, and 3 is also valid.
|
||||
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 14.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,-1,-3,8]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> In this example, the subsequence [5,8] consisting of indices 0 and 3 can be selected.
|
||||
nums[3] - nums[0] >= 3 - 0.
|
||||
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,-1]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> In this example, the subsequence [-1] can be selected.
|
||||
It is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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,52 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> integer array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> associated with the <code>i<sup>th</sup></code> node.</p>
|
||||
|
||||
<p>You start with a score of <code>0</code>. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any node <code>i</code>.</li>
|
||||
<li>Add <code>values[i]</code> to your score.</li>
|
||||
<li>Set <code>values[i]</code> to <code>0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A tree is <strong>healthy</strong> if the sum of values on the path from the root to any leaf node is different than zero.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum score</strong> you can obtain after performing these operations on the tree any number of times so that it remains <strong>healthy</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png" style="width: 515px; height: 443px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11.
|
||||
It can be shown that 11 is the maximum score obtainable after any number of operations on the tree.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png" style="width: 522px; height: 245px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
|
||||
<strong>Output:</strong> 40
|
||||
<strong>Explanation:</strong> We can choose nodes 0, 2, 3, and 4.
|
||||
- The sum of values on the path from 0 to 4 is equal to 10.
|
||||
- The sum of values on the path from 0 to 3 is equal to 10.
|
||||
- The sum of values on the path from 0 to 5 is equal to 3.
|
||||
- The sum of values on the path from 0 to 6 is equal to 5.
|
||||
Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40.
|
||||
It can be shown that 40 is the maximum score obtainable after any number of operations on the tree.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
60
leetcode/problem/maximum-spending-after-buying-items.html
Normal file
60
leetcode/problem/maximum-spending-after-buying-items.html
Normal file
@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m * n</code> integer matrix <code>values</code>, representing the values of <code>m * n</code> different items in <code>m</code> different shops. Each shop has <code>n</code> items where the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> shop has a value of <code>values[i][j]</code>. Additionally, the items in the <code>i<sup>th</sup></code> shop are sorted in non-increasing order of value. That is, <code>values[i][j] >= values[i][j + 1]</code> for all <code>0 <= j < n - 1</code>.</p>
|
||||
|
||||
<p>On each day, you would like to buy a single item from one of the shops. Specifically, On the <code>d<sup>th</sup></code> day you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any shop <code>i</code>.</li>
|
||||
<li>Buy the rightmost available item <code>j</code> for the price of <code>values[i][j] * d</code>. That is, find the greatest index <code>j</code> such that item <code>j</code> was never bought before, and buy it for the price of <code>values[i][j] * d</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that all items are pairwise different. For example, if you have bought item <code>0</code> from shop <code>1</code>, you can still buy item <code>0</code> from any other shop.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum amount of money that can be spent</strong> on buying all </em> <code>m * n</code> <em>products</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> values = [[8,5,2],[6,4,1],[9,7,3]]
|
||||
<strong>Output:</strong> 285
|
||||
<strong>Explanation:</strong> On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1.
|
||||
On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4.
|
||||
On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9.
|
||||
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16.
|
||||
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25.
|
||||
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36.
|
||||
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49.
|
||||
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64.
|
||||
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81.
|
||||
Hence, our total spending is equal to 285.
|
||||
It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> values = [[10,8,6,4,2],[9,7,5,3,2]]
|
||||
<strong>Output:</strong> 386
|
||||
<strong>Explanation:</strong> On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2.
|
||||
On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4.
|
||||
On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9.
|
||||
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16.
|
||||
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25.
|
||||
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36.
|
||||
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49.
|
||||
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64
|
||||
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81.
|
||||
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100.
|
||||
Hence, our total spending is equal to 386.
|
||||
It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> are sorted in non-increasing order.</li>
|
||||
</ul>
|
47
leetcode/problem/maximum-strong-pair-xor-i.html
Normal file
47
leetcode/problem/maximum-strong-pair-xor-i.html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
|
||||
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,100]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 2 strong pairs in the array <code>nums</code>: (10, 10) and (100, 100).
|
||||
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,6,25,30]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 6 strong pairs in the array <code>nums</code>: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
|
||||
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
47
leetcode/problem/maximum-strong-pair-xor-ii.html
Normal file
47
leetcode/problem/maximum-strong-pair-xor-ii.html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>|x - y| <= min(x, y)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
|
||||
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,100]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
|
||||
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [500,520,2500,3000]
|
||||
<strong>Output:</strong> 1020
|
||||
<strong>Explanation:</strong> There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
|
||||
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
@ -0,0 +1,60 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays, <code>nums1</code> and <code>nums2</code>, both having length <code>n</code>.</p>
|
||||
|
||||
<p>You are allowed to perform a series of <strong>operations</strong> (<strong>possibly none</strong>).</p>
|
||||
|
||||
<p>In an operation, you select an index <code>i</code> in the range <code>[0, n - 1]</code> and <strong>swap</strong> the values of <code>nums1[i]</code> and <code>nums2[i]</code>.</p>
|
||||
|
||||
<p>Your task is to find the <strong>minimum</strong> number of operations required to satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1[n - 1]</code> is equal to the <strong>maximum value</strong> among all elements of <code>nums1</code>, i.e., <code>nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])</code>.</li>
|
||||
<li><code>nums2[n - 1]</code> is equal to the <strong>maximum</strong> <strong>value</strong> among all elements of <code>nums2</code>, i.e., <code>nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of operations needed to meet <strong>both</strong> conditions</em>, <em>or </em><code>-1</code><em> if it is <strong>impossible</strong> to satisfy both conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,7], nums2 = [4,5,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, an operation can be performed using index i = 2.
|
||||
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
|
||||
Both conditions are now satisfied.
|
||||
It can be shown that the minimum number of operations needed to be performed is 1.
|
||||
So, the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the following operations can be performed:
|
||||
First operation using index i = 4.
|
||||
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
|
||||
Another operation using index i = 3.
|
||||
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
|
||||
Both conditions are now satisfied.
|
||||
It can be shown that the minimum number of operations needed to be performed is 2.
|
||||
So, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,5,4], nums2 = [2,5,3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> In this example, it is not possible to satisfy both conditions.
|
||||
So, the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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,42 @@
|
||||
<p>You are given an integer <code>n</code>.</p>
|
||||
|
||||
<p>A string <code>s</code> is called <strong>good </strong>if it contains only lowercase English characters <strong>and</strong> it is possible to rearrange the characters of <code>s</code> such that the new string contains <code>"leet"</code> as a <strong>substring</strong>.</p>
|
||||
|
||||
<p>For example:</p>
|
||||
|
||||
<ul>
|
||||
<li>The string <code>"lteer"</code> is good because we can rearrange it to form <code>"leetr"</code> .</li>
|
||||
<li><code>"letl"</code> is not good because we cannot rearrange it to contain <code>"leet"</code> as a substring.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>total</strong> number of good strings of length </em><code>n</code>.</p>
|
||||
|
||||
<p>Since the answer may be large, return it <strong>modulo </strong><code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 83943898
|
||||
<strong>Explanation:</strong> The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (10<sup>9</sup> + 7) = 83943898.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user