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
0bbe66ecc9
commit
26ccf4c890
leetcode-cn
origin-data.json
originData
[no content]find-bursty-behavior.json[no content]guess-the-number-using-bitwise-questions-ii.jsonapply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.jsonlongest-common-suffix-queries.jsonmaximum-length-substring-with-two-occurrences.jsonmost-frequent-ids.json
problem (Chinese)
执行操作使数据元素之和大于等于 K [apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k].html最长公共后缀查询 [longest-common-suffix-queries].html最高频率的 ID [most-frequent-ids].html每个字符最多出现两次的最长子字符串 [maximum-length-substring-with-two-occurrences].html
problem (English)
File diff suppressed because it is too large
Load Diff
62
leetcode-cn/originData/[no content]find-bursty-behavior.json
Normal file
62
leetcode-cn/originData/[no content]find-bursty-behavior.json
Normal file
@ -0,0 +1,62 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3400",
|
||||
"questionFrontendId": "3089",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 2704231,
|
||||
"title": "Find Bursty Behavior",
|
||||
"titleSlug": "find-bursty-behavior",
|
||||
"content": null,
|
||||
"translatedTitle": "查找突发行为",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": "数据库",
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"25\", \"totalSubmission\": \"62\", \"totalAcceptedRaw\": 25, \"totalSubmissionRaw\": 62, \"acRate\": \"40.3%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Posts\": [\"post_id\", \"user_id\", \"post_date\"]}, \"rows\": {\"Posts\": [[1, 1, \"2024-02-27\"], [2, 5, \"2024-02-06\"], [3, 3, \"2024-02-25\"], [4, 3, \"2024-02-14\"], [5, 3, \"2024-02-06\"], [6, 2, \"2024-02-25\"]]}}",
|
||||
"metaData": "{\"mysql\":[\"Create table If Not Exists Posts (post_id int, user_id int, post_date date)\"],\"mssql\":[\"Create table Posts (post_id int, user_id int, post_date date)\"],\"oraclesql\":[\"Create table Posts (post_id int, user_id int, post_date date)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_bursty_behavior\",\"pythondata\":[\"Posts = pd.DataFrame([], columns=['post_id', 'user_id', 'post_date']).astype({'post_id':'Int64', 'user_id':'Int64', 'post_date':'datetime64[ns]'})\\n\"],\"postgresql\":[\"CREATE TABLE Posts (\\n post_id int,\\n user_id int,\\n post_date date\\n);\\n\",\"SELECT TO_CHAR(post_date, 'YYYY-MM-DD') AS formatted_post_date FROM Posts;\\n\"],\"database_schema\":{\"Posts\":{\"post_id\":\"INT\",\"user_id\":\"INT\",\"post_date\":\"DATE\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Posts (post_id int, user_id int, post_date date)",
|
||||
"Truncate table Posts",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('1', '1', '2024-02-27')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('2', '5', '2024-02-06')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('3', '3', '2024-02-25')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('4', '3', '2024-02-14')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('5', '3', '2024-02-06')",
|
||||
"insert into Posts (post_id, user_id, post_date) values ('6', '2', '2024-02-25')"
|
||||
],
|
||||
"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>\"],\"pythondata\":[\"Pandas\",\"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>PostgreSQL 16<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{\"headers\": {\"Posts\": [\"post_id\", \"user_id\", \"post_date\"]}, \"rows\": {\"Posts\": [[1, 1, \"2024-02-27\"], [2, 5, \"2024-02-06\"], [3, 3, \"2024-02-25\"], [4, 3, \"2024-02-14\"], [5, 3, \"2024-02-06\"], [6, 2, \"2024-02-25\"]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.json
Normal file
183
leetcode-cn/originData/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/longest-common-suffix-queries.json
Normal file
183
leetcode-cn/originData/longest-common-suffix-queries.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
190
leetcode-cn/originData/most-frequent-ids.json
Normal file
190
leetcode-cn/originData/most-frequent-ids.json
Normal file
File diff suppressed because one or more lines are too long
52
leetcode-cn/problem (Chinese)/执行操作使数据元素之和大于等于 K [apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k].html
Normal file
52
leetcode-cn/problem (Chinese)/执行操作使数据元素之和大于等于 K [apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k].html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>给你一个<strong>正整数</strong> <code>k</code> 。最初,你有一个数组 <code>nums = [1]</code> 。</p>
|
||||
|
||||
<p>你可以对数组执行以下 <strong>任意 </strong>操作 <strong>任意 </strong>次数(<strong>可能为零</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>选择数组中的任何一个元素,然后将它的值<strong> 增加</strong> <code>1</code> 。</li>
|
||||
<li>复制数组中的任何一个元素,然后将它附加到数组的末尾。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回使得最终数组元素之<strong> 和 </strong>大于或等于 <code>k</code> 所需的 <strong>最少 </strong>操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong><span class="example-io">k = 11</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>可以对数组 <code>nums = [1]</code> 执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>将元素的值增加 <code>1</code> 三次。结果数组为 <code>nums = [4]</code> 。</li>
|
||||
<li>复制元素两次。结果数组为 <code>nums = [4,4,4]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>最终数组的和为 <code>4 + 4 + 4 = 12</code> ,大于等于 <code>k = 11</code> 。<br />
|
||||
执行的总操作次数为 <code>3 + 2 = 5</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong><span class="example-io">k = 1</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>原始数组的和已经大于等于 <code>1</code> ,因此不需要执行操作。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,57 @@
|
||||
<p>给你两个字符串数组 <code>wordsContainer</code> 和 <code>wordsQuery</code> 。</p>
|
||||
|
||||
<p>对于每个 <code>wordsQuery[i]</code> ,你需要从 <code>wordsContainer</code> 中找到一个与 <code>wordsQuery[i]</code> 有 <strong>最长公共后缀</strong> 的字符串。如果 <code>wordsContainer</code> 中有两个或者更多字符串有最长公共后缀,那么答案为长度<strong> 最短</strong> 的。如果有超过两个字符串有 <strong>相同</strong> 最短长度,那么答案为它们在 <code>wordsContainer</code> 中出现 <strong>更早</strong> 的一个。</p>
|
||||
|
||||
<p>请你返回一个整数数组<em> </em><code>ans</code> ,其中<em> </em><code>ans[i]</code>是<em> </em><code>wordsContainer</code>中与 <code>wordsQuery[i]</code> 有 <strong>最长公共后缀</strong> 字符串的下标。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>[1,1,1]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>我们分别来看每一个 <code>wordsQuery[i]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>对于 <code>wordsQuery[0] = "cd"</code> ,<code>wordsContainer</code> 中有最长公共后缀 <code>"cd"</code> 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。</li>
|
||||
<li>对于 <code>wordsQuery[1] = "bcd"</code> ,<code>wordsContainer</code> 中有最长公共后缀 <code>"bcd"</code> 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。</li>
|
||||
<li>对于 <code>wordsQuery[2] = "xyz"</code> ,<code>wordsContainer</code> 中没有字符串跟它有公共后缀,所以最长公共后缀为 <code>""</code> ,下标为 0 ,1 和 2 的字符串都得到这一公共后缀。这些字符串中, 答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>[2,0,2]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>我们分别来看每一个 <code>wordsQuery[i]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>对于 <code>wordsQuery[0] = "gh"</code> ,<code>wordsContainer</code> 中有最长公共后缀 <code>"gh"</code> 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 2 的字符串,因为它的长度为 6 ,是最短的字符串。</li>
|
||||
<li>对于 <code>wordsQuery[1] = "acbfgh"</code> ,只有下标为 0 的字符串有最长公共后缀 <code>"fgh"</code> 。所以尽管下标为 2 的字符串是最短的字符串,但答案是 0 。</li>
|
||||
<li>对于 <code>wordsQuery[2] = "acbfegh"</code> ,<code>wordsContainer</code> 中有最长公共后缀 <code>"gh"</code> 的字符串下标分别为 0 ,1 和 2 。这些字符串中,答案是下标为 2 的字符串,因为它的长度为 6 ,是最短的字符串。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= wordsContainer.length, wordsQuery.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= wordsContainer[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= wordsQuery[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>wordsContainer[i]</code> 只包含小写英文字母。</li>
|
||||
<li><code>wordsQuery[i]</code> 只包含小写英文字母。</li>
|
||||
<li><code>wordsContainer[i].length</code> 的和至多为 <code>5 * 10<sup>5</sup></code> 。</li>
|
||||
<li><code>wordsQuery[i].length</code> 的和至多为 <code>5 * 10<sup>5</sup></code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>你需要在一个集合里动态记录 ID 的出现频率。给你两个长度都为 <code>n</code> 的整数数组 <code>nums</code> 和 <code>freq</code> ,<code>nums</code> 中每一个元素表示一个 ID ,对应的 <code>freq</code> 中的元素表示这个 ID 在集合中此次操作后需要增加或者减少的数目。</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>增加 ID 的数目:</strong>如果 <code>freq[i]</code> 是正数,那么 <code>freq[i]</code> 个 ID 为 <code>nums[i]</code> 的元素在第 <code>i</code> 步操作后会添加到集合中。</li>
|
||||
<li><strong>减少 ID 的数目:</strong>如果 <code>freq[i]</code> 是负数,那么 <code>-freq[i]</code> 个 ID 为 <code>nums[i]</code> 的元素在第 <code>i</code> 步操作后会从集合中删除。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个长度为 <code>n</code> 的数组 <code>ans</code> ,其中 <code>ans[i]</code> 表示第 <code>i</code> 步操作后出现频率最高的 ID <strong>数目</strong> ,如果在某次操作后集合为空,那么 <code>ans[i]</code> 为 0 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [2,3,2,1], freq = [3,2,-3,1]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>[3,3,2,2]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>第 0 步操作后,有 3 个 ID 为 2 的元素,所以 <code>ans[0] = 3</code> 。<br />
|
||||
第 1 步操作后,有 3 个 ID 为 2 的元素和 2 个 ID 为 3 的元素,所以 <code>ans[1] = 3</code> 。<br />
|
||||
第 2 步操作后,有 2 个 ID 为 3 的元素,所以 <code>ans[2] = 2</code> 。<br />
|
||||
第 3 步操作后,有 2 个 ID 为 3 的元素和 1 个 ID 为 1 的元素,所以 <code>ans[3] = 2</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [5,5,3], freq = [2,-2,1]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>[2,0,1]</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>第 0 步操作后,有 2 个 ID 为 5 的元素,所以 <code>ans[0] = 2</code> 。<br />
|
||||
第 1 步操作后,集合中没有任何元素,所以 <code>ans[1] = 0</code> 。<br />
|
||||
第 2 步操作后,有 1 个 ID 为 3 的元素,所以 <code>ans[2] = 1</code> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == freq.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= freq[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>freq[i] != 0</code></li>
|
||||
<li>输入保证任何操作后,集合中的元素出现次数不会为负数。</li>
|
||||
</ul>
|
37
leetcode-cn/problem (Chinese)/每个字符最多出现两次的最长子字符串 [maximum-length-substring-with-two-occurrences].html
Normal file
37
leetcode-cn/problem (Chinese)/每个字符最多出现两次的最长子字符串 [maximum-length-substring-with-two-occurrences].html
Normal file
@ -0,0 +1,37 @@
|
||||
<p>给你一个字符串 <code>s</code> ,请找出满足每个字符最多出现两次的最长子字符串,并返回该<span data-keyword="substring">子字符串</span>的<strong> 最大 </strong>长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong> <span class="example-io">s = "bcbbbcba"</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>以下子字符串长度为 4,并且每个字符最多出现两次:<code>"bcbb<u>bcba</u>"</code>。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>输入:</strong> <span class="example-io">s = "aaaa"</span></p>
|
||||
|
||||
<p><strong>输出:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>以下子字符串长度为 2,并且每个字符最多出现两次:<code>"<u>aa</u>aa"</code>。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul><!-- 字符串 s 的长度在 2 到 100 之间 -->
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<!-- 字符串 s 仅包含小写英文字母 -->
|
||||
<li><code>s</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
50
leetcode-cn/problem (English)/执行操作使数据元素之和大于等于 K(English) [apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k].html
Normal file
50
leetcode-cn/problem (English)/执行操作使数据元素之和大于等于 K(English) [apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k].html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>k</code>. Initially, you have an array <code>nums = [1]</code>.</p>
|
||||
|
||||
<p>You can perform <strong>any</strong> of the following operations on the array <strong>any</strong> number of times (<strong>possibly zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose any element in the array and <strong>increase</strong> its value by <code>1</code>.</li>
|
||||
<li>Duplicate any element in the array and add it to the end of the array.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make the <strong>sum</strong> of elements of the final array greater than or equal to </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 11</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>We can do the following operations on the array <code>nums = [1]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase the element by <code>1</code> three times. The resulting array is <code>nums = [4]</code>.</li>
|
||||
<li>Duplicate the element two times. The resulting array is <code>nums = [4,4,4]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The sum of the final array is <code>4 + 4 + 4 = 12</code> which is greater than or equal to <code>k = 11</code>.<br />
|
||||
The total number of operations performed is <code>3 + 2 = 5</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The sum of the original array is already greater than or equal to <code>1</code>, so no operations are needed.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,55 @@
|
||||
<p>You are given two arrays of strings <code>wordsContainer</code> and <code>wordsQuery</code>.</p>
|
||||
|
||||
<p>For each <code>wordsQuery[i]</code>, you need to find a string from <code>wordsContainer</code> that has the <strong>longest common suffix</strong> with <code>wordsQuery[i]</code>. If there are two or more strings in <code>wordsContainer</code> that share the longest common suffix, find the string that is the <strong>smallest</strong> in length. If there are two or more such strings that have the <strong>same</strong> smallest length, find the one that occurred <strong>earlier</strong> in <code>wordsContainer</code>.</p>
|
||||
|
||||
<p>Return <em>an array of integers </em><code>ans</code><em>, where </em><code>ans[i]</code><em> is the index of the string in </em><code>wordsContainer</code><em> that has the <strong>longest common suffix</strong> with </em><code>wordsQuery[i]</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,1,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at each <code>wordsQuery[i]</code> separately:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>wordsQuery[0] = "cd"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"cd"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
<li>For <code>wordsQuery[1] = "bcd"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"bcd"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
<li>For <code>wordsQuery[2] = "xyz"</code>, there is no string from <code>wordsContainer</code> that shares a common suffix. Hence the longest common suffix is <code>""</code>, that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,0,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Let's look at each <code>wordsQuery[i]</code> separately:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>wordsQuery[0] = "gh"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"gh"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.</li>
|
||||
<li>For <code>wordsQuery[1] = "acbfgh"</code>, only the string at index 0 shares the longest common suffix <code>"fgh"</code>. Hence it is the answer, even though the string at index 2 is shorter.</li>
|
||||
<li>For <code>wordsQuery[2] = "acbfegh"</code>, strings from <code>wordsContainer</code> that share the longest common suffix <code>"gh"</code> are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= wordsContainer.length, wordsQuery.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= wordsContainer[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= wordsQuery[i].length <= 5 * 10<sup>3</sup></code></li>
|
||||
<li><code>wordsContainer[i]</code> consists only of lowercase English letters.</li>
|
||||
<li><code>wordsQuery[i]</code> consists only of lowercase English letters.</li>
|
||||
<li>Sum of <code>wordsContainer[i].length</code> is at most <code>5 * 10<sup>5</sup></code>.</li>
|
||||
<li>Sum of <code>wordsQuery[i].length</code> is at most <code>5 * 10<sup>5</sup></code>.</li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, <code>nums</code> and <code>freq</code>, of equal length <code>n</code>. Each element in <code>nums</code> represents an ID, and the corresponding element in <code>freq</code> indicates how many times that ID should be added to or removed from the collection at each step.</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Addition of IDs:</strong> If <code>freq[i]</code> is positive, it means <code>freq[i]</code> IDs with the value <code>nums[i]</code> are added to the collection at step <code>i</code>.</li>
|
||||
<li><strong>Removal of IDs:</strong> If <code>freq[i]</code> is negative, it means <code>-freq[i]</code> IDs with the value <code>nums[i]</code> are removed from the collection at step <code>i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an array <code>ans</code> of length <code>n</code>, where <code>ans[i]</code> represents the <strong>count</strong> of the <em>most frequent ID</em> in the collection after the <code>i<sup>th</sup></code> step. If the collection is empty at any step, <code>ans[i]</code> should be 0 for that step.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,2,1], freq = [3,2,-3,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,3,2,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>After step 0, we have 3 IDs with the value of 2. So <code>ans[0] = 3</code>.<br />
|
||||
After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So <code>ans[1] = 3</code>.<br />
|
||||
After step 2, we have 2 IDs with the value of 3. So <code>ans[2] = 2</code>.<br />
|
||||
After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So <code>ans[3] = 2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,3], freq = [2,-2,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,0,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>After step 0, we have 2 IDs with the value of 5. So <code>ans[0] = 2</code>.<br />
|
||||
After step 1, there are no IDs. So <code>ans[1] = 0</code>.<br />
|
||||
After step 2, we have 1 ID with the value of 3. So <code>ans[2] = 1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == freq.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= freq[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>freq[i] != 0</code></li>
|
||||
<li>The input is generated<!-- notionvc: a136b55a-f319-4fa6-9247-11be9f3b1db8 --> such that the occurrences of an ID will not be negative in any step.</li>
|
||||
</ul>
|
29
leetcode-cn/problem (English)/每个字符最多出现两次的最长子字符串(English) [maximum-length-substring-with-two-occurrences].html
Normal file
29
leetcode-cn/problem (English)/每个字符最多出现两次的最长子字符串(English) [maximum-length-substring-with-two-occurrences].html
Normal file
@ -0,0 +1,29 @@
|
||||
Given a string <code>s</code>, return the <strong>maximum</strong> length of a <span data-keyword="substring">substring</span> such that it contains <em>at most two occurrences</em> of each character.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "bcbbbcba"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
The following substring has a length of 4 and contains at most two occurrences of each character: <code>"bcbb<u>bcba</u>"</code>.</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "aaaa"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
The following substring has a length of 2 and contains at most two occurrences of each character: <code>"<u>aa</u>aa"</code>.</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user