mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
560c99b559
commit
6e78d50055
File diff suppressed because it is too large
Load Diff
165
leetcode-cn/originData/collect-coins-in-a-tree.json
Normal file
165
leetcode-cn/originData/collect-coins-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
165
leetcode-cn/originData/k-items-with-the-maximum-sum.json
Normal file
165
leetcode-cn/originData/k-items-with-the-maximum-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/prime-subtraction-operation.json
Normal file
164
leetcode-cn/originData/prime-subtraction-operation.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,39 @@
|
||||
<p>袋子中装有一些物品,每个物品上都标记着数字 <code>1</code> 、<code>0</code> 或 <code>-1</code> 。</p>
|
||||
|
||||
<p>给你四个非负整数 <code>numOnes</code> 、<code>numZeros</code> 、<code>numNegOnes</code> 和 <code>k</code> 。</p>
|
||||
|
||||
<p>袋子最初包含:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numOnes</code> 件标记为 <code>1</code> 的物品。</li>
|
||||
<li><code>numZeroes</code> 件标记为 <code>0</code> 的物品。</li>
|
||||
<li><code>numNegOnes</code> 件标记为 <code>-1</code> 的物品。</li>
|
||||
</ul>
|
||||
|
||||
<p>现计划从这些物品中恰好选出 <code>k</code> 件物品。返回所有可行方案中,物品上所标记数字之和的最大值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>袋子中的物品分别标记为 {1, 1, 1, 0, 0} 。取 2 件标记为 1 的物品,得到的数字之和为 2 。
|
||||
可以证明 2 是所有可行方案中的最大值。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>袋子中的物品分别标记为 {1, 1, 1, 0, 0} 。取 3 件标记为 1 的物品,1 件标记为 0 的物品,得到的数字之和为 3 。
|
||||
可以证明 3 是所有可行方案中的最大值。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= numOnes, numZeros, numNegOnes <= 50</code></li>
|
||||
<li><code>0 <= k <= numOnes + numZeros + numNegOnes</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>同时给你一个长度为 <code>m</code> 的整数数组 <code>queries</code> 。第 <code>i</code> 个查询中,你需要将 <code>nums</code> 中所有元素变成 <code>queries[i]</code> 。你可以执行以下操作 <strong>任意</strong> 次:</p>
|
||||
|
||||
<ul>
|
||||
<li>将数组里一个元素 <strong>增大</strong> 或者 <strong>减小</strong> <code>1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个长度为 <code>m</code> 的数组<em> </em><code>answer</code> ,其中<em> </em><code>answer[i]</code>是将 <code>nums</code> 中所有元素变成 <code>queries[i]</code> 的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p><strong>注意</strong>,每次查询后,数组变回最开始的值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,1,6,8], queries = [1,5]
|
||||
<b>输出:</b>[14,10]
|
||||
<b>解释:</b>第一个查询,我们可以执行以下操作:
|
||||
- 将 nums[0] 减小 2 次,nums = [1,1,6,8] 。
|
||||
- 将 nums[2] 减小 5 次,nums = [1,1,1,8] 。
|
||||
- 将 nums[3] 减小 7 次,nums = [1,1,1,1] 。
|
||||
第一个查询的总操作次数为 2 + 5 + 7 = 14 。
|
||||
第二个查询,我们可以执行以下操作:
|
||||
- 将 nums[0] 增大 2 次,nums = [5,1,6,8] 。
|
||||
- 将 nums[1] 增大 4 次,nums = [5,5,6,8] 。
|
||||
- 将 nums[2] 减小 1 次,nums = [5,5,5,8] 。
|
||||
- 将 nums[3] 减小 3 次,nums = [5,5,5,5] 。
|
||||
第二个查询的总操作次数为 2 + 4 + 1 + 3 = 10 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [2,9,6,3], queries = [10]
|
||||
<b>输出:</b>[20]
|
||||
<b>解释:</b>我们可以将数组中所有元素都增大到 10 ,总操作次数为 8 + 1 + 4 + 7 = 20 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个 <code>n</code> 个节点的无向无根树,节点编号从 <code>0</code> 到 <code>n - 1</code> 。给你整数 <code>n</code> 和一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示树中节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条边。再给你一个长度为 <code>n</code> 的数组 <code>coins</code> ,其中 <code>coins[i]</code> 可能为 <code>0</code> 也可能为 <code>1</code> ,<code>1</code> 表示节点 <code>i</code> 处有一个金币。</p>
|
||||
|
||||
<p>一开始,你需要选择树中任意一个节点出发。你可以执行下述操作任意次:</p>
|
||||
|
||||
<ul>
|
||||
<li>收集距离当前节点距离为 <code>2</code> 以内的所有金币,或者</li>
|
||||
<li>移动到树中一个相邻节点。</li>
|
||||
</ul>
|
||||
|
||||
<p>你需要收集树中所有的金币,并且回到出发节点,请你返回最少经过的边数。</p>
|
||||
|
||||
<p>如果你多次经过一条边,每一次经过都会给答案加一。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/01/graph-2.png" style="width: 522px; height: 522px;"></p>
|
||||
|
||||
<pre><b>输入:</b>coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>从节点 2 出发,收集节点 0 处的金币,移动到节点 3 ,收集节点 5 处的金币,然后移动回节点 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/02/graph-4.png" style="width: 522px; height: 522px;"></p>
|
||||
|
||||
<pre><b>输入:</b>coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>从节点 0 出发,收集节点 4 和 3 处的金币,移动到节点 2 处,收集节点 7 处的金币,移动回节点 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>1 <= n <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= coins[i] <= 1</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> 表示一棵合法的树。</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,数组长度为 <code>n</code> 。</p>
|
||||
|
||||
<p>你可以执行无限次下述运算:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择一个之前未选过的下标 <code>i</code> ,并选择一个 <strong>严格小于</strong> <code>nums[i]</code> 的质数 <code>p</code> ,从 <code>nums[i]</code> 中减去 <code>p</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果你能通过上述运算使得 <code>nums</code> 成为严格递增数组,则返回 <code>true</code> ;否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p><strong>严格递增数组</strong> 中的每个元素都严格大于其前面的元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,9,6,10]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>
|
||||
在第一次运算中:选择 i = 0 和 p = 3 ,然后从 nums[0] 减去 3 ,nums 变为 [1,9,6,10] 。
|
||||
在第二次运算中:选择 i = 1 和 p = 7 ,然后从 nums[1] 减去 7 ,nums 变为 [1,2,6,10] 。
|
||||
第二次运算后,nums 按严格递增顺序排序,因此答案为 true 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [6,8,11,12]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>nums 从一开始就按严格递增顺序排序,因此不需要执行任何运算。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,8,3]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>可以证明,执行运算无法使 nums 按严格递增顺序排序,因此答案是 false 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums.length == n</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>There is a bag that consists of items, each item has a number <code>1</code>, <code>0</code>, or <code>-1</code> written on it.</p>
|
||||
|
||||
<p>You are given four <strong>non-negative </strong>integers <code>numOnes</code>, <code>numZeros</code>, <code>numNegOnes</code>, and <code>k</code>.</p>
|
||||
|
||||
<p>The bag initially contains:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numOnes</code> items with <code>1</code>s written on them.</li>
|
||||
<li><code>numZeroes</code> items with <code>0</code>s written on them.</li>
|
||||
<li><code>numNegOnes</code> items with <code>-1</code>s written on them.</li>
|
||||
</ul>
|
||||
|
||||
<p>We want to pick exactly <code>k</code> items among the available items. Return <em>the <strong>maximum</strong> possible sum of numbers written on the items</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
|
||||
It can be proven that 2 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
|
||||
It can be proven that 3 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= numOnes, numZeros, numNegOnes <= 50</code></li>
|
||||
<li><code>0 <= k <= numOnes + numZeros + numNegOnes</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You are also given an integer array <code>queries</code> of size <code>m</code>. For the <code>i<sup>th</sup></code> query, you want to make all of the elements of <code>nums</code> equal to<code> queries[i]</code>. You can perform the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Increase</strong> or <strong>decrease</strong> an element of the array by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>queries[i]</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that after each query the array is reset to its original state.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,6,8], queries = [1,5]
|
||||
<strong>Output:</strong> [14,10]
|
||||
<strong>Explanation:</strong> For the first query we can do the following operations:
|
||||
- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
|
||||
- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
|
||||
- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
|
||||
So the total number of operations for the first query is 2 + 5 + 7 = 14.
|
||||
For the second query we can do the following operations:
|
||||
- Increase nums[0] 2 times, so that nums = [5,1,6,8].
|
||||
- Increase nums[1] 4 times, so that nums = [5,5,6,8].
|
||||
- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
|
||||
- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
|
||||
So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,9,6,3], queries = [10]
|
||||
<strong>Output:</strong> [20]
|
||||
<strong>Explanation:</strong> We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>There exists an undirected and unrooted tree with <code>n</code> nodes indexed from <code>0</code> to <code>n - 1</code>. You are given an integer <code>n</code> and a 2D integer array edges 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. You are also given an array <code>coins</code> of size <code>n</code> where <code>coins[i]</code> can be either <code>0</code> or <code>1</code>, where <code>1</code> indicates the presence of a coin in the vertex <code>i</code>.</p>
|
||||
|
||||
<p>Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times: </p>
|
||||
|
||||
<ul>
|
||||
<li>Collect all the coins that are at a distance of at most <code>2</code> from the current vertex, or</li>
|
||||
<li>Move to any adjacent vertex in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p>Find <em>the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex</em>.</p>
|
||||
|
||||
<p>Note that if you pass an edge several times, you need to count it into the answer several times.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/01/graph-2.png" style="width: 522px; height: 522px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/02/graph-4.png" style="width: 522px; height: 522px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2, collect the coin at vertex 7, then move back to vertex 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>1 <= n <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= coins[i] <= 1</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>You can perform the following operation as many times as you want:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick an index <code>i</code> that you haven’t picked before, and pick a prime <code>p</code> <strong>strictly less than</strong> <code>nums[i]</code>, then subtract <code>p</code> from <code>nums[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>true if you can make <code>nums</code> a strictly increasing array using the above operation and false otherwise.</em></p>
|
||||
|
||||
<p>A <strong>strictly increasing array</strong> is an array whose each element is strictly greater than its preceding element.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,9,6,10]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
|
||||
In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
|
||||
After the second operation, nums is sorted in strictly increasing order, so the answer is true.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,8,11,12]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>Initially nums is sorted in strictly increasing order, so we don't need to make any operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,8,3]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code><font face="monospace">nums.length == n</font></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
162
leetcode/originData/collect-coins-in-a-tree.json
Normal file
162
leetcode/originData/collect-coins-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/k-items-with-the-maximum-sum.json
Normal file
162
leetcode/originData/k-items-with-the-maximum-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/prime-subtraction-operation.json
Normal file
161
leetcode/originData/prime-subtraction-operation.json
Normal file
File diff suppressed because one or more lines are too long
43
leetcode/problem/collect-coins-in-a-tree.html
Normal file
43
leetcode/problem/collect-coins-in-a-tree.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>There exists an undirected and unrooted tree with <code>n</code> nodes indexed from <code>0</code> to <code>n - 1</code>. You are given an integer <code>n</code> and a 2D integer array edges 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. You are also given an array <code>coins</code> of size <code>n</code> where <code>coins[i]</code> can be either <code>0</code> or <code>1</code>, where <code>1</code> indicates the presence of a coin in the vertex <code>i</code>.</p>
|
||||
|
||||
<p>Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times: </p>
|
||||
|
||||
<ul>
|
||||
<li>Collect all the coins that are at a distance of at most <code>2</code> from the current vertex, or</li>
|
||||
<li>Move to any adjacent vertex in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p>Find <em>the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex</em>.</p>
|
||||
|
||||
<p>Note that if you pass an edge several times, you need to count it into the answer several times.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/01/graph-2.png" style="width: 522px; height: 522px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/03/02/graph-4.png" style="width: 522px; height: 522px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2, collect the coin at vertex 7, then move back to vertex 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>1 <= n <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= coins[i] <= 1</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
40
leetcode/problem/k-items-with-the-maximum-sum.html
Normal file
40
leetcode/problem/k-items-with-the-maximum-sum.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>There is a bag that consists of items, each item has a number <code>1</code>, <code>0</code>, or <code>-1</code> written on it.</p>
|
||||
|
||||
<p>You are given four <strong>non-negative </strong>integers <code>numOnes</code>, <code>numZeros</code>, <code>numNegOnes</code>, and <code>k</code>.</p>
|
||||
|
||||
<p>The bag initially contains:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numOnes</code> items with <code>1</code>s written on them.</li>
|
||||
<li><code>numZeroes</code> items with <code>0</code>s written on them.</li>
|
||||
<li><code>numNegOnes</code> items with <code>-1</code>s written on them.</li>
|
||||
</ul>
|
||||
|
||||
<p>We want to pick exactly <code>k</code> items among the available items. Return <em>the <strong>maximum</strong> possible sum of numbers written on the items</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
|
||||
It can be proven that 2 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
|
||||
It can be proven that 3 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= numOnes, numZeros, numNegOnes <= 50</code></li>
|
||||
<li><code>0 <= k <= numOnes + numZeros + numNegOnes</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You are also given an integer array <code>queries</code> of size <code>m</code>. For the <code>i<sup>th</sup></code> query, you want to make all of the elements of <code>nums</code> equal to<code> queries[i]</code>. You can perform the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Increase</strong> or <strong>decrease</strong> an element of the array by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>queries[i]</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that after each query the array is reset to its original state.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,6,8], queries = [1,5]
|
||||
<strong>Output:</strong> [14,10]
|
||||
<strong>Explanation:</strong> For the first query we can do the following operations:
|
||||
- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
|
||||
- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
|
||||
- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
|
||||
So the total number of operations for the first query is 2 + 5 + 7 = 14.
|
||||
For the second query we can do the following operations:
|
||||
- Increase nums[0] 2 times, so that nums = [5,1,6,8].
|
||||
- Increase nums[1] 4 times, so that nums = [5,5,6,8].
|
||||
- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
|
||||
- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
|
||||
So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,9,6,3], queries = [10]
|
||||
<strong>Output:</strong> [20]
|
||||
<strong>Explanation:</strong> We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
44
leetcode/problem/prime-subtraction-operation.html
Normal file
44
leetcode/problem/prime-subtraction-operation.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>You can perform the following operation as many times as you want:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick an index <code>i</code> that you haven’t picked before, and pick a prime <code>p</code> <strong>strictly less than</strong> <code>nums[i]</code>, then subtract <code>p</code> from <code>nums[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>true if you can make <code>nums</code> a strictly increasing array using the above operation and false otherwise.</em></p>
|
||||
|
||||
<p>A <strong>strictly increasing array</strong> is an array whose each element is strictly greater than its preceding element.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,9,6,10]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
|
||||
In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
|
||||
After the second operation, nums is sorted in strictly increasing order, so the answer is true.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,8,11,12]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>Initially nums is sorted in strictly increasing order, so we don't need to make any operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,8,3]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code><font face="monospace">nums.length == n</font></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user