mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
19b3e4846b
commit
6ba311f0f3
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.05.24**
|
||||
> 最后更新日期: **2022.06.10**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
50
leetcode-cn/originData/[no content]jump-game-ix.json
Normal file
50
leetcode-cn/originData/[no content]jump-game-ix.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
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2431",
|
||||
"questionFrontendId": "2292",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 1532945,
|
||||
"title": "Products With Three or More Orders in Two Consecutive Years",
|
||||
"titleSlug": "products-with-three-or-more-orders-in-two-consecutive-years",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"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\": \"47\", \"totalSubmission\": \"100\", \"totalAcceptedRaw\": 47, \"totalSubmissionRaw\": 100, \"acRate\": \"47.0%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Orders\": [\"order_id\", \"product_id\", \"quantity\", \"purchase_date\"]}, \"rows\": {\"Orders\": [[1, 1, 7, \"2020-03-16\"], [2, 1, 4, \"2020-12-02\"], [3, 1, 7, \"2020-05-10\"], [4, 1, 6, \"2021-12-23\"], [5, 1, 5, \"2021-05-21\"], [6, 1, 6, \"2021-10-11\"], [7, 2, 6, \"2022-10-11\"]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Orders (order_id int, product_id int, quantity int, purchase_date date)\"\n ],\n \"mssql\": [\n \"Create table Orders (order_id int, product_id int, quantity int, purchase_date date)\"\n ],\n \"oraclesql\": [\n \"Create table Orders (order_id int, product_id int, quantity int, purchase_date date)\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Orders (order_id int, product_id int, quantity int, purchase_date date)",
|
||||
"Truncate table Orders",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('1', '1', '7', '2020-03-16')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('2', '1', '4', '2020-12-02')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('3', '1', '7', '2020-05-10')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('4', '1', '6', '2021-12-23')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('5', '1', '5', '2021-05-21')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('6', '1', '6', '2021-10-11')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('7', '2', '6', '2022-10-11')"
|
||||
],
|
||||
"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\": {\"Orders\": [\"order_id\", \"product_id\", \"quantity\", \"purchase_date\"]}, \"rows\": {\"Orders\": [[1, 1, 7, \"2020-03-16\"], [2, 1, 4, \"2020-12-02\"], [3, 1, 7, \"2020-05-10\"], [4, 1, 6, \"2021-12-23\"], [5, 1, 5, \"2021-05-21\"], [6, 1, 6, \"2021-10-11\"], [7, 2, 6, \"2022-10-11\"]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
165
leetcode-cn/originData/apply-discount-to-prices.json
Normal file
165
leetcode-cn/originData/apply-discount-to-prices.json
Normal file
File diff suppressed because one or more lines are too long
185
leetcode-cn/originData/booking-concert-tickets-in-groups.json
Normal file
185
leetcode-cn/originData/booking-concert-tickets-in-groups.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
197
leetcode-cn/originData/design-a-text-editor.json
Normal file
197
leetcode-cn/originData/design-a-text-editor.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/maximum-total-importance-of-roads.json
Normal file
183
leetcode-cn/originData/maximum-total-importance-of-roads.json
Normal file
File diff suppressed because one or more lines are too long
171
leetcode-cn/originData/min-max-game.json
Normal file
171
leetcode-cn/originData/min-max-game.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
177
leetcode-cn/originData/replace-elements-in-an-array.json
Normal file
177
leetcode-cn/originData/replace-elements-in-an-array.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/sender-with-largest-word-count.json
Normal file
183
leetcode-cn/originData/sender-with-largest-word-count.json
Normal file
File diff suppressed because one or more lines are too long
184
leetcode-cn/originData/steps-to-make-array-non-decreasing.json
Normal file
184
leetcode-cn/originData/steps-to-make-array-non-decreasing.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,56 @@
|
||||
<p>一个音乐会总共有 <code>n</code> 排座位,编号从 <code>0</code> 到 <code>n - 1</code> ,每一排有 <code>m</code> 个座椅,编号为 <code>0</code> 到 <code>m - 1</code> 。你需要设计一个买票系统,针对以下情况进行座位安排:</p>
|
||||
|
||||
<ul>
|
||||
<li>同一组的 <code>k</code> 位观众坐在<strong> 同一排座位,且座位连续 </strong>。</li>
|
||||
<li><code>k</code> 位观众中 <strong>每一位</strong> 都有座位坐,但他们 <strong>不一定</strong> 坐在一起。</li>
|
||||
</ul>
|
||||
|
||||
<p>由于观众非常挑剔,所以:</p>
|
||||
|
||||
<ul>
|
||||
<li>只有当一个组里所有成员座位的排数都 <strong>小于等于</strong> <code>maxRow</code> ,这个组才能订座位。每一组的 <code>maxRow</code> 可能 <strong>不同</strong> 。</li>
|
||||
<li>如果有多排座位可以选择,优先选择 <strong>最小</strong> 的排数。如果同一排中有多个座位可以坐,优先选择号码 <strong>最小</strong> 的。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你实现 <code>BookMyShow</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>BookMyShow(int n, int m)</code> ,初始化对象,<code>n</code> 是排数,<code>m</code> 是每一排的座位数。</li>
|
||||
<li><code>int[] gather(int k, int maxRow)</code> 返回长度为 <code>2</code> 的数组,表示 <code>k</code> 个成员中 <strong>第一个座位</strong> 的排数和座位编号,这 <code>k</code> 位成员必须坐在 <strong>同一排座位,且座位连续 </strong>。换言之,返回最小可能的 <code>r</code> 和 <code>c</code> 满足第 <code>r</code> 排中 <code>[c, c + k - 1]</code> 的座位都是空的,且 <code>r <= maxRow</code> 。如果 <strong>无法</strong> 安排座位,返回 <code>[]</code> 。</li>
|
||||
<li><code>boolean scatter(int k, int maxRow)</code> 如果组里所有 <code>k</code> 个成员 <strong>不一定</strong> 要坐在一起的前提下,都能在第 <code>0</code> 排到第 <code>maxRow</code> 排之间找到座位,那么请返回 <code>true</code> 。这种情况下,每个成员都优先找排数 <strong>最小</strong> ,然后是座位编号最小的座位。如果不能安排所有 <code>k</code> 个成员的座位,请返回 <code>false</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["BookMyShow", "gather", "gather", "scatter", "scatter"]
|
||||
[[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
|
||||
<strong>输出:</strong>
|
||||
[null, [0, 0], [], true, false]
|
||||
|
||||
<strong>解释:</strong>
|
||||
BookMyShow bms = new BookMyShow(2, 5); // 总共有 2 排,每排 5 个座位。
|
||||
bms.gather(4, 0); // 返回 [0, 0]
|
||||
// 这一组安排第 0 排 [0, 3] 的座位。
|
||||
bms.gather(2, 0); // 返回 []
|
||||
// 第 0 排只剩下 1 个座位。
|
||||
// 所以无法安排 2 个连续座位。
|
||||
bms.scatter(5, 1); // 返回 True
|
||||
// 这一组安排第 0 排第 4 个座位和第 1 排 [0, 3] 的座位。
|
||||
bms.scatter(5, 1); // 返回 False
|
||||
// 总共只剩下 2 个座位。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m, k <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= maxRow <= n - 1</code></li>
|
||||
<li><code>gather</code> 和 <code>scatter</code> <strong>总</strong> 调用次数不超过 <code>5 * 10<sup>4</sup></code> 次。</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p><strong>句子</strong> 是由若干个单词组成的字符串,单词之间用单个空格分隔,其中每个单词可以包含数字、小写字母、和美元符号 <code>'$'</code> 。如果单词的形式为美元符号后跟着一个非负实数,那么这个单词就表示一个价格。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如 <code>"$100"</code>、<code>"$23"</code> 和 <code>"$6.75"</code> 表示价格,而 <code>"100"</code>、<code>"$"</code> 和 <code>"2$3"</code> 不是。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意:</strong>本题输入中的价格均为整数。</p>
|
||||
|
||||
<p>给你一个字符串 <code>sentence</code> 和一个整数 <code>discount</code> 。对于每个表示价格的单词,都在价格的基础上减免 <code>discount%</code> ,并 <strong>更新</strong> 该单词到句子中。所有更新后的价格应该表示为一个 <strong>恰好保留小数点后两位</strong> 的数字。</p>
|
||||
|
||||
<p>返回表示修改后句子的字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50
|
||||
<strong>输出:</strong>"there are $0.50 $1.00 and 5$ candies in the shop"
|
||||
<strong>解释:</strong>
|
||||
表示价格的单词是 "$1" 和 "$2" 。
|
||||
- "$1" 减免 50% 为 "$0.50" ,所以 "$1" 替换为 "$0.50" 。
|
||||
- "$2" 减免 50% 为 "$1" ,所以 "$1" 替换为 "$1.00" 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100
|
||||
<strong>输出:</strong>"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"
|
||||
<strong>解释:</strong>
|
||||
任何价格减免 100% 都会得到 0 。
|
||||
表示价格的单词分别是 "$3"、"$5"、"$6" 和 "$9"。
|
||||
每个单词都替换为 "$0.00"。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>sentence</code> 由小写英文字母、数字、<code>' '</code> 和 <code>'$'</code> 组成</li>
|
||||
<li><code>sentence</code> 不含前导和尾随空格</li>
|
||||
<li><code>sentence</code> 的所有单词都用单个空格分隔</li>
|
||||
<li>所有价格都是 <strong>正</strong> 整数且不含前导零</li>
|
||||
<li>所有价格 <strong>最多</strong> 为 <code>10</code> 位数字</li>
|
||||
<li><code>0 <= discount <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。在一步操作中,移除所有满足 <code>nums[i - 1] > nums[i]</code> 的 <code>nums[i]</code> ,其中 <code>0 < i < nums.length</code> 。</p>
|
||||
|
||||
<p>重复执行步骤,直到 <code>nums</code> 变为 <strong>非递减</strong> 数组,返回所需执行的操作数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,3,4,4,7,3,6,11,8,5,11]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>执行下述几个步骤:
|
||||
- 步骤 1 :[5,<em><strong>3</strong></em>,4,4,7,<em><strong>3</strong></em>,6,11,<em><strong>8</strong></em>,<em><strong>5</strong></em>,11] 变为 [5,4,4,7,6,11,11]
|
||||
- 步骤 2 :[5,<em><strong>4</strong></em>,4,7,<em><strong>6</strong></em>,11,11] 变为 [5,4,7,11,11]
|
||||
- 步骤 3 :[5,<em><strong>4</strong></em>,7,11,11] 变为 [5,7,11,11]
|
||||
[5,7,11,11] 是一个非递减数组,因此,返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,5,7,7,13]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums 已经是一个非递减数组,因此,返回 0 。
|
||||
</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>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,54 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。你可以将 <code>nums</code> 划分成一个或多个 <strong>子序列</strong> ,使 <code>nums</code> 中的每个元素都 <strong>恰好</strong> 出现在一个子序列中。</p>
|
||||
|
||||
<p>在满足每个子序列中最大值和最小值之间的差值最多为 <code>k</code> 的前提下,返回需要划分的 <strong>最少</strong> 子序列数目。</p>
|
||||
|
||||
<p><strong>子序列</strong> 本质是一个序列,可以通过删除另一个序列中的某些元素(或者不删除)但不改变剩下元素的顺序得到。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,6,1,2,5], k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
可以将 nums 划分为两个子序列 [3,1,2] 和 [6,5] 。
|
||||
第一个子序列中最大值和最小值的差值是 3 - 1 = 2 。
|
||||
第二个子序列中最大值和最小值的差值是 6 - 5 = 1 。
|
||||
由于创建了两个子序列,返回 2 。可以证明需要划分的最少子序列数目就是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3], k = 1
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
可以将 nums 划分为两个子序列 [1,2] 和 [3] 。
|
||||
第一个子序列中最大值和最小值的差值是 2 - 1 = 1 。
|
||||
第二个子序列中最大值和最小值的差值是 3 - 3 = 0 。
|
||||
由于创建了两个子序列,返回 2 。注意,另一种最优解法是将 nums 划分成子序列 [1] 和 [2,3] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,2,4,5], k = 0
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
可以将 nums 划分为三个子序列 [2,2]、[4] 和 [5] 。
|
||||
第一个子序列中最大值和最小值的差值是 2 - 2 = 0 。
|
||||
第二个子序列中最大值和最小值的差值是 4 - 4 = 0 。
|
||||
第三个子序列中最大值和最小值的差值是 5 - 5 = 0 。
|
||||
由于创建了三个子序列,返回 3 。可以证明需要划分的最少子序列数目就是 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的字符串 <code>num</code> ,它只包含数字。</p>
|
||||
|
||||
<p>如果对于 <strong>每个</strong><em> </em><code>0 <= i < n</code> 的下标 <code>i</code> ,都满足数位<em> </em><code>i</code> 在 <code>num</code> 中出现了 <code>num[i]</code>次,那么请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>num = "1210"
|
||||
<b>输出:</b>true
|
||||
<strong>解释:</strong>
|
||||
num[0] = '1' 。数字 0 在 num 中出现了一次。
|
||||
num[1] = '2' 。数字 1 在 num 中出现了两次。
|
||||
num[2] = '1' 。数字 2 在 num 中出现了一次。
|
||||
num[3] = '0' 。数字 3 在 num 中出现了零次。
|
||||
"1210" 满足题目要求条件,所以返回 true 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>num = "030"
|
||||
<b>输出:</b>false
|
||||
<strong>解释:</strong>
|
||||
num[0] = '0' 。数字 0 应该出现 0 次,但是在 num 中出现了一次。
|
||||
num[1] = '3' 。数字 1 应该出现 3 次,但是在 num 中出现了零次。
|
||||
num[2] = '0' 。数字 2 在 num 中出现了 0 次。
|
||||
下标 0 和 1 都违反了题目要求,所以返回 false 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == num.length</code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>num</code> 只包含数字。</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>grid</code> ,数组大小为 <code>m x n</code> 。每个单元格都是两个值之一:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> 表示一个 <strong>空</strong> 单元格,</li>
|
||||
<li><code>1</code> 表示一个可以移除的 <strong>障碍物</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>你可以向上、下、左、右移动,从一个空单元格移动到另一个空单元格。</p>
|
||||
|
||||
<p>现在你需要从左上角 <code>(0, 0)</code> 移动到右下角 <code>(m - 1, n - 1)</code> ,返回需要移除的障碍物的 <strong>最小</strong> 数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio-1.png" style="width: 605px; height: 246px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1,1],[1,1,0],[1,1,0]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以移除位于 (0, 1) 和 (0, 2) 的障碍物来创建从 (0, 0) 到 (2, 2) 的路径。
|
||||
可以证明我们至少需要移除两个障碍物,所以返回 2 。
|
||||
注意,可能存在其他方式来移除 2 个障碍物,创建出可行的路径。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio.png" style="width: 405px; height: 246px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不移除任何障碍物就能从 (0, 0) 到 (2, 4) ,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> 为 <code>0</code> <strong>或</strong> <code>1</code></li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,它包含 <code>n</code> 个 <strong>互不相同</strong> 的正整数。请你对这个数组执行 <code>m</code> 个操作,在第 <code>i</code> 个操作中,你需要将数字 <code>operations[i][0]</code> 替换成 <code>operations[i][1]</code> 。</p>
|
||||
|
||||
<p>题目保证在第 <code>i</code> 个操作中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>operations[i][0]</code> 在 <code>nums</code> 中存在。</li>
|
||||
<li><code>operations[i][1]</code> 在 <code>nums</code> 中不存在。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回执行完所有操作后的数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
|
||||
<b>输出:</b>[3,2,7,1]
|
||||
<b>解释:</b>我们对 nums 执行以下操作:
|
||||
- 将数字 1 替换为 3 。nums 变为 [<em><strong>3</strong></em>,2,4,6] 。
|
||||
- 将数字 4 替换为 7 。nums 变为 [3,2,<em><strong>7</strong></em>,6] 。
|
||||
- 将数字 6 替换为 1 。nums 变为 [3,2,7,<em><strong>1</strong></em>] 。
|
||||
返回最终数组 [3,2,7,1] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2], operations = [[1,3],[2,1],[3,2]]
|
||||
<b>输出:</b>[2,1]
|
||||
<b>解释:</b>我们对 nums 执行以下操作:
|
||||
- 将数字 1 替换为 3 。nums 变为 [<em><strong>3</strong></em>,2] 。
|
||||
- 将数字 2 替换为 1 。nums 变为 [3,<em><strong>1</strong></em>] 。
|
||||
- 将数字 3 替换为 2 。nums 变为 [<em><strong>2</strong></em>,1] 。
|
||||
返回最终数组 [2,1] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == operations.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums</code> 中所有数字 <strong>互不相同</strong> 。</li>
|
||||
<li><code>operations[i].length == 2</code></li>
|
||||
<li><code>1 <= nums[i], operations[i][0], operations[i][1] <= 10<sup>6</sup></code></li>
|
||||
<li>在执行第 <code>i</code> 个操作时,<code>operations[i][0]</code> 在 <code>nums</code> 中存在。</li>
|
||||
<li>在执行第 <code>i</code> 个操作时,<code>operations[i][1]</code> 在 <code>nums</code> 中不存在。</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个聊天记录,共包含 <code>n</code> 条信息。给你两个字符串数组 <code>messages</code> 和 <code>senders</code> ,其中 <code>messages[i]</code> 是 <code>senders[i]</code> 发出的一条 <strong>信息</strong> 。</p>
|
||||
|
||||
<p>一条 <strong>信息</strong> 是若干用单个空格连接的 <strong>单词</strong> ,信息开头和结尾不会有多余空格。发件人的 <strong>单词计数</strong> 是这个发件人总共发出的 <strong>单词数</strong> 。注意,一个发件人可能会发出多于一条信息。</p>
|
||||
|
||||
<p>请你返回发出单词数 <strong>最多</strong> 的发件人名字。如果有多个发件人发出最多单词数,请你返回 <strong>字典序</strong> 最大的名字。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>字典序里,大写字母小于小写字母。</li>
|
||||
<li><code>"Alice"</code> 和 <code>"alice"</code> 是不同的名字。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
|
||||
<b>输出:</b>"Alice"
|
||||
<b>解释:</b>Alice 总共发出了 2 + 3 = 5 个单词。
|
||||
userTwo 发出了 2 个单词。
|
||||
userThree 发出了 3 个单词。
|
||||
由于 Alice 发出单词数最多,所以我们返回 "Alice" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
|
||||
<b>输出:</b>"Charlie"
|
||||
<b>解释:</b>Bob 总共发出了 5 个单词。
|
||||
Charlie 总共发出了 5 个单词。
|
||||
由于最多单词数打平,返回字典序最大的名字,也就是 Charlie 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == messages.length == senders.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= messages[i].length <= 100</code></li>
|
||||
<li><code>1 <= senders[i].length <= 10</code></li>
|
||||
<li><code>messages[i]</code> 包含大写字母、小写字母和 <code>' '</code> 。</li>
|
||||
<li><code>messages[i]</code> 中所有单词都由 <strong>单个空格</strong> 隔开。</li>
|
||||
<li><code>messages[i]</code> 不包含前导和后缀空格。</li>
|
||||
<li><code>senders[i]</code> 只包含大写英文字母和小写英文字母。</li>
|
||||
</ul>
|
47
leetcode-cn/problem (Chinese)/极大极小游戏 [min-max-game].html
Normal file
47
leetcode-cn/problem (Chinese)/极大极小游戏 [min-max-game].html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,其长度是 <code>2</code> 的幂。</p>
|
||||
|
||||
<p>对 <code>nums</code> 执行下述算法:</p>
|
||||
|
||||
<ol>
|
||||
<li>设 <code>n</code> 等于 <code>nums</code> 的长度,如果 <code>n == 1</code> ,<strong>终止</strong> 算法过程。否则,<strong>创建</strong> 一个新的整数数组 <code>newNums</code> ,新数组长度为 <code>n / 2</code> ,下标从 <strong>0</strong> 开始。</li>
|
||||
<li>对于满足 <code>0 <= i < n / 2</code> 的每个 <strong>偶数</strong> 下标 <code>i</code> ,将 <code>newNums[i]</code> <strong>赋值</strong> 为 <code>min(nums[2 * i], nums[2 * i + 1])</code> 。</li>
|
||||
<li>对于满足 <code>0 <= i < n / 2</code> 的每个 <strong>奇数</strong> 下标 <code>i</code> ,将 <code>newNums[i]</code> <strong>赋值</strong> 为 <code>max(nums[2 * i], nums[2 * i + 1])</code> 。</li>
|
||||
<li>用 <code>newNums</code> 替换 <code>nums</code> 。</li>
|
||||
<li>从步骤 1 开始 <strong>重复</strong> 整个过程。</li>
|
||||
</ol>
|
||||
|
||||
<p>执行算法后,返回 <code>nums</code> 中剩下的那个数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/13/example1drawio-1.png" style="width: 500px; height: 240px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,5,2,4,8,2,2]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>重复执行算法会得到下述数组。
|
||||
第一轮:nums = [1,5,4,2]
|
||||
第二轮:nums = [1,4]
|
||||
第三轮:nums = [1]
|
||||
1 是最后剩下的那个数字,返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>3 就是最后剩下的数字,返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1024</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>nums.length</code> 是 <code>2</code> 的幂</li>
|
||||
</ul>
|
@ -0,0 +1,70 @@
|
||||
<p>请你设计一个带光标的文本编辑器,它可以实现以下功能:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>添加:</strong>在光标所在处添加文本。</li>
|
||||
<li><strong>删除:</strong>在光标所在处删除文本(模拟键盘的删除键)。</li>
|
||||
<li><strong>移动:</strong>将光标往左或者往右移动。</li>
|
||||
</ul>
|
||||
|
||||
<p>当删除文本时,只有光标左边的字符会被删除。光标会留在文本内,也就是说任意时候 <code>0 <= cursor.position <= currentText.length</code> 都成立。</p>
|
||||
|
||||
<p>请你实现 <code>TextEditor</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>TextEditor()</code> 用空文本初始化对象。</li>
|
||||
<li><code>void addText(string text)</code> 将 <code>text</code> 添加到光标所在位置。添加完后光标在 <code>text</code> 的右边。</li>
|
||||
<li><code>int deleteText(int k)</code> 删除光标左边 <code>k</code> 个字符。返回实际删除的字符数目。</li>
|
||||
<li><code>string cursorLeft(int k)</code> 将光标向左移动 <code>k</code> 次。返回移动后光标左边 <code>min(10, len)</code> 个字符,其中 <code>len</code> 是光标左边的字符数目。</li>
|
||||
<li><code>string cursorRight(int k)</code> 将光标向右移动 <code>k</code> 次。返回移动后光标左边 <code>min(10, len)</code> 个字符,其中 <code>len</code> 是光标左边的字符数目。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"]
|
||||
[[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]
|
||||
<strong>输出:</strong>
|
||||
[null, null, 4, null, "etpractice", "leet", 4, "", "practi"]
|
||||
|
||||
<strong>解释:</strong>
|
||||
TextEditor textEditor = new TextEditor(); // 当前 text 为 "|" 。('|' 字符表示光标)
|
||||
textEditor.addText("leetcode"); // 当前文本为 "leetcode|" 。
|
||||
textEditor.deleteText(4); // 返回 4
|
||||
// 当前文本为 "leet|" 。
|
||||
// 删除了 4 个字符。
|
||||
textEditor.addText("practice"); // 当前文本为 "leetpractice|" 。
|
||||
textEditor.cursorRight(3); // 返回 "etpractice"
|
||||
// 当前文本为 "leetpractice|".
|
||||
// 光标无法移动到文本以外,所以无法移动。
|
||||
// "etpractice" 是光标左边的 10 个字符。
|
||||
textEditor.cursorLeft(8); // 返回 "leet"
|
||||
// 当前文本为 "leet|practice" 。
|
||||
// "leet" 是光标左边的 min(10, 4) = 4 个字符。
|
||||
textEditor.deleteText(10); // 返回 4
|
||||
// 当前文本为 "|practice" 。
|
||||
// 只有 4 个字符被删除了。
|
||||
textEditor.cursorLeft(2); // 返回 ""
|
||||
// 当前文本为 "|practice" 。
|
||||
// 光标无法移动到文本以外,所以无法移动。
|
||||
// "" 是光标左边的 min(10, 0) = 0 个字符。
|
||||
textEditor.cursorRight(6); // 返回 "practi"
|
||||
// 当前文本为 "practi|ce" 。
|
||||
// "practi" 是光标左边的 min(10, 6) = 6 个字符。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length, k <= 40</code></li>
|
||||
<li><code>text</code> 只含有小写英文字母。</li>
|
||||
<li>调用 <code>addText</code> ,<code>deleteText</code> ,<code>cursorLeft</code> 和 <code>cursorRight</code> 的 <strong>总</strong> 次数不超过 <code>2 * 10<sup>4</sup></code> 次。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你能设计并实现一个每次调用时间复杂度为 <code>O(k)</code> 的解决方案吗?</p>
|
@ -0,0 +1,53 @@
|
||||
<p>给你一个整数 <code>n</code> ,表示一个国家里的城市数目。城市编号为 <code>0</code> 到 <code>n - 1</code> 。</p>
|
||||
|
||||
<p>给你一个二维整数数组 <code>roads</code> ,其中 <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示城市 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条 <strong>双向</strong> 道路。</p>
|
||||
|
||||
<p>你需要给每个城市安排一个从 <code>1</code> 到 <code>n</code> 之间的整数值,且每个值只能被使用 <strong>一次</strong> 。道路的 <strong>重要性</strong> 定义为这条道路连接的两座城市数值 <strong>之和</strong> 。</p>
|
||||
|
||||
<p>请你返回在最优安排下,<strong>所有道路重要性</strong> 之和 <strong>最大</strong> 为多少。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png" style="width: 290px; height: 215px;"></p>
|
||||
|
||||
<pre><b>输入:</b>n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
|
||||
<b>输出:</b>43
|
||||
<b>解释:</b>上图展示了国家图和每个城市被安排的值 [2,4,5,3,1] 。
|
||||
- 道路 (0,1) 重要性为 2 + 4 = 6 。
|
||||
- 道路 (1,2) 重要性为 4 + 5 = 9 。
|
||||
- 道路 (2,3) 重要性为 5 + 3 = 8 。
|
||||
- 道路 (0,2) 重要性为 2 + 5 = 7 。
|
||||
- 道路 (1,3) 重要性为 4 + 3 = 7 。
|
||||
- 道路 (2,4) 重要性为 5 + 1 = 6 。
|
||||
所有道路重要性之和为 6 + 9 + 8 + 7 + 7 + 6 = 43 。
|
||||
可以证明,重要性之和不可能超过 43 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png" style="width: 281px; height: 151px;"></p>
|
||||
|
||||
<pre><b>输入:</b>n = 5, roads = [[0,3],[2,4],[1,3]]
|
||||
<b>输出:</b>20
|
||||
<b>解释:</b>上图展示了国家图和每个城市被安排的值 [4,3,2,5,1] 。
|
||||
- 道路 (0,3) 重要性为 4 + 5 = 9 。
|
||||
- 道路 (2,4) 重要性为 2 + 1 = 3 。
|
||||
- 道路 (1,3) 重要性为 3 + 5 = 8 。
|
||||
所有道路重要性之和为 9 + 3 + 8 = 20 。
|
||||
可以证明,重要性之和不可能超过 20 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= roads.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>roads[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>没有重复道路。</li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的字符串 <code>s</code> 和 <code>target</code> 。你可以从 <code>s</code> 取出一些字符并将其重排,得到若干新的字符串。</p>
|
||||
|
||||
<p>从 <code>s</code> 中取出字符并重新排列,返回可以形成 <code>target</code> 的 <strong>最大</strong> 副本数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "ilovecodingonleetcode", target = "code"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
对于 "code" 的第 1 个副本,选取下标为 4 、5 、6 和 7 的字符。
|
||||
对于 "code" 的第 2 个副本,选取下标为 17 、18 、19 和 20 的字符。
|
||||
形成的字符串分别是 "ecod" 和 "code" ,都可以重排为 "code" 。
|
||||
可以形成最多 2 个 "code" 的副本,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abcba", target = "abc"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>
|
||||
选取下标为 0 、1 和 2 的字符,可以形成 "abc" 的 1 个副本。
|
||||
可以形成最多 1 个 "abc" 的副本,所以返回 1 。
|
||||
注意,尽管下标 3 和 4 分别有额外的 'a' 和 'b' ,但不能重用下标 2 处的 'c' ,所以无法形成 "abc" 的第 2 个副本。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abbaccaddaeea", target = "aaaaa"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>
|
||||
选取下标为 0 、3 、6 、9 和 12 的字符,可以形成 "aaaaa" 的 1 个副本。
|
||||
可以形成最多 1 个 "aaaaa" 的副本,所以返回 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>1 <= target.length <= 10</code></li>
|
||||
<li><code>s</code> 和 <code>target</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@ -0,0 +1,54 @@
|
||||
<p>A concert hall has <code>n</code> rows numbered from <code>0</code> to <code>n - 1</code>, each with <code>m</code> seats, numbered from <code>0</code> to <code>m - 1</code>. You need to design a ticketing system that can allocate seats in the following cases:</p>
|
||||
|
||||
<ul>
|
||||
<li>If a group of <code>k</code> spectators can sit <strong>together</strong> in a row.</li>
|
||||
<li>If <strong>every</strong> member of a group of <code>k</code> spectators can get a seat. They may or <strong>may not</strong> sit together.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that the spectators are very picky. Hence:</p>
|
||||
|
||||
<ul>
|
||||
<li>They will book seats only if each member of their group can get a seat with row number <strong>less than or equal</strong> to <code>maxRow</code>. <code>maxRow</code> can <strong>vary</strong> from group to group.</li>
|
||||
<li>In case there are multiple rows to choose from, the row with the <strong>smallest</strong> number is chosen. If there are multiple seats to choose in the same row, the seat with the <strong>smallest</strong> number is chosen.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>BookMyShow</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>BookMyShow(int n, int m)</code> Initializes the object with <code>n</code> as number of rows and <code>m</code> as number of seats per row.</li>
|
||||
<li><code>int[] gather(int k, int maxRow)</code> Returns an array of length <code>2</code> denoting the row and seat number (respectively) of the <strong>first seat</strong> being allocated to the <code>k</code> members of the group, who must sit <strong>together</strong>. In other words, it returns the smallest possible <code>r</code> and <code>c</code> such that all <code>[c, c + k - 1]</code> seats are valid and empty in row <code>r</code>, and <code>r <= maxRow</code>. Returns <code>[]</code> in case it is <strong>not possible</strong> to allocate seats to the group.</li>
|
||||
<li><code>boolean scatter(int k, int maxRow)</code> Returns <code>true</code> if all <code>k</code> members of the group can be allocated seats in rows <code>0</code> to <code>maxRow</code>, who may or <strong>may not</strong> sit together. If the seats can be allocated, it allocates <code>k</code> seats to the group with the <strong>smallest</strong> row numbers, and the smallest possible seat numbers in each row. Otherwise, returns <code>false</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["BookMyShow", "gather", "gather", "scatter", "scatter"]
|
||||
[[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
|
||||
<strong>Output</strong>
|
||||
[null, [0, 0], [], true, false]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each
|
||||
bms.gather(4, 0); // return [0, 0]
|
||||
// The group books seats [0, 3] of row 0.
|
||||
bms.gather(2, 0); // return []
|
||||
// There is only 1 seat left in row 0,
|
||||
// so it is not possible to book 2 consecutive seats.
|
||||
bms.scatter(5, 1); // return True
|
||||
// The group books seat 4 of row 0 and seats [0, 3] of row 1.
|
||||
bms.scatter(5, 1); // return False
|
||||
// There is only one seat left in the hall.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m, k <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= maxRow <= n - 1</code></li>
|
||||
<li>At most <code>5 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>gather</code> and <code>scatter</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>A <strong>sentence</strong> is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign <code>'$'</code>. A word represents a <strong>price</strong> if it is a sequence of digits preceded by a dollar sign.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"$100"</code>, <code>"$23"</code>, and <code>"$6"</code> represent prices while <code>"100"</code>, <code>"$"</code>, and <code>"$1e5"</code> do not.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given a string <code>sentence</code> representing a sentence and an integer <code>discount</code>. For each word representing a price, apply a discount of <code>discount%</code> on the price and <strong>update</strong> the word in the sentence. All updated prices should be represented with <strong>exactly two</strong> decimal places.</p>
|
||||
|
||||
<p>Return <em>a string representing the modified sentence</em>.</p>
|
||||
|
||||
<p>Note that all prices will contain <strong>at most</strong> <code>10</code> digits.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50
|
||||
<strong>Output:</strong> "there are $0.50 $1.00 and 5$ candies in the shop"
|
||||
<strong>Explanation:</strong>
|
||||
The words which represent prices are "$1" and "$2".
|
||||
- A 50% discount on "$1" yields "$0.50", so "$1" is replaced by "$0.50".
|
||||
- A 50% discount on "$2" yields "$1". Since we need to have exactly 2 decimal places after a price, we replace "$2" with "$1.00".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100
|
||||
<strong>Output:</strong> "1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"
|
||||
<strong>Explanation:</strong>
|
||||
Applying a 100% discount on any price will result in 0.
|
||||
The words representing prices are "$3", "$5", "$6", and "$9".
|
||||
Each of them is replaced by "$0.00".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>sentence</code> consists of lowercase English letters, digits, <code>' '</code>, and <code>'$'</code>.</li>
|
||||
<li><code>sentence</code> does not have leading or trailing spaces.</li>
|
||||
<li>All words in <code>sentence</code> are separated by a single space.</li>
|
||||
<li>All prices will be <strong>positive</strong> integers without leading zeros.</li>
|
||||
<li>All prices will have <strong>at most</strong> <code>10</code> digits.</li>
|
||||
<li><code>0 <= discount <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one step, <strong>remove</strong> all elements <code>nums[i]</code> where <code>nums[i - 1] > nums[i]</code> for all <code>0 < i < nums.length</code>.</p>
|
||||
|
||||
<p>Return <em>the number of steps performed until </em><code>nums</code><em> becomes a <strong>non-decreasing</strong> array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,3,4,4,7,3,6,11,8,5,11]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following are the steps performed:
|
||||
- Step 1: [5,<strong><u>3</u></strong>,4,4,7,<u><strong>3</strong></u>,6,11,<u><strong>8</strong></u>,<u><strong>5</strong></u>,11] becomes [5,4,4,7,6,11,11]
|
||||
- Step 2: [5,<u><strong>4</strong></u>,4,7,<u><strong>6</strong></u>,11,11] becomes [5,4,7,11,11]
|
||||
- Step 3: [5,<u><strong>4</strong></u>,7,11,11] becomes [5,7,11,11]
|
||||
[5,7,11,11] is a non-decreasing array. Therefore, we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,5,7,7,13]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> nums is already a non-decreasing array. Therefore, we return 0.
|
||||
</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>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You may partition <code>nums</code> into one or more <strong>subsequences</strong> such that each element in <code>nums</code> appears in <strong>exactly</strong> one of the subsequences.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum </strong>number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is <strong>at most</strong> </em><code>k</code><em>.</em></p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,6,1,2,5], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can partition nums into the two subsequences [3,1,2] and [6,5].
|
||||
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
|
||||
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
|
||||
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can partition nums into the two subsequences [1,2] and [3].
|
||||
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
|
||||
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
|
||||
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,4,5], k = 0
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
We can partition nums into the three subsequences [2,2], [4], and [5].
|
||||
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
|
||||
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
|
||||
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
|
||||
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>num</code> of length <code>n</code> consisting of digits.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if for <strong>every</strong> index </em><code>i</code><em> in the range </em><code>0 <= i < n</code><em>, the digit </em><code>i</code><em> occurs </em><code>num[i]</code><em> times in </em><code>num</code><em>, otherwise return </em><code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "1210"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
num[0] = '1'. The digit 0 occurs once in num.
|
||||
num[1] = '2'. The digit 1 occurs twice in num.
|
||||
num[2] = '1'. The digit 2 occurs once in num.
|
||||
num[3] = '0'. The digit 3 occurs zero times in num.
|
||||
The condition holds true for every index in "1210", so return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "030"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.
|
||||
num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.
|
||||
num[2] = '0'. The digit 2 occurs zero times in num.
|
||||
The indices 0 and 1 both violate the condition, so return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == num.length</code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>num</code> consists of digits.</li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code>. Each cell has one of two values:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> represents an <strong>empty</strong> cell,</li>
|
||||
<li><code>1</code> represents an <strong>obstacle</strong> that may be removed.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can move up, down, left, or right from and to an empty cell.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of <strong>obstacles</strong> to <strong>remove</strong> so you can move from the upper left corner </em><code>(0, 0)</code><em> to the lower right corner </em><code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio-1.png" style="width: 605px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,1],[1,1,0],[1,1,0]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).
|
||||
It can be shown that we need to remove at least 2 obstacles, so we return 2.
|
||||
Note that there may be other ways to remove 2 obstacles to create a path.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio.png" style="width: 405px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> <strong>or</strong> <code>1</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> that consists of <code>n</code> <strong>distinct</strong> positive integers. Apply <code>m</code> operations to this array, where in the <code>i<sup>th</sup></code> operation you replace the number <code>operations[i][0]</code> with <code>operations[i][1]</code>.</p>
|
||||
|
||||
<p>It is guaranteed that in the <code>i<sup>th</sup></code> operation:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>operations[i][0]</code> <strong>exists</strong> in <code>nums</code>.</li>
|
||||
<li><code>operations[i][1]</code> does <strong>not</strong> exist in <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array obtained after applying all the operations</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
|
||||
<strong>Output:</strong> [3,2,7,1]
|
||||
<strong>Explanation:</strong> We perform the following operations on nums:
|
||||
- Replace the number 1 with 3. nums becomes [<u><strong>3</strong></u>,2,4,6].
|
||||
- Replace the number 4 with 7. nums becomes [3,2,<u><strong>7</strong></u>,6].
|
||||
- Replace the number 6 with 1. nums becomes [3,2,7,<u><strong>1</strong></u>].
|
||||
We return the final array [3,2,7,1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2], operations = [[1,3],[2,1],[3,2]]
|
||||
<strong>Output:</strong> [2,1]
|
||||
<strong>Explanation:</strong> We perform the following operations to nums:
|
||||
- Replace the number 1 with 3. nums becomes [<u><strong>3</strong></u>,2].
|
||||
- Replace the number 2 with 1. nums becomes [3,<u><strong>1</strong></u>].
|
||||
- Replace the number 3 with 2. nums becomes [<u><strong>2</strong></u>,1].
|
||||
We return the array [2,1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == operations.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li>All the values of <code>nums</code> are <strong>distinct</strong>.</li>
|
||||
<li><code>operations[i].length == 2</code></li>
|
||||
<li><code>1 <= nums[i], operations[i][0], operations[i][1] <= 10<sup>6</sup></code></li>
|
||||
<li><code>operations[i][0]</code> will exist in <code>nums</code> when applying the <code>i<sup>th</sup></code> operation.</li>
|
||||
<li><code>operations[i][1]</code> will not exist in <code>nums</code> when applying the <code>i<sup>th</sup></code> operation.</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You have a chat log of <code>n</code> messages. You are given two string arrays <code>messages</code> and <code>senders</code> where <code>messages[i]</code> is a <strong>message</strong> sent by <code>senders[i]</code>.</p>
|
||||
|
||||
<p>A <strong>message</strong> is list of <strong>words</strong> that are separated by a single space with no leading or trailing spaces. The <strong>word count</strong> of a sender is the total number of <strong>words</strong> sent by the sender. Note that a sender may send more than one message.</p>
|
||||
|
||||
<p>Return <em>the sender with the <strong>largest</strong> word count</em>. If there is more than one sender with the largest word count, return <em>the one with the <strong>lexicographically largest</strong> name</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Uppercase letters come before lowercase letters in lexicographical order.</li>
|
||||
<li><code>"Alice"</code> and <code>"alice"</code> are distinct.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
|
||||
<strong>Output:</strong> "Alice"
|
||||
<strong>Explanation:</strong> Alice sends a total of 2 + 3 = 5 words.
|
||||
userTwo sends a total of 2 words.
|
||||
userThree sends a total of 3 words.
|
||||
Since Alice has the largest word count, we return "Alice".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
|
||||
<strong>Output:</strong> "Charlie"
|
||||
<strong>Explanation:</strong> Bob sends a total of 5 words.
|
||||
Charlie sends a total of 5 words.
|
||||
Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == messages.length == senders.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= messages[i].length <= 100</code></li>
|
||||
<li><code>1 <= senders[i].length <= 10</code></li>
|
||||
<li><code>messages[i]</code> consists of uppercase and lowercase English letters and <code>' '</code>.</li>
|
||||
<li>All the words in <code>messages[i]</code> are separated by <strong>a single space</strong>.</li>
|
||||
<li><code>messages[i]</code> does not have leading or trailing spaces.</li>
|
||||
<li><code>senders[i]</code> consists of uppercase and lowercase English letters only.</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> whose length is a power of <code>2</code>.</p>
|
||||
|
||||
<p>Apply the following algorithm on <code>nums</code>:</p>
|
||||
|
||||
<ol>
|
||||
<li>Let <code>n</code> be the length of <code>nums</code>. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n / 2</code>.</li>
|
||||
<li>For every <strong>even</strong> index <code>i</code> where <code>0 <= i < n / 2</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>min(nums[2 * i], nums[2 * i + 1])</code>.</li>
|
||||
<li>For every <strong>odd</strong> index <code>i</code> where <code>0 <= i < n / 2</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>max(nums[2 * i], nums[2 * i + 1])</code>.</li>
|
||||
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
|
||||
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the last number that remains in </em><code>nums</code><em> after applying the algorithm.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/13/example1drawio-1.png" style="width: 500px; height: 240px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,4,8,2,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The following arrays are the results of applying the algorithm repeatedly.
|
||||
First: nums = [1,5,4,2]
|
||||
Second: nums = [1,4]
|
||||
Third: nums = [1]
|
||||
1 is the last remaining number, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 is already the last remaining number, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1024</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>nums.length</code> is a power of <code>2</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,67 @@
|
||||
<p>Design a text editor with a cursor that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Add</strong> text to where the cursor is.</li>
|
||||
<li><strong>Delete</strong> text from where the cursor is (simulating the backspace key).</li>
|
||||
<li><strong>Move</strong> the cursor either left or right.</li>
|
||||
</ul>
|
||||
|
||||
<p>When deleting text, only characters to the left of the cursor will be deleted. The cursor will also remain within the actual text and cannot be moved beyond it. More formally, we have that <code>0 <= cursor.position <= currentText.length</code> always holds.</p>
|
||||
|
||||
<p>Implement the <code>TextEditor</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>TextEditor()</code> Initializes the object with empty text.</li>
|
||||
<li><code>void addText(string text)</code> Appends <code>text</code> to where the cursor is. The cursor ends to the right of <code>text</code>.</li>
|
||||
<li><code>int deleteText(int k)</code> Deletes <code>k</code> characters to the left of the cursor. Returns the number of characters actually deleted.</li>
|
||||
<li><code>string cursorLeft(int k)</code> Moves the cursor to the left <code>k</code> times. Returns the last <code>min(10, len)</code> characters to the left of the cursor, where <code>len</code> is the number of characters to the left of the cursor.</li>
|
||||
<li><code>string cursorRight(int k)</code> Moves the cursor to the right <code>k</code> times. Returns the last <code>min(10, len)</code> characters to the left of the cursor, where <code>len</code> is the number of characters to the left of the cursor.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"]
|
||||
[[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]
|
||||
<strong>Output</strong>
|
||||
[null, null, 4, null, "etpractice", "leet", 4, "", "practi"]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
TextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor)
|
||||
textEditor.addText("leetcode"); // The current text is "leetcode|".
|
||||
textEditor.deleteText(4); // return 4
|
||||
// The current text is "leet|".
|
||||
// 4 characters were deleted.
|
||||
textEditor.addText("practice"); // The current text is "leetpractice|".
|
||||
textEditor.cursorRight(3); // return "etpractice"
|
||||
// The current text is "leetpractice|".
|
||||
// The cursor cannot be moved beyond the actual text and thus did not move.
|
||||
// "etpractice" is the last 10 characters to the left of the cursor.
|
||||
textEditor.cursorLeft(8); // return "leet"
|
||||
// The current text is "leet|practice".
|
||||
// "leet" is the last min(10, 4) = 4 characters to the left of the cursor.
|
||||
textEditor.deleteText(10); // return 4
|
||||
// The current text is "|practice".
|
||||
// Only 4 characters were deleted.
|
||||
textEditor.cursorLeft(2); // return ""
|
||||
// The current text is "|practice".
|
||||
// The cursor cannot be moved beyond the actual text and thus did not move.
|
||||
// "" is the last min(10, 0) = 0 characters to the left of the cursor.
|
||||
textEditor.cursorRight(6); // return "practi"
|
||||
// The current text is "practi|ce".
|
||||
// "practi" is the last min(10, 6) = 6 characters to the left of the cursor.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length, k <= 40</code></li>
|
||||
<li><code>text</code> consists of lowercase English letters.</li>
|
||||
<li>At most <code>2 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>addText</code>, <code>deleteText</code>, <code>cursorLeft</code> and <code>cursorRight</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow-up:</strong> Could you find a solution with time complexity of <code>O(k)</code> per call?</p>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional</strong> road connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>You need to assign each city with an integer value from <code>1</code> to <code>n</code>, where each value can only be used <strong>once</strong>. The <strong>importance</strong> of a road is then defined as the <strong>sum</strong> of the values of the two cities it connects.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum total importance</strong> of all roads possible after assigning the values optimally.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png" style="width: 290px; height: 215px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
|
||||
<strong>Output:</strong> 43
|
||||
<strong>Explanation:</strong> The figure above shows the country and the assigned values of [2,4,5,3,1].
|
||||
- The road (0,1) has an importance of 2 + 4 = 6.
|
||||
- The road (1,2) has an importance of 4 + 5 = 9.
|
||||
- The road (2,3) has an importance of 5 + 3 = 8.
|
||||
- The road (0,2) has an importance of 2 + 5 = 7.
|
||||
- The road (1,3) has an importance of 4 + 3 = 7.
|
||||
- The road (2,4) has an importance of 5 + 1 = 6.
|
||||
The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
|
||||
It can be shown that we cannot obtain a greater total importance than 43.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png" style="width: 281px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, roads = [[0,3],[2,4],[1,3]]
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> The figure above shows the country and the assigned values of [4,3,2,5,1].
|
||||
- The road (0,3) has an importance of 4 + 5 = 9.
|
||||
- The road (2,4) has an importance of 2 + 1 = 3.
|
||||
- The road (1,3) has an importance of 3 + 5 = 8.
|
||||
The total importance of all roads is 9 + 3 + 8 = 20.
|
||||
It can be shown that we cannot obtain a greater total importance than 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= roads.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>roads[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no duplicate roads.</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given two <strong>0-indexed</strong> strings <code>s</code> and <code>target</code>. You can take some letters from <code>s</code> and rearrange them to form new strings.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> number of copies of </em><code>target</code><em> that can be formed by taking letters from </em><code>s</code><em> and rearranging them.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ilovecodingonleetcode", target = "code"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
For the first copy of "code", take the letters at indices 4, 5, 6, and 7.
|
||||
For the second copy of "code", take the letters at indices 17, 18, 19, and 20.
|
||||
The strings that are formed are "ecod" and "code" which can both be rearranged into "code".
|
||||
We can make at most two copies of "code", so we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcba", target = "abc"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
We can make one copy of "abc" by taking the letters at indices 0, 1, and 2.
|
||||
We can make at most one copy of "abc", so we return 1.
|
||||
Note that while there is an extra 'a' and 'b' at indices 3 and 4, we cannot reuse the letter 'c' at index 2, so we cannot make a second copy of "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abbaccaddaeea", target = "aaaaa"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
We can make one copy of "aaaaa" by taking the letters at indices 0, 3, 6, 9, and 12.
|
||||
We can make at most one copy of "aaaaa", so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>1 <= target.length <= 10</code></li>
|
||||
<li><code>s</code> and <code>target</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
47
leetcode/originData/[no content]jump-game-ix.json
Normal file
47
leetcode/originData/[no content]jump-game-ix.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
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2431",
|
||||
"questionFrontendId": "2292",
|
||||
"boundTopicId": null,
|
||||
"title": "Products With Three or More Orders in Two Consecutive Years",
|
||||
"titleSlug": "products-with-three-or-more-orders-in-two-consecutive-years",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 4,
|
||||
"dislikes": 6,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\": {\"Orders\": [\"order_id\", \"product_id\", \"quantity\", \"purchase_date\"]}, \"rows\": {\"Orders\": [[1, 1, 7, \"2020-03-16\"], [2, 1, 4, \"2020-12-02\"], [3, 1, 7, \"2020-05-10\"], [4, 1, 6, \"2021-12-23\"], [5, 1, 5, \"2021-05-21\"], [6, 1, 6, \"2021-10-11\"], [7, 2, 6, \"2022-10-11\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"243\", \"totalSubmission\": \"639\", \"totalAcceptedRaw\": 243, \"totalSubmissionRaw\": 639, \"acRate\": \"38.0%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Orders\": [\"order_id\", \"product_id\", \"quantity\", \"purchase_date\"]}, \"rows\": {\"Orders\": [[1, 1, 7, \"2020-03-16\"], [2, 1, 4, \"2020-12-02\"], [3, 1, 7, \"2020-05-10\"], [4, 1, 6, \"2021-12-23\"], [5, 1, 5, \"2021-05-21\"], [6, 1, 6, \"2021-10-11\"], [7, 2, 6, \"2022-10-11\"]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Orders (order_id int, product_id int, quantity int, purchase_date date)\"\n ],\n \"mssql\": [\n \"Create table Orders (order_id int, product_id int, quantity int, purchase_date date)\"\n ],\n \"oraclesql\": [\n \"Create table Orders (order_id int, product_id int, quantity int, purchase_date date)\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Orders (order_id int, product_id int, quantity int, purchase_date date)",
|
||||
"Truncate table Orders",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('1', '1', '7', '2020-03-16')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('2', '1', '4', '2020-12-02')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('3', '1', '7', '2020-05-10')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('4', '1', '6', '2021-12-23')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('5', '1', '5', '2021-05-21')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('6', '1', '6', '2021-10-11')",
|
||||
"insert into Orders (order_id, product_id, quantity, purchase_date) values ('7', '2', '6', '2022-10-11')"
|
||||
],
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
162
leetcode/originData/apply-discount-to-prices.json
Normal file
162
leetcode/originData/apply-discount-to-prices.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode/originData/booking-concert-tickets-in-groups.json
Normal file
182
leetcode/originData/booking-concert-tickets-in-groups.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
194
leetcode/originData/design-a-text-editor.json
Normal file
194
leetcode/originData/design-a-text-editor.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/maximum-total-importance-of-roads.json
Normal file
180
leetcode/originData/maximum-total-importance-of-roads.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode/originData/min-max-game.json
Normal file
168
leetcode/originData/min-max-game.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
174
leetcode/originData/replace-elements-in-an-array.json
Normal file
174
leetcode/originData/replace-elements-in-an-array.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/sender-with-largest-word-count.json
Normal file
180
leetcode/originData/sender-with-largest-word-count.json
Normal file
File diff suppressed because one or more lines are too long
181
leetcode/originData/steps-to-make-array-non-decreasing.json
Normal file
181
leetcode/originData/steps-to-make-array-non-decreasing.json
Normal file
File diff suppressed because one or more lines are too long
47
leetcode/problem/apply-discount-to-prices.html
Normal file
47
leetcode/problem/apply-discount-to-prices.html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>A <strong>sentence</strong> is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign <code>'$'</code>. A word represents a <strong>price</strong> if it is a sequence of digits preceded by a dollar sign.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"$100"</code>, <code>"$23"</code>, and <code>"$6"</code> represent prices while <code>"100"</code>, <code>"$"</code>, and <code>"$1e5"</code> do not.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given a string <code>sentence</code> representing a sentence and an integer <code>discount</code>. For each word representing a price, apply a discount of <code>discount%</code> on the price and <strong>update</strong> the word in the sentence. All updated prices should be represented with <strong>exactly two</strong> decimal places.</p>
|
||||
|
||||
<p>Return <em>a string representing the modified sentence</em>.</p>
|
||||
|
||||
<p>Note that all prices will contain <strong>at most</strong> <code>10</code> digits.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50
|
||||
<strong>Output:</strong> "there are $0.50 $1.00 and 5$ candies in the shop"
|
||||
<strong>Explanation:</strong>
|
||||
The words which represent prices are "$1" and "$2".
|
||||
- A 50% discount on "$1" yields "$0.50", so "$1" is replaced by "$0.50".
|
||||
- A 50% discount on "$2" yields "$1". Since we need to have exactly 2 decimal places after a price, we replace "$2" with "$1.00".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100
|
||||
<strong>Output:</strong> "1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"
|
||||
<strong>Explanation:</strong>
|
||||
Applying a 100% discount on any price will result in 0.
|
||||
The words representing prices are "$3", "$5", "$6", and "$9".
|
||||
Each of them is replaced by "$0.00".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sentence.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>sentence</code> consists of lowercase English letters, digits, <code>' '</code>, and <code>'$'</code>.</li>
|
||||
<li><code>sentence</code> does not have leading or trailing spaces.</li>
|
||||
<li>All words in <code>sentence</code> are separated by a single space.</li>
|
||||
<li>All prices will be <strong>positive</strong> integers without leading zeros.</li>
|
||||
<li>All prices will have <strong>at most</strong> <code>10</code> digits.</li>
|
||||
<li><code>0 <= discount <= 100</code></li>
|
||||
</ul>
|
54
leetcode/problem/booking-concert-tickets-in-groups.html
Normal file
54
leetcode/problem/booking-concert-tickets-in-groups.html
Normal file
@ -0,0 +1,54 @@
|
||||
<p>A concert hall has <code>n</code> rows numbered from <code>0</code> to <code>n - 1</code>, each with <code>m</code> seats, numbered from <code>0</code> to <code>m - 1</code>. You need to design a ticketing system that can allocate seats in the following cases:</p>
|
||||
|
||||
<ul>
|
||||
<li>If a group of <code>k</code> spectators can sit <strong>together</strong> in a row.</li>
|
||||
<li>If <strong>every</strong> member of a group of <code>k</code> spectators can get a seat. They may or <strong>may not</strong> sit together.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that the spectators are very picky. Hence:</p>
|
||||
|
||||
<ul>
|
||||
<li>They will book seats only if each member of their group can get a seat with row number <strong>less than or equal</strong> to <code>maxRow</code>. <code>maxRow</code> can <strong>vary</strong> from group to group.</li>
|
||||
<li>In case there are multiple rows to choose from, the row with the <strong>smallest</strong> number is chosen. If there are multiple seats to choose in the same row, the seat with the <strong>smallest</strong> number is chosen.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>BookMyShow</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>BookMyShow(int n, int m)</code> Initializes the object with <code>n</code> as number of rows and <code>m</code> as number of seats per row.</li>
|
||||
<li><code>int[] gather(int k, int maxRow)</code> Returns an array of length <code>2</code> denoting the row and seat number (respectively) of the <strong>first seat</strong> being allocated to the <code>k</code> members of the group, who must sit <strong>together</strong>. In other words, it returns the smallest possible <code>r</code> and <code>c</code> such that all <code>[c, c + k - 1]</code> seats are valid and empty in row <code>r</code>, and <code>r <= maxRow</code>. Returns <code>[]</code> in case it is <strong>not possible</strong> to allocate seats to the group.</li>
|
||||
<li><code>boolean scatter(int k, int maxRow)</code> Returns <code>true</code> if all <code>k</code> members of the group can be allocated seats in rows <code>0</code> to <code>maxRow</code>, who may or <strong>may not</strong> sit together. If the seats can be allocated, it allocates <code>k</code> seats to the group with the <strong>smallest</strong> row numbers, and the smallest possible seat numbers in each row. Otherwise, returns <code>false</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["BookMyShow", "gather", "gather", "scatter", "scatter"]
|
||||
[[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
|
||||
<strong>Output</strong>
|
||||
[null, [0, 0], [], true, false]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each
|
||||
bms.gather(4, 0); // return [0, 0]
|
||||
// The group books seats [0, 3] of row 0.
|
||||
bms.gather(2, 0); // return []
|
||||
// There is only 1 seat left in row 0,
|
||||
// so it is not possible to book 2 consecutive seats.
|
||||
bms.scatter(5, 1); // return True
|
||||
// The group books seat 4 of row 0 and seats [0, 3] of row 1.
|
||||
bms.scatter(5, 1); // return False
|
||||
// There is only one seat left in the hall.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m, k <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= maxRow <= n - 1</code></li>
|
||||
<li>At most <code>5 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>gather</code> and <code>scatter</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>num</code> of length <code>n</code> consisting of digits.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if for <strong>every</strong> index </em><code>i</code><em> in the range </em><code>0 <= i < n</code><em>, the digit </em><code>i</code><em> occurs </em><code>num[i]</code><em> times in </em><code>num</code><em>, otherwise return </em><code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "1210"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
num[0] = '1'. The digit 0 occurs once in num.
|
||||
num[1] = '2'. The digit 1 occurs twice in num.
|
||||
num[2] = '1'. The digit 2 occurs once in num.
|
||||
num[3] = '0'. The digit 3 occurs zero times in num.
|
||||
The condition holds true for every index in "1210", so return true.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "030"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.
|
||||
num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.
|
||||
num[2] = '0'. The digit 2 occurs zero times in num.
|
||||
The indices 0 and 1 both violate the condition, so return false.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == num.length</code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>num</code> consists of digits.</li>
|
||||
</ul>
|
67
leetcode/problem/design-a-text-editor.html
Normal file
67
leetcode/problem/design-a-text-editor.html
Normal file
@ -0,0 +1,67 @@
|
||||
<p>Design a text editor with a cursor that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Add</strong> text to where the cursor is.</li>
|
||||
<li><strong>Delete</strong> text from where the cursor is (simulating the backspace key).</li>
|
||||
<li><strong>Move</strong> the cursor either left or right.</li>
|
||||
</ul>
|
||||
|
||||
<p>When deleting text, only characters to the left of the cursor will be deleted. The cursor will also remain within the actual text and cannot be moved beyond it. More formally, we have that <code>0 <= cursor.position <= currentText.length</code> always holds.</p>
|
||||
|
||||
<p>Implement the <code>TextEditor</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>TextEditor()</code> Initializes the object with empty text.</li>
|
||||
<li><code>void addText(string text)</code> Appends <code>text</code> to where the cursor is. The cursor ends to the right of <code>text</code>.</li>
|
||||
<li><code>int deleteText(int k)</code> Deletes <code>k</code> characters to the left of the cursor. Returns the number of characters actually deleted.</li>
|
||||
<li><code>string cursorLeft(int k)</code> Moves the cursor to the left <code>k</code> times. Returns the last <code>min(10, len)</code> characters to the left of the cursor, where <code>len</code> is the number of characters to the left of the cursor.</li>
|
||||
<li><code>string cursorRight(int k)</code> Moves the cursor to the right <code>k</code> times. Returns the last <code>min(10, len)</code> characters to the left of the cursor, where <code>len</code> is the number of characters to the left of the cursor.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"]
|
||||
[[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]
|
||||
<strong>Output</strong>
|
||||
[null, null, 4, null, "etpractice", "leet", 4, "", "practi"]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
TextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor)
|
||||
textEditor.addText("leetcode"); // The current text is "leetcode|".
|
||||
textEditor.deleteText(4); // return 4
|
||||
// The current text is "leet|".
|
||||
// 4 characters were deleted.
|
||||
textEditor.addText("practice"); // The current text is "leetpractice|".
|
||||
textEditor.cursorRight(3); // return "etpractice"
|
||||
// The current text is "leetpractice|".
|
||||
// The cursor cannot be moved beyond the actual text and thus did not move.
|
||||
// "etpractice" is the last 10 characters to the left of the cursor.
|
||||
textEditor.cursorLeft(8); // return "leet"
|
||||
// The current text is "leet|practice".
|
||||
// "leet" is the last min(10, 4) = 4 characters to the left of the cursor.
|
||||
textEditor.deleteText(10); // return 4
|
||||
// The current text is "|practice".
|
||||
// Only 4 characters were deleted.
|
||||
textEditor.cursorLeft(2); // return ""
|
||||
// The current text is "|practice".
|
||||
// The cursor cannot be moved beyond the actual text and thus did not move.
|
||||
// "" is the last min(10, 0) = 0 characters to the left of the cursor.
|
||||
textEditor.cursorRight(6); // return "practi"
|
||||
// The current text is "practi|ce".
|
||||
// "practi" is the last min(10, 6) = 6 characters to the left of the cursor.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length, k <= 40</code></li>
|
||||
<li><code>text</code> consists of lowercase English letters.</li>
|
||||
<li>At most <code>2 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>addText</code>, <code>deleteText</code>, <code>cursorLeft</code> and <code>cursorRight</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow-up:</strong> Could you find a solution with time complexity of <code>O(k)</code> per call?</p>
|
49
leetcode/problem/maximum-total-importance-of-roads.html
Normal file
49
leetcode/problem/maximum-total-importance-of-roads.html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional</strong> road connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>You need to assign each city with an integer value from <code>1</code> to <code>n</code>, where each value can only be used <strong>once</strong>. The <strong>importance</strong> of a road is then defined as the <strong>sum</strong> of the values of the two cities it connects.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum total importance</strong> of all roads possible after assigning the values optimally.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex1drawio.png" style="width: 290px; height: 215px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
|
||||
<strong>Output:</strong> 43
|
||||
<strong>Explanation:</strong> The figure above shows the country and the assigned values of [2,4,5,3,1].
|
||||
- The road (0,1) has an importance of 2 + 4 = 6.
|
||||
- The road (1,2) has an importance of 4 + 5 = 9.
|
||||
- The road (2,3) has an importance of 5 + 3 = 8.
|
||||
- The road (0,2) has an importance of 2 + 5 = 7.
|
||||
- The road (1,3) has an importance of 4 + 3 = 7.
|
||||
- The road (2,4) has an importance of 5 + 1 = 6.
|
||||
The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43.
|
||||
It can be shown that we cannot obtain a greater total importance than 43.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/07/ex2drawio.png" style="width: 281px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, roads = [[0,3],[2,4],[1,3]]
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> The figure above shows the country and the assigned values of [4,3,2,5,1].
|
||||
- The road (0,3) has an importance of 4 + 5 = 9.
|
||||
- The road (2,4) has an importance of 2 + 1 = 3.
|
||||
- The road (1,3) has an importance of 3 + 5 = 8.
|
||||
The total importance of all roads is 9 + 3 + 8 = 20.
|
||||
It can be shown that we cannot obtain a greater total importance than 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= roads.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>roads[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no duplicate roads.</li>
|
||||
</ul>
|
43
leetcode/problem/min-max-game.html
Normal file
43
leetcode/problem/min-max-game.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> whose length is a power of <code>2</code>.</p>
|
||||
|
||||
<p>Apply the following algorithm on <code>nums</code>:</p>
|
||||
|
||||
<ol>
|
||||
<li>Let <code>n</code> be the length of <code>nums</code>. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n / 2</code>.</li>
|
||||
<li>For every <strong>even</strong> index <code>i</code> where <code>0 <= i < n / 2</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>min(nums[2 * i], nums[2 * i + 1])</code>.</li>
|
||||
<li>For every <strong>odd</strong> index <code>i</code> where <code>0 <= i < n / 2</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>max(nums[2 * i], nums[2 * i + 1])</code>.</li>
|
||||
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
|
||||
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the last number that remains in </em><code>nums</code><em> after applying the algorithm.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/13/example1drawio-1.png" style="width: 500px; height: 240px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,4,8,2,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The following arrays are the results of applying the algorithm repeatedly.
|
||||
First: nums = [1,5,4,2]
|
||||
Second: nums = [1,4]
|
||||
Third: nums = [1]
|
||||
1 is the last remaining number, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 is already the last remaining number, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1024</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>nums.length</code> is a power of <code>2</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code>. Each cell has one of two values:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> represents an <strong>empty</strong> cell,</li>
|
||||
<li><code>1</code> represents an <strong>obstacle</strong> that may be removed.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can move up, down, left, or right from and to an empty cell.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of <strong>obstacles</strong> to <strong>remove</strong> so you can move from the upper left corner </em><code>(0, 0)</code><em> to the lower right corner </em><code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio-1.png" style="width: 605px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,1],[1,1,0],[1,1,0]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).
|
||||
It can be shown that we need to remove at least 2 obstacles, so we return 2.
|
||||
Note that there may be other ways to remove 2 obstacles to create a path.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/06/example1drawio.png" style="width: 405px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> <strong>or</strong> <code>1</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You may partition <code>nums</code> into one or more <strong>subsequences</strong> such that each element in <code>nums</code> appears in <strong>exactly</strong> one of the subsequences.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum </strong>number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is <strong>at most</strong> </em><code>k</code><em>.</em></p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,6,1,2,5], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can partition nums into the two subsequences [3,1,2] and [6,5].
|
||||
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
|
||||
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
|
||||
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can partition nums into the two subsequences [1,2] and [3].
|
||||
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
|
||||
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
|
||||
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,4,5], k = 0
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
We can partition nums into the three subsequences [2,2], [4], and [5].
|
||||
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
|
||||
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
|
||||
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
|
||||
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given two <strong>0-indexed</strong> strings <code>s</code> and <code>target</code>. You can take some letters from <code>s</code> and rearrange them to form new strings.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> number of copies of </em><code>target</code><em> that can be formed by taking letters from </em><code>s</code><em> and rearranging them.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ilovecodingonleetcode", target = "code"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
For the first copy of "code", take the letters at indices 4, 5, 6, and 7.
|
||||
For the second copy of "code", take the letters at indices 17, 18, 19, and 20.
|
||||
The strings that are formed are "ecod" and "code" which can both be rearranged into "code".
|
||||
We can make at most two copies of "code", so we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcba", target = "abc"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
We can make one copy of "abc" by taking the letters at indices 0, 1, and 2.
|
||||
We can make at most one copy of "abc", so we return 1.
|
||||
Note that while there is an extra 'a' and 'b' at indices 3 and 4, we cannot reuse the letter 'c' at index 2, so we cannot make a second copy of "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abbaccaddaeea", target = "aaaaa"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
We can make one copy of "aaaaa" by taking the letters at indices 0, 3, 6, 9, and 12.
|
||||
We can make at most one copy of "aaaaa", so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>1 <= target.length <= 10</code></li>
|
||||
<li><code>s</code> and <code>target</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
49
leetcode/problem/replace-elements-in-an-array.html
Normal file
49
leetcode/problem/replace-elements-in-an-array.html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> that consists of <code>n</code> <strong>distinct</strong> positive integers. Apply <code>m</code> operations to this array, where in the <code>i<sup>th</sup></code> operation you replace the number <code>operations[i][0]</code> with <code>operations[i][1]</code>.</p>
|
||||
|
||||
<p>It is guaranteed that in the <code>i<sup>th</sup></code> operation:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>operations[i][0]</code> <strong>exists</strong> in <code>nums</code>.</li>
|
||||
<li><code>operations[i][1]</code> does <strong>not</strong> exist in <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array obtained after applying all the operations</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
|
||||
<strong>Output:</strong> [3,2,7,1]
|
||||
<strong>Explanation:</strong> We perform the following operations on nums:
|
||||
- Replace the number 1 with 3. nums becomes [<u><strong>3</strong></u>,2,4,6].
|
||||
- Replace the number 4 with 7. nums becomes [3,2,<u><strong>7</strong></u>,6].
|
||||
- Replace the number 6 with 1. nums becomes [3,2,7,<u><strong>1</strong></u>].
|
||||
We return the final array [3,2,7,1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2], operations = [[1,3],[2,1],[3,2]]
|
||||
<strong>Output:</strong> [2,1]
|
||||
<strong>Explanation:</strong> We perform the following operations to nums:
|
||||
- Replace the number 1 with 3. nums becomes [<u><strong>3</strong></u>,2].
|
||||
- Replace the number 2 with 1. nums becomes [3,<u><strong>1</strong></u>].
|
||||
- Replace the number 3 with 2. nums becomes [<u><strong>2</strong></u>,1].
|
||||
We return the array [2,1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == operations.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li>All the values of <code>nums</code> are <strong>distinct</strong>.</li>
|
||||
<li><code>operations[i].length == 2</code></li>
|
||||
<li><code>1 <= nums[i], operations[i][0], operations[i][1] <= 10<sup>6</sup></code></li>
|
||||
<li><code>operations[i][0]</code> will exist in <code>nums</code> when applying the <code>i<sup>th</sup></code> operation.</li>
|
||||
<li><code>operations[i][1]</code> will not exist in <code>nums</code> when applying the <code>i<sup>th</sup></code> operation.</li>
|
||||
</ul>
|
47
leetcode/problem/sender-with-largest-word-count.html
Normal file
47
leetcode/problem/sender-with-largest-word-count.html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>You have a chat log of <code>n</code> messages. You are given two string arrays <code>messages</code> and <code>senders</code> where <code>messages[i]</code> is a <strong>message</strong> sent by <code>senders[i]</code>.</p>
|
||||
|
||||
<p>A <strong>message</strong> is list of <strong>words</strong> that are separated by a single space with no leading or trailing spaces. The <strong>word count</strong> of a sender is the total number of <strong>words</strong> sent by the sender. Note that a sender may send more than one message.</p>
|
||||
|
||||
<p>Return <em>the sender with the <strong>largest</strong> word count</em>. If there is more than one sender with the largest word count, return <em>the one with the <strong>lexicographically largest</strong> name</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Uppercase letters come before lowercase letters in lexicographical order.</li>
|
||||
<li><code>"Alice"</code> and <code>"alice"</code> are distinct.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
|
||||
<strong>Output:</strong> "Alice"
|
||||
<strong>Explanation:</strong> Alice sends a total of 2 + 3 = 5 words.
|
||||
userTwo sends a total of 2 words.
|
||||
userThree sends a total of 3 words.
|
||||
Since Alice has the largest word count, we return "Alice".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
|
||||
<strong>Output:</strong> "Charlie"
|
||||
<strong>Explanation:</strong> Bob sends a total of 5 words.
|
||||
Charlie sends a total of 5 words.
|
||||
Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == messages.length == senders.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= messages[i].length <= 100</code></li>
|
||||
<li><code>1 <= senders[i].length <= 10</code></li>
|
||||
<li><code>messages[i]</code> consists of uppercase and lowercase English letters and <code>' '</code>.</li>
|
||||
<li>All the words in <code>messages[i]</code> are separated by <strong>a single space</strong>.</li>
|
||||
<li><code>messages[i]</code> does not have leading or trailing spaces.</li>
|
||||
<li><code>senders[i]</code> consists of uppercase and lowercase English letters only.</li>
|
||||
</ul>
|
32
leetcode/problem/steps-to-make-array-non-decreasing.html
Normal file
32
leetcode/problem/steps-to-make-array-non-decreasing.html
Normal file
@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one step, <strong>remove</strong> all elements <code>nums[i]</code> where <code>nums[i - 1] > nums[i]</code> for all <code>0 < i < nums.length</code>.</p>
|
||||
|
||||
<p>Return <em>the number of steps performed until </em><code>nums</code><em> becomes a <strong>non-decreasing</strong> array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,3,4,4,7,3,6,11,8,5,11]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following are the steps performed:
|
||||
- Step 1: [5,<strong><u>3</u></strong>,4,4,7,<u><strong>3</strong></u>,6,11,<u><strong>8</strong></u>,<u><strong>5</strong></u>,11] becomes [5,4,4,7,6,11,11]
|
||||
- Step 2: [5,<u><strong>4</strong></u>,4,7,<u><strong>6</strong></u>,11,11] becomes [5,4,7,11,11]
|
||||
- Step 3: [5,<u><strong>4</strong></u>,7,11,11] becomes [5,7,11,11]
|
||||
[5,7,11,11] is a non-decreasing array. Therefore, we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,5,7,7,13]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> nums is already a non-decreasing array. Therefore, we return 0.
|
||||
</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>9</sup></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user