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
25625a52f2
commit
4cea624fbf
README.md
leetcode-cn
origin-data.json
originData
[no content]market-analysis-iii.json[no content]maximum-profitable-triplets-with-increasing-prices-ii.json[no content]number-of-ways-to-reach-destination-in-the-grid.jsonfind-the-k-or-of-an-array.jsonlength-of-the-longest-subsequence-that-sums-to-target.jsonmaximum-points-after-collecting-coins-from-all-nodes.jsonminimum-equal-sum-of-two-arrays-after-replacing-zeros.jsonminimum-increment-operations-to-make-array-beautiful.jsonminimum-number-of-changes-to-make-binary-string-beautiful.jsonsubarrays-distinct-element-sum-of-squares-i.jsonsubarrays-distinct-element-sum-of-squares-ii.json
problem (Chinese)
使二进制字符串变美丽的最少修改次数 [minimum-number-of-changes-to-make-binary-string-beautiful].html使数组变美的最小增量运算数 [minimum-increment-operations-to-make-array-beautiful].html和为目标值的最长子序列的长度 [length-of-the-longest-subsequence-that-sums-to-target].html子数组不同元素数目的平方和 I [subarrays-distinct-element-sum-of-squares-i].html子数组不同元素数目的平方和 II [subarrays-distinct-element-sum-of-squares-ii].html找出数组中的 K-or 值 [find-the-k-or-of-an-array].html收集所有金币可获得的最大积分 [maximum-points-after-collecting-coins-from-all-nodes].html数组的最小相等和 [minimum-equal-sum-of-two-arrays-after-replacing-zeros].html
problem (English)
使二进制字符串变美丽的最少修改次数(English) [minimum-number-of-changes-to-make-binary-string-beautiful].html使数组变美的最小增量运算数(English) [minimum-increment-operations-to-make-array-beautiful].html和为目标值的最长子序列的长度(English) [length-of-the-longest-subsequence-that-sums-to-target].html子数组不同元素数目的平方和 I(English) [subarrays-distinct-element-sum-of-squares-i].html子数组不同元素数目的平方和 II(English) [subarrays-distinct-element-sum-of-squares-ii].html找出数组中的 K-or 值(English) [find-the-k-or-of-an-array].html收集所有金币可获得的最大积分(English) [maximum-points-after-collecting-coins-from-all-nodes].html数组的最小相等和(English) [minimum-equal-sum-of-two-arrays-after-replacing-zeros].html
leetcode
origin-data.json
originData
[no content]market-analysis-iii.json[no content]maximum-profitable-triplets-with-increasing-prices-ii.json[no content]number-of-ways-to-reach-destination-in-the-grid.jsonfind-the-k-or-of-an-array.jsonlength-of-the-longest-subsequence-that-sums-to-target.jsonmaximum-points-after-collecting-coins-from-all-nodes.jsonminimum-equal-sum-of-two-arrays-after-replacing-zeros.jsonminimum-increment-operations-to-make-array-beautiful.jsonminimum-number-of-changes-to-make-binary-string-beautiful.jsonsubarrays-distinct-element-sum-of-squares-i.jsonsubarrays-distinct-element-sum-of-squares-ii.json
problem
find-the-k-or-of-an-array.htmllength-of-the-longest-subsequence-that-sums-to-target.htmlmaximum-points-after-collecting-coins-from-all-nodes.htmlminimum-equal-sum-of-two-arrays-after-replacing-zeros.htmlminimum-increment-operations-to-make-array-beautiful.htmlminimum-number-of-changes-to-make-binary-string-beautiful.htmlsubarrays-distinct-element-sum-of-squares-i.htmlsubarrays-distinct-element-sum-of-squares-ii.html
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.10.25**
|
||||
> 最后更新日期: **2023.11.03**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
65
leetcode-cn/originData/[no content]market-analysis-iii.json
Normal file
65
leetcode-cn/originData/[no content]market-analysis-iii.json
Normal file
@ -0,0 +1,65 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3205",
|
||||
"questionFrontendId": "2922",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 2509032,
|
||||
"title": "Market Analysis III",
|
||||
"titleSlug": "market-analysis-iii",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"14\", \"totalSubmission\": \"23\", \"totalAcceptedRaw\": 14, \"totalSubmissionRaw\": 23, \"acRate\": \"60.9%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Users\": [\"seller_id\", \"join_date\", \"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2019-01-01\",\"Lenovo\"],[2,\"2019-02-09\",\"Samsung\"],[3,\"2019-01-19\",\"LG\"]],\"Orders\":[[1,\"2019-08-01\",4,2],[2,\"2019-08-02\",2,3],[3,\"2019-08-03\",3,3],[4,\"2019-08-04\",1,2],[5,\"2019-08-04\",4,2]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}",
|
||||
"metaData": "{\"mysql\":[\"Create table If Not Exists Users (seller_id int, join_date date, favorite_brand varchar(10))\",\"Create table If Not Exists Orders (order_id int, order_date date, item_id int, seller_id int)\",\"Create table If Not Exists Items (item_id int, item_brand varchar(10))\"],\"mssql\":[\"Create table Users (seller_id int, join_date date, favorite_brand varchar(10))\",\"Create table Orders (order_id int, order_date date, item_id int, seller_id int)\",\"Create table Items (item_id int, item_brand varchar(10))\"],\"oraclesql\":[\"Create table Users (seller_id int, join_date date, favorite_brand varchar(10))\",\"Create table Orders (order_id int, order_date date, item_id int, seller_id int)\",\"Create table Items (item_id int, item_brand varchar(10))\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"languages\":[\"mysql\",\"mssql\",\"oraclesql\"],\"database_schema\":{\"Users\":{\"seller_id\":\"INT\",\"join_date\":\"DATE\",\"favorite_brand\":\"VARCHAR(10)\"},\"Orders\":{\"order_id\":\"INT\",\"order_date\":\"DATE\",\"item_id\":\"INT\",\"seller_id\":\"INT\"},\"Items\":{\"item_id\":\"INT\",\"item_brand\":\"VARCHAR(10)\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Users (seller_id int, join_date date, favorite_brand varchar(10))",
|
||||
"Create table If Not Exists Orders (order_id int, order_date date, item_id int, seller_id int)",
|
||||
"Create table If Not Exists Items (item_id int, item_brand varchar(10))",
|
||||
"Truncate table Users",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('1', '2019-01-01', 'Lenovo')",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('2', '2019-02-09', 'Samsung')",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('3', '2019-01-19', 'LG')",
|
||||
"Truncate table Orders",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('1', '2019-08-01', '4', '2')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('2', '2019-08-02', '2', '3')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('3', '2019-08-03', '3', '3')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('4', '2019-08-04', '1', '2')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('5', '2019-08-04', '4', '2')",
|
||||
"Truncate table Items",
|
||||
"insert into Items (item_id, item_brand) values ('1', 'Samsung')",
|
||||
"insert into Items (item_id, item_brand) values ('2', 'Lenovo')",
|
||||
"insert into Items (item_id, item_brand) values ('3', 'LG')",
|
||||
"insert into Items (item_id, item_brand) values ('4', 'HP')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{\"headers\": {\"Users\": [\"seller_id\", \"join_date\", \"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2019-01-01\",\"Lenovo\"],[2,\"2019-02-09\",\"Samsung\"],[3,\"2019-01-19\",\"LG\"]],\"Orders\":[[1,\"2019-08-01\",4,2],[2,\"2019-08-02\",2,3],[3,\"2019-08-03\",3,3],[4,\"2019-08-04\",1,2],[5,\"2019-08-04\",4,2]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
53
leetcode-cn/originData/[no content]maximum-profitable-triplets-with-increasing-prices-ii.json
Normal file
53
leetcode-cn/originData/[no content]maximum-profitable-triplets-with-increasing-prices-ii.json
Normal file
File diff suppressed because one or more lines are too long
72
leetcode-cn/originData/[no content]number-of-ways-to-reach-destination-in-the-grid.json
Normal file
72
leetcode-cn/originData/[no content]number-of-ways-to-reach-destination-in-the-grid.json
Normal file
File diff suppressed because one or more lines are too long
179
leetcode-cn/originData/find-the-k-or-of-an-array.json
Normal file
179
leetcode-cn/originData/find-the-k-or-of-an-array.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
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/minimum-number-of-changes-to-make-binary-string-beautiful.json
Normal file
177
leetcode-cn/originData/minimum-number-of-changes-to-make-binary-string-beautiful.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
File diff suppressed because one or more lines are too long
52
leetcode-cn/problem (Chinese)/使二进制字符串变美丽的最少修改次数 [minimum-number-of-changes-to-make-binary-string-beautiful].html
Normal file
52
leetcode-cn/problem (Chinese)/使二进制字符串变美丽的最少修改次数 [minimum-number-of-changes-to-make-binary-string-beautiful].html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>给你一个长度为偶数下标从 <strong>0</strong> 开始的二进制字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 <strong>美丽的</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>每个子字符串的长度都是 <strong>偶数</strong> 。</li>
|
||||
<li>每个子字符串都 <strong>只</strong> 包含 <code>1</code> 或 <strong>只</strong> 包含 <code>0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>你可以将 <code>s</code> 中任一字符改成 <code>0</code> 或者 <code>1</code> 。</p>
|
||||
|
||||
<p>请你返回让字符串 <code>s</code> 美丽的<strong> 最少</strong> 字符修改次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "1001"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。
|
||||
字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。
|
||||
将字符串变美丽最少需要 2 次修改。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "10"
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们将 s[1] 改为 1 ,得到字符串 "11" 。
|
||||
字符串 "11" 是美丽的,因为它已经是美丽的。
|
||||
将字符串变美丽最少需要 1 次修改。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "0000"
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>不需要进行任何修改,字符串 "0000" 已经是美丽字符串。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 的长度为偶数。</li>
|
||||
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li>
|
||||
</ul>
|
64
leetcode-cn/problem (Chinese)/使数组变美的最小增量运算数 [minimum-increment-operations-to-make-array-beautiful].html
Normal file
64
leetcode-cn/problem (Chinese)/使数组变美的最小增量运算数 [minimum-increment-operations-to-make-array-beautiful].html
Normal file
@ -0,0 +1,64 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的整数数组 <code>nums</code> ,和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>你可以执行下述 <strong>递增</strong> 运算 <strong>任意</strong> 次(可以是 <strong>0</strong> 次):</p>
|
||||
|
||||
<ul>
|
||||
<li>从范围 <code>[0, n - 1]</code> 中选择一个下标 <code>i</code> ,并将 <code>nums[i]</code> 的值加 <code>1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果数组中任何长度 <strong>大于或等于 3</strong> 的子数组,其 <strong>最大</strong> 元素都大于或等于 <code>k</code> ,则认为数组是一个 <strong>美丽数组</strong> 。</p>
|
||||
|
||||
<p>以整数形式返回使数组变为 <strong>美丽数组</strong> 需要执行的 <strong>最小</strong> 递增运算数。</p>
|
||||
|
||||
<p>子数组是数组中的一个连续 <strong>非空</strong> 元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,3,0,0,2], k = 4
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>可以执行下述递增运算,使 nums 变为美丽数组:
|
||||
选择下标 i = 1 ,并且将 nums[1] 的值加 1 -> [2,4,0,0,2] 。
|
||||
选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,3] 。
|
||||
选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,4] 。
|
||||
长度大于或等于 3 的子数组为 [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4] 。
|
||||
在所有子数组中,最大元素都等于 k = 4 ,所以 nums 现在是美丽数组。
|
||||
可以证明无法用少于 3 次递增运算使 nums 变为美丽数组。
|
||||
因此,答案为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1,3,3], k = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以执行下述递增运算,使 nums 变为美丽数组:
|
||||
选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,4,3] 。
|
||||
选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,5,3] 。
|
||||
长度大于或等于 3 的子数组为 [0,1,5]、[1,5,3]、[0,1,5,3] 。
|
||||
在所有子数组中,最大元素都等于 k = 5 ,所以 nums 现在是美丽数组。
|
||||
可以证明无法用少于 2 次递增运算使 nums 变为美丽数组。
|
||||
因此,答案为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,2], k = 1
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>在这个示例中,只有一个长度大于或等于 3 的子数组 [1,1,2] 。
|
||||
其最大元素 2 已经大于 k = 1 ,所以无需执行任何增量运算。
|
||||
因此,答案为 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
41
leetcode-cn/problem (Chinese)/和为目标值的最长子序列的长度 [length-of-the-longest-subsequence-that-sums-to-target].html
Normal file
41
leetcode-cn/problem (Chinese)/和为目标值的最长子序列的长度 [length-of-the-longest-subsequence-that-sums-to-target].html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>target</code> 。</p>
|
||||
|
||||
<p>返回和为 <code>target</code> 的 <code>nums</code> 子序列中,子序列 <strong>长度的最大值 </strong>。如果不存在和为 <code>target</code> 的子序列,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>子序列</strong> 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5], target = 9
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>总共有 3 个子序列的和为 9 :[4,5] ,[1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,1,3,2,1,5], target = 7
|
||||
<b>输出:</b>4
|
||||
<strong>解释:</strong>总共有 5 个子序列的和为 7 :[4,3] ,[4,1,2] ,[4,2,1] ,[1,1,5] 和 [1,3,2,1] 。最长子序列为 [1,3,2,1] 。所以答案为 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,1,5,4,5], target = 3
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>无法得到和为 3 的子序列。
|
||||
</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>1 <= target <= 1000</code></li>
|
||||
</ul>
|
51
leetcode-cn/problem (Chinese)/子数组不同元素数目的平方和 I [subarrays-distinct-element-sum-of-squares-i].html
Normal file
51
leetcode-cn/problem (Chinese)/子数组不同元素数目的平方和 I [subarrays-distinct-element-sum-of-squares-i].html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>定义 <code>nums</code> 一个子数组的 <strong>不同计数</strong> 值如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>令 <code>nums[i..j]</code> 表示 <code>nums</code> 中所有下标在 <code>i</code> 到 <code>j</code> 范围内的元素构成的子数组(满足 <code>0 <= i <= j < nums.length</code> ),那么我们称子数组 <code>nums[i..j]</code> 中不同值的数目为 <code>nums[i..j]</code> 的不同计数。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中所有子数组的 <strong>不同计数</strong> 的 <strong>平方</strong> 和。</p>
|
||||
|
||||
<p>由于答案可能会很大,请你将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p>子数组指的是一个数组里面一段连续 <strong>非空</strong> 的元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,1]
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>六个子数组分别为:
|
||||
[1]: 1 个互不相同的元素。
|
||||
[2]: 1 个互不相同的元素。
|
||||
[1]: 1 个互不相同的元素。
|
||||
[1,2]: 2 个互不相同的元素。
|
||||
[2,1]: 2 个互不相同的元素。
|
||||
[1,2,1]: 2 个互不相同的元素。
|
||||
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,2]
|
||||
<b>输出:3</b>
|
||||
<strong>解释:</strong>三个子数组分别为:
|
||||
[2]: 1 个互不相同的元素。
|
||||
[2]: 1 个互不相同的元素。
|
||||
[2,2]: 1 个互不相同的元素。
|
||||
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
51
leetcode-cn/problem (Chinese)/子数组不同元素数目的平方和 II [subarrays-distinct-element-sum-of-squares-ii].html
Normal file
51
leetcode-cn/problem (Chinese)/子数组不同元素数目的平方和 II [subarrays-distinct-element-sum-of-squares-ii].html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>定义 <code>nums</code> 一个子数组的 <strong>不同计数</strong> 值如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>令 <code>nums[i..j]</code> 表示 <code>nums</code> 中所有下标在 <code>i</code> 到 <code>j</code> 范围内的元素构成的子数组(满足 <code>0 <= i <= j < nums.length</code> ),那么我们称子数组 <code>nums[i..j]</code> 中不同值的数目为 <code>nums[i..j]</code> 的不同计数。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中所有子数组的 <strong>不同计数</strong> 的 <strong>平方</strong> 和。</p>
|
||||
|
||||
<p>由于答案可能会很大,请你将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p>子数组指的是一个数组里面一段连续 <strong>非空</strong> 的元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,1]
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>六个子数组分别为:
|
||||
[1]: 1 个互不相同的元素。
|
||||
[2]: 1 个互不相同的元素。
|
||||
[1]: 1 个互不相同的元素。
|
||||
[1,2]: 2 个互不相同的元素。
|
||||
[2,1]: 2 个互不相同的元素。
|
||||
[1,2,1]: 2 个互不相同的元素。
|
||||
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,2]
|
||||
<b>输出:3</b>
|
||||
<strong>解释:</strong>三个子数组分别为:
|
||||
[2]: 1 个互不相同的元素。
|
||||
[2]: 1 个互不相同的元素。
|
||||
[2,2]: 1 个互不相同的元素。
|
||||
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p><code>nums</code> 中的 <strong>K-or</strong> 是一个满足以下条件的非负整数:</p>
|
||||
|
||||
<ul>
|
||||
<li>只有在 <code>nums</code> 中,至少存在 <code>k</code> 个元素的第 <code>i</code> 位值为 1 ,那么 K-or 中的第 <code>i</code> 位的值才是 1 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <code>nums</code> 的 <strong>K-or</strong> 值。</p>
|
||||
|
||||
<p><strong>注意</strong> :对于整数 <code>x</code> ,如果 <code>(2<sup>i</sup> AND x) == 2<sup>i</sup></code> ,则 <code>x</code> 中的第 <code>i</code> 位值为 1 ,其中 <code>AND</code> 为按位与运算符。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [7,12,9,8,9,15], k = 4
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>nums[0]、nums[2]、nums[4] 和 nums[5] 的第 0 位的值为 1 。
|
||||
nums[0] 和 nums[5] 的第 1 位的值为 1 。
|
||||
nums[0]、nums[1] 和 nums[5] 的第 2 位的值为 1 。
|
||||
nums[1]、nums[2]、nums[3]、nums[4] 和 nums[5] 的第 3 位的值为 1 。
|
||||
只有第 0 位和第 3 位满足数组中至少存在 k 个元素在对应位上的值为 1 。因此,答案为 2^0 + 2^3 = 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,12,1,11,4,5], k = 6
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>因为 k == 6 == nums.length ,所以数组的 6-or 等于其中所有元素按位与运算的结果。因此,答案为 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [10,8,5,9,11,6,8], k = 1
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>因为 k == 1 ,数组的 1-or 等于其中所有元素按位或运算的结果。因此,答案为 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>31</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
50
leetcode-cn/problem (Chinese)/收集所有金币可获得的最大积分 [maximum-points-after-collecting-coins-from-all-nodes].html
Normal file
50
leetcode-cn/problem (Chinese)/收集所有金币可获得的最大积分 [maximum-points-after-collecting-coins-from-all-nodes].html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>有一棵由 <code>n</code> 个节点组成的无向树,以 <code>0</code> 为根节点,节点编号从 <code>0</code> 到 <code>n - 1</code> 。给你一个长度为 <code>n - 1</code> 的二维 <strong>整数</strong> 数组 <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> 之间存在一条边。另给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的数组 <code>coins</code> 和一个整数 <code>k</code> ,其中 <code>coins[i]</code> 表示节点 <code>i</code> 处的金币数量。</p>
|
||||
|
||||
<p>从根节点开始,你必须收集所有金币。要想收集节点上的金币,必须先收集该节点的祖先节点上的金币。</p>
|
||||
|
||||
<p>节点 <code>i</code> 上的金币可以用下述方法之一进行收集:</p>
|
||||
|
||||
<ul>
|
||||
<li>收集所有金币,得到共计 <code>coins[i] - k</code> 点积分。如果 <code>coins[i] - k</code> 是负数,你将会失去 <code>abs(coins[i] - k)</code> 点积分。</li>
|
||||
<li>收集所有金币,得到共计 <code>floor(coins[i] / 2)</code> 点积分。如果采用这种方法,节点 <code>i</code> 子树中所有节点 <code>j</code> 的金币数 <code>coins[j]</code> 将会减少至 <code>floor(coins[j] / 2)</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回收集 <strong>所有</strong> 树节点的金币之后可以获得的最大积分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png" style="width: 60px; height: 316px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
|
||||
<strong>输出:</strong>11
|
||||
<strong>解释:</strong>
|
||||
使用第一种方法收集节点 0 上的所有金币。总积分 = 10 - 5 = 5 。
|
||||
使用第一种方法收集节点 1 上的所有金币。总积分 = 5 + (10 - 5) = 10 。
|
||||
使用第二种方法收集节点 2 上的所有金币。所以节点 3 上的金币将会变为 floor(3 / 2) = 1 ,总积分 = 10 + floor(3 / 2) = 11 。
|
||||
使用第二种方法收集节点 3 上的所有金币。总积分 = 11 + floor(1 / 2) = 11.
|
||||
可以证明收集所有节点上的金币能获得的最大积分是 11 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<strong class="example"> <img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex2.png" style="width: 140px; height: 147px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>
|
||||
使用第一种方法收集所有节点上的金币,因此,总积分 = (8 - 0) + (4 - 0) + (4 - 0) = 16 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= coins[i] <= 10<sup>4</sup></font></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code><font face="monospace">0 <= edges[i][0], edges[i][1] < n</font></code></li>
|
||||
<li><code><font face="monospace">0 <= k <= 10<sup>4</sup></font></code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/数组的最小相等和 [minimum-equal-sum-of-two-arrays-after-replacing-zeros].html
Normal file
35
leetcode-cn/problem (Chinese)/数组的最小相等和 [minimum-equal-sum-of-two-arrays-after-replacing-zeros].html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>给你两个由正整数和 <code>0</code> 组成的数组 <code>nums1</code> 和 <code>nums2</code> 。</p>
|
||||
|
||||
<p>你必须将两个数组中的<strong> 所有</strong> <code>0</code> 替换为 <strong>严格</strong> 正整数,并且满足两个数组中所有元素的和 <strong>相等</strong> 。</p>
|
||||
|
||||
<p>返回 <strong>最小</strong> 相等和 ,如果无法使两数组相等,则返回 <code>-1</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [3,2,0,1,0], nums2 = [6,5,0]
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>可以按下述方式替换数组中的 0 :
|
||||
- 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。
|
||||
- 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。
|
||||
两个数组的元素和相等,都等于 12 。可以证明这是可以获得的最小相等和。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [2,0,2,0], nums2 = [1,4]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无法使两个数组的和相等。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
50
leetcode-cn/problem (English)/使二进制字符串变美丽的最少修改次数(English) [minimum-number-of-changes-to-make-binary-string-beautiful].html
Normal file
50
leetcode-cn/problem (English)/使二进制字符串变美丽的最少修改次数(English) [minimum-number-of-changes-to-make-binary-string-beautiful].html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p>
|
||||
|
||||
<p>A string is <strong>beautiful</strong> if it's possible to partition it into one or more substrings such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each substring has an <strong>even length</strong>.</li>
|
||||
<li>Each substring contains <strong>only</strong> <code>1</code>'s or <strong>only</strong> <code>0</code>'s.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string "1100".
|
||||
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
|
||||
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "10"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We change s[1] to 1 to get string "11".
|
||||
It can be seen that the string "11" is beautiful because we can partition it into "11".
|
||||
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0000"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We don't need to make any changes as the string "0000" is beautiful already.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> has an even length.</li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
62
leetcode-cn/problem (English)/使数组变美的最小增量运算数(English) [minimum-increment-operations-to-make-array-beautiful].html
Normal file
62
leetcode-cn/problem (English)/使数组变美的最小增量运算数(English) [minimum-increment-operations-to-make-array-beautiful].html
Normal file
@ -0,0 +1,62 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform the following <strong>increment</strong> operation <strong>any</strong> number of times (<strong>including zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the range <code>[0, n - 1]</code>, and increase <code>nums[i]</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>An array is considered <strong>beautiful</strong> if, for any <strong>subarray</strong> with a size of <code>3</code> or <strong>more</strong>, its <strong>maximum</strong> element is <strong>greater than or equal</strong> to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of increment operations needed to make </em><code>nums</code><em> <strong>beautiful</strong>.</em></p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,0,0,2], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can perform the following increment operations to make nums beautiful:
|
||||
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
|
||||
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
|
||||
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
|
||||
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
|
||||
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
|
||||
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
|
||||
Hence, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,3,3], k = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can perform the following increment operations to make nums beautiful:
|
||||
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
|
||||
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
|
||||
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
|
||||
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
|
||||
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
|
||||
Hence, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2], k = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The only subarray with a size of 3 or more in this example is [1,1,2].
|
||||
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
|
||||
Hence, the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
39
leetcode-cn/problem (English)/和为目标值的最长子序列的长度(English) [length-of-the-longest-subsequence-that-sums-to-target].html
Normal file
39
leetcode-cn/problem (English)/和为目标值的最长子序列的长度(English) [length-of-the-longest-subsequence-that-sums-to-target].html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>length of the longest subsequence</strong> of</em> <code>nums</code> <em>that sums up to</em> <code>target</code>. <em>If no such subsequence exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5], target = 9
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,3,2,1,5], target = 7
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,5,4,5], target = 3
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that nums has no subsequence that sums up to 3.
|
||||
</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>1 <= target <= 1000</code></li>
|
||||
</ul>
|
46
leetcode-cn/problem (English)/子数组不同元素数目的平方和 I(English) [subarrays-distinct-element-sum-of-squares-i].html
Normal file
46
leetcode-cn/problem (English)/子数组不同元素数目的平方和 I(English) [subarrays-distinct-element-sum-of-squares-i].html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Six possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,2]: 2 distinct values
|
||||
[2,1]: 2 distinct values
|
||||
[1,2,1]: 2 distinct values
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Three possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,1]: 1 distinct value
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
48
leetcode-cn/problem (English)/子数组不同元素数目的平方和 II(English) [subarrays-distinct-element-sum-of-squares-ii].html
Normal file
48
leetcode-cn/problem (English)/子数组不同元素数目的平方和 II(English) [subarrays-distinct-element-sum-of-squares-ii].html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Six possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,2]: 2 distinct values
|
||||
[2,1]: 2 distinct values
|
||||
[1,2,1]: 2 distinct values
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Three possible subarrays are:
|
||||
[2]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[2,2]: 1 distinct value
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
49
leetcode-cn/problem (English)/找出数组中的 K-or 值(English) [find-the-k-or-of-an-array].html
Normal file
49
leetcode-cn/problem (English)/找出数组中的 K-or 值(English) [find-the-k-or-of-an-array].html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>K-or</strong> of <code>nums</code> is a non-negative integer that satisfies the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <code>i<sup>th</sup></code> bit is set in the K-or <strong>if and only if</strong> there are at least <code>k</code> elements of nums in which bit <code>i</code> is set.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong> K-or</strong> of</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that a bit <code>i</code> is set in <code>x</code> if <code>(2<sup>i</sup> AND x) == 2<sup>i</sup></code>, where <code>AND</code> is the bitwise <code>AND</code> operator.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,12,9,8,9,15], k = 4
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
|
||||
Bit 1 is set at nums[0], and nums[5].
|
||||
Bit 2 is set at nums[0], nums[1], and nums[5].
|
||||
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
|
||||
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,12,1,11,4,5], k = 6
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,8,5,9,11,6,8], k = 1
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>31</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
48
leetcode-cn/problem (English)/收集所有金币可获得的最大积分(English) [maximum-points-after-collecting-coins-from-all-nodes].html
Normal file
48
leetcode-cn/problem (English)/收集所有金币可获得的最大积分(English) [maximum-points-after-collecting-coins-from-all-nodes].html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>There exists an undirected tree rooted at node <code>0</code> with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given a 2D <strong>integer</strong> 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. You are also given a <strong>0-indexed</strong> array <code>coins</code> of size <code>n</code> where <code>coins[i]</code> indicates the number of coins in the vertex <code>i</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.</p>
|
||||
|
||||
<p>Coins at <code>node<sub>i</sub></code> can be collected in one of the following ways:</p>
|
||||
|
||||
<ul>
|
||||
<li>Collect all the coins, but you will get <code>coins[i] - k</code> points. If <code>coins[i] - k</code> is negative then you will lose <code>abs(coins[i] - k)</code> points.</li>
|
||||
<li>Collect all the coins, but you will get <code>floor(coins[i] / 2)</code> points. If this way is used, then for all the <code>node<sub>j</sub></code> present in the subtree of <code>node<sub>i</sub></code>, <code>coins[j]</code> will get reduced to <code>floor(coins[j] / 2)</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum points</strong> you can get after collecting the coins from <strong>all</strong> the tree nodes.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png" style="width: 60px; height: 316px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong>
|
||||
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
|
||||
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
|
||||
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
|
||||
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
|
||||
It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<strong class="example"> <img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex2.png" style="width: 140px; height: 147px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= coins[i] <= 10<sup>4</sup></font></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code><font face="monospace">0 <= edges[i][0], edges[i][1] < n</font></code></li>
|
||||
<li><code><font face="monospace">0 <= k <= 10<sup>4</sup></font></code></li>
|
||||
</ul>
|
33
leetcode-cn/problem (English)/数组的最小相等和(English) [minimum-equal-sum-of-two-arrays-after-replacing-zeros].html
Normal file
33
leetcode-cn/problem (English)/数组的最小相等和(English) [minimum-equal-sum-of-two-arrays-after-replacing-zeros].html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>You are given two arrays <code>nums1</code> and <code>nums2</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You have to replace <strong>all</strong> the <code>0</code>'s in both arrays with <strong>strictly</strong> positive integers such that the sum of elements of both arrays becomes <strong>equal</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> equal sum you can obtain, or </em><code>-1</code><em> if it is impossible</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,0,1,0], nums2 = [6,5,0]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> We can replace 0's in the following way:
|
||||
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
|
||||
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
|
||||
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,0,2,0], nums2 = [1,4]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is impossible to make the sum of both arrays equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
62
leetcode/originData/[no content]market-analysis-iii.json
Normal file
62
leetcode/originData/[no content]market-analysis-iii.json
Normal file
@ -0,0 +1,62 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3205",
|
||||
"questionFrontendId": "2922",
|
||||
"boundTopicId": null,
|
||||
"title": "Market Analysis III",
|
||||
"titleSlug": "market-analysis-iii",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 5,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\": {\"Users\": [\"seller_id\", \"join_date\", \"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2019-01-01\",\"Lenovo\"],[2,\"2019-02-09\",\"Samsung\"],[3,\"2019-01-19\",\"LG\"]],\"Orders\":[[1,\"2019-08-01\",4,2],[2,\"2019-08-02\",2,3],[3,\"2019-08-03\",3,3],[4,\"2019-08-04\",1,2],[5,\"2019-08-04\",4,2]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"62\", \"totalSubmission\": \"119\", \"totalAcceptedRaw\": 62, \"totalSubmissionRaw\": 119, \"acRate\": \"52.1%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Users\": [\"seller_id\", \"join_date\", \"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2019-01-01\",\"Lenovo\"],[2,\"2019-02-09\",\"Samsung\"],[3,\"2019-01-19\",\"LG\"]],\"Orders\":[[1,\"2019-08-01\",4,2],[2,\"2019-08-02\",2,3],[3,\"2019-08-03\",3,3],[4,\"2019-08-04\",1,2],[5,\"2019-08-04\",4,2]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}",
|
||||
"metaData": "{\"mysql\": [\"Create table If Not Exists Users (seller_id int, join_date date, favorite_brand varchar(10))\", \"Create table If Not Exists Orders (order_id int, order_date date, item_id int, seller_id int)\", \"Create table If Not Exists Items (item_id int, item_brand varchar(10))\"], \"mssql\": [\"Create table Users (seller_id int, join_date date, favorite_brand varchar(10))\", \"Create table Orders (order_id int, order_date date, item_id int, seller_id int)\", \"Create table Items (item_id int, item_brand varchar(10))\"], \"oraclesql\": [\"Create table Users (seller_id int, join_date date, favorite_brand varchar(10))\", \"Create table Orders (order_id int, order_date date, item_id int, seller_id int)\", \"Create table Items (item_id int, item_brand varchar(10))\", \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"], \"database\": true, \"languages\": [\"mysql\", \"mssql\", \"oraclesql\"], \"database_schema\": {\"Users\": {\"seller_id\": \"INT\", \"join_date\": \"DATE\", \"favorite_brand\": \"VARCHAR(10)\"}, \"Orders\": {\"order_id\": \"INT\", \"order_date\": \"DATE\", \"item_id\": \"INT\", \"seller_id\": \"INT\"}, \"Items\": {\"item_id\": \"INT\", \"item_brand\": \"VARCHAR(10)\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Users (seller_id int, join_date date, favorite_brand varchar(10))",
|
||||
"Create table If Not Exists Orders (order_id int, order_date date, item_id int, seller_id int)",
|
||||
"Create table If Not Exists Items (item_id int, item_brand varchar(10))",
|
||||
"Truncate table Users",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('1', '2019-01-01', 'Lenovo')",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('2', '2019-02-09', 'Samsung')",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('3', '2019-01-19', 'LG')",
|
||||
"Truncate table Orders",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('1', '2019-08-01', '4', '2')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('2', '2019-08-02', '2', '3')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('3', '2019-08-03', '3', '3')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('4', '2019-08-04', '1', '2')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('5', '2019-08-04', '4', '2')",
|
||||
"Truncate table Items",
|
||||
"insert into Items (item_id, item_brand) values ('1', 'Samsung')",
|
||||
"insert into Items (item_id, item_brand) values ('2', 'Lenovo')",
|
||||
"insert into Items (item_id, item_brand) values ('3', 'LG')",
|
||||
"insert into Items (item_id, item_brand) values ('4', 'HP')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
50
leetcode/originData/[no content]maximum-profitable-triplets-with-increasing-prices-ii.json
Normal file
50
leetcode/originData/[no content]maximum-profitable-triplets-with-increasing-prices-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
176
leetcode/originData/find-the-k-or-of-an-array.json
Normal file
176
leetcode/originData/find-the-k-or-of-an-array.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
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
49
leetcode/problem/find-the-k-or-of-an-array.html
Normal file
49
leetcode/problem/find-the-k-or-of-an-array.html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>K-or</strong> of <code>nums</code> is a non-negative integer that satisfies the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <code>i<sup>th</sup></code> bit is set in the K-or <strong>if and only if</strong> there are at least <code>k</code> elements of nums in which bit <code>i</code> is set.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong> K-or</strong> of</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that a bit <code>i</code> is set in <code>x</code> if <code>(2<sup>i</sup> AND x) == 2<sup>i</sup></code>, where <code>AND</code> is the bitwise <code>AND</code> operator.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,12,9,8,9,15], k = 4
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
|
||||
Bit 1 is set at nums[0], and nums[5].
|
||||
Bit 2 is set at nums[0], nums[1], and nums[5].
|
||||
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
|
||||
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,12,1,11,4,5], k = 6
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,8,5,9,11,6,8], k = 1
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>31</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>length of the longest subsequence</strong> of</em> <code>nums</code> <em>that sums up to</em> <code>target</code>. <em>If no such subsequence exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5], target = 9
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,3,2,1,5], target = 7
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,5,4,5], target = 3
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that nums has no subsequence that sums up to 3.
|
||||
</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>1 <= target <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>There exists an undirected tree rooted at node <code>0</code> with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given a 2D <strong>integer</strong> 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. You are also given a <strong>0-indexed</strong> array <code>coins</code> of size <code>n</code> where <code>coins[i]</code> indicates the number of coins in the vertex <code>i</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.</p>
|
||||
|
||||
<p>Coins at <code>node<sub>i</sub></code> can be collected in one of the following ways:</p>
|
||||
|
||||
<ul>
|
||||
<li>Collect all the coins, but you will get <code>coins[i] - k</code> points. If <code>coins[i] - k</code> is negative then you will lose <code>abs(coins[i] - k)</code> points.</li>
|
||||
<li>Collect all the coins, but you will get <code>floor(coins[i] / 2)</code> points. If this way is used, then for all the <code>node<sub>j</sub></code> present in the subtree of <code>node<sub>i</sub></code>, <code>coins[j]</code> will get reduced to <code>floor(coins[j] / 2)</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum points</strong> you can get after collecting the coins from <strong>all</strong> the tree nodes.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png" style="width: 60px; height: 316px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong>
|
||||
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
|
||||
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
|
||||
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
|
||||
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
|
||||
It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<strong class="example"> <img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex2.png" style="width: 140px; height: 147px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= coins[i] <= 10<sup>4</sup></font></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code><font face="monospace">0 <= edges[i][0], edges[i][1] < n</font></code></li>
|
||||
<li><code><font face="monospace">0 <= k <= 10<sup>4</sup></font></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>You are given two arrays <code>nums1</code> and <code>nums2</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You have to replace <strong>all</strong> the <code>0</code>'s in both arrays with <strong>strictly</strong> positive integers such that the sum of elements of both arrays becomes <strong>equal</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> equal sum you can obtain, or </em><code>-1</code><em> if it is impossible</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,0,1,0], nums2 = [6,5,0]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> We can replace 0's in the following way:
|
||||
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
|
||||
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
|
||||
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,0,2,0], nums2 = [1,4]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is impossible to make the sum of both arrays equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,62 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform the following <strong>increment</strong> operation <strong>any</strong> number of times (<strong>including zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the range <code>[0, n - 1]</code>, and increase <code>nums[i]</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>An array is considered <strong>beautiful</strong> if, for any <strong>subarray</strong> with a size of <code>3</code> or <strong>more</strong>, its <strong>maximum</strong> element is <strong>greater than or equal</strong> to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of increment operations needed to make </em><code>nums</code><em> <strong>beautiful</strong>.</em></p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,0,0,2], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can perform the following increment operations to make nums beautiful:
|
||||
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
|
||||
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
|
||||
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
|
||||
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
|
||||
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
|
||||
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
|
||||
Hence, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,3,3], k = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can perform the following increment operations to make nums beautiful:
|
||||
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
|
||||
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
|
||||
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
|
||||
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
|
||||
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
|
||||
Hence, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2], k = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The only subarray with a size of 3 or more in this example is [1,1,2].
|
||||
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
|
||||
Hence, the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p>
|
||||
|
||||
<p>A string is <strong>beautiful</strong> if it's possible to partition it into one or more substrings such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each substring has an <strong>even length</strong>.</li>
|
||||
<li>Each substring contains <strong>only</strong> <code>1</code>'s or <strong>only</strong> <code>0</code>'s.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string "1100".
|
||||
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
|
||||
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "10"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We change s[1] to 1 to get string "11".
|
||||
It can be seen that the string "11" is beautiful because we can partition it into "11".
|
||||
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0000"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We don't need to make any changes as the string "0000" is beautiful already.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> has an even length.</li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Six possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,2]: 2 distinct values
|
||||
[2,1]: 2 distinct values
|
||||
[1,2,1]: 2 distinct values
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Three possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,1]: 1 distinct value
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Six possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,2]: 2 distinct values
|
||||
[2,1]: 2 distinct values
|
||||
[1,2,1]: 2 distinct values
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Three possible subarrays are:
|
||||
[2]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[2,2]: 1 distinct value
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user