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
e6b8964ee2
commit
321b55e52f
92
README.md
92
README.md
@ -1,46 +1,46 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.07.23**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
### 仓库介绍
|
||||
|
||||
使用 Python 脚本分批将力扣(`leetcode.com` 和 `leetcode-cn.com`)上面的题目保存下来,方便没有网的时候进行学习。(仅包含可以在网页上直接打开的公开题目,不包含 VIP 题目)
|
||||
|
||||
|
||||
|
||||
### 仓库目录结构
|
||||
|
||||
国外版力扣题库,在仓库 `leetcode/problem` 文件夹下;国内版力扣题库,在仓库 `leetcode-cn/problem (Chinese)` 和 `leetcode-cn/problem (English)` 文件夹下。(部分题目只有中文版,无对应英文版)。
|
||||
|
||||
|
||||
|
||||
### 特别注意!
|
||||
|
||||
#### 版权相关
|
||||
|
||||
**所有版权都为 LeetCode (及力扣中国) 官方所有,此处仅供学习使用,不要他用。也请大家不要滥用,不要侵犯力扣平台的合法权益。**
|
||||
|
||||
**感谢 LeetCode 平台为我们提供大量的算法题目进行练习与提升。如果大家经济条件允许,请大家多多支持力扣,例如冲冲会员等。**
|
||||
|
||||
力扣题库的权益归属力扣,使用力扣题库,需要遵循力扣使用条例,若您不同意此条例,请立即关闭当前网页,不要继续使用本题库。
|
||||
|
||||
力扣(LeetCode)• 使用条例: https://leetcode-cn.com/terms-c/
|
||||
|
||||
LeetCode Terms of Service: https://leetcode.com/terms/
|
||||
|
||||
|
||||
|
||||
#### 脚本原作者
|
||||
|
||||
Python脚本是在网上教程的基础上进行二改得到的,原版地址:https://blog.csdn.net/weixin_37267014/article/details/81429057
|
||||
|
||||
|
||||
|
||||
#### 其他
|
||||
|
||||
由于脚本运行时会向力扣网站发出大量请求,所以请大家不要随便尝试此脚本,以免影响力扣网站正常运行。
|
||||
|
||||
因为使用此脚本所造成的一系列问题,责任由您自己承担,作者不承担相应责任。
|
||||
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.07.29**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
### 仓库介绍
|
||||
|
||||
使用 Python 脚本分批将力扣(`leetcode.com` 和 `leetcode-cn.com`)上面的题目保存下来,方便没有网的时候进行学习。(仅包含可以在网页上直接打开的公开题目,不包含 VIP 题目)
|
||||
|
||||
|
||||
|
||||
### 仓库目录结构
|
||||
|
||||
国外版力扣题库,在仓库 `leetcode/problem` 文件夹下;国内版力扣题库,在仓库 `leetcode-cn/problem (Chinese)` 和 `leetcode-cn/problem (English)` 文件夹下。(部分题目只有中文版,无对应英文版)。
|
||||
|
||||
|
||||
|
||||
### 特别注意!
|
||||
|
||||
#### 版权相关
|
||||
|
||||
**所有版权都为 LeetCode (及力扣中国) 官方所有,此处仅供学习使用,不要他用。也请大家不要滥用,不要侵犯力扣平台的合法权益。**
|
||||
|
||||
**感谢 LeetCode 平台为我们提供大量的算法题目进行练习与提升。如果大家经济条件允许,请大家多多支持力扣,例如冲冲会员等。**
|
||||
|
||||
力扣题库的权益归属力扣,使用力扣题库,需要遵循力扣使用条例,若您不同意此条例,请立即关闭当前网页,不要继续使用本题库。
|
||||
|
||||
力扣(LeetCode)• 使用条例: https://leetcode-cn.com/terms-c/
|
||||
|
||||
LeetCode Terms of Service: https://leetcode.com/terms/
|
||||
|
||||
|
||||
|
||||
#### 脚本原作者
|
||||
|
||||
Python脚本是在网上教程的基础上进行二改得到的,原版地址:https://blog.csdn.net/weixin_37267014/article/details/81429057
|
||||
|
||||
|
||||
|
||||
#### 其他
|
||||
|
||||
由于脚本运行时会向力扣网站发出大量请求,所以请大家不要随便尝试此脚本,以免影响力扣网站正常运行。
|
||||
|
||||
因为使用此脚本所造成的一系列问题,责任由您自己承担,作者不承担相应责任。
|
||||
|
||||
|
114138
leetcode-cn/origin-data.json
114138
leetcode-cn/origin-data.json
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -0,0 +1,56 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2495",
|
||||
"questionFrontendId": "2356",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 1706030,
|
||||
"title": "Number of Unique Subjects Taught by Each Teacher",
|
||||
"titleSlug": "number-of-unique-subjects-taught-by-each-teacher",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Easy",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"13\", \"totalSubmission\": \"13\", \"totalAcceptedRaw\": 13, \"totalSubmissionRaw\": 13, \"acRate\": \"100.0%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Teacher\":[\"teacher_id\",\"subject_id\",\"dept_id\"]},\"rows\":{\"Teacher\":[[1,2,3],[1,2,4],[1,3,3],[2,1,1],[2,2,1],[2,3,1],[2,4,1]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)\"\n ],\n \"mssql\": [\n \"Create table Teacher (teacher_id int, subject_id int, dept_id int)\"\n ],\n \"oraclesql\": [\n \"Create table Teacher (teacher_id int, subject_id int, dept_id int)\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)",
|
||||
"Truncate table Teacher",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '3')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '4')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '3', '3')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '1', '1')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '2', '1')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '3', '1')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '4', '1')"
|
||||
],
|
||||
"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\":{\"Teacher\":[\"teacher_id\",\"subject_id\",\"dept_id\"]},\"rows\":{\"Teacher\":[[1,2,3],[1,2,4],[1,3,3],[2,1,1],[2,2,1],[2,3,1],[2,4,1]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
176
leetcode-cn/originData/best-poker-hand.json
Normal file
176
leetcode-cn/originData/best-poker-hand.json
Normal file
File diff suppressed because one or more lines are too long
184
leetcode-cn/originData/design-a-food-rating-system.json
Normal file
184
leetcode-cn/originData/design-a-food-rating-system.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/design-a-number-container-system.json
Normal file
183
leetcode-cn/originData/design-a-number-container-system.json
Normal file
File diff suppressed because one or more lines are too long
184
leetcode-cn/originData/equal-row-and-column-pairs.json
Normal file
184
leetcode-cn/originData/equal-row-and-column-pairs.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/first-letter-to-appear-twice.json
Normal file
177
leetcode-cn/originData/first-letter-to-appear-twice.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/number-of-excellent-pairs.json
Normal file
183
leetcode-cn/originData/number-of-excellent-pairs.json
Normal file
File diff suppressed because one or more lines are too long
171
leetcode-cn/originData/number-of-zero-filled-subarrays.json
Normal file
171
leetcode-cn/originData/number-of-zero-filled-subarrays.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,48 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>rolls</code> 和一个整数 <code>k</code> 。你扔一个 <code>k</code> 面的骰子 <code>n</code> 次,骰子的每个面分别是 <code>1</code> 到 <code>k</code> ,其中第 <code>i</code> 次扔得到的数字是 <code>rolls[i]</code> 。</p>
|
||||
|
||||
<p>请你返回 <strong>无法</strong> 从 <code>rolls</code> 中得到的 <strong>最短</strong> 骰子子序列的长度。</p>
|
||||
|
||||
<p>扔一个 <code>k</code> 面的骰子 <code>len</code> 次得到的是一个长度为 <code>len</code> 的 <strong>骰子子序列</strong> 。</p>
|
||||
|
||||
<p><strong>注意</strong> ,子序列只需要保持在原数组中的顺序,不需要连续。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>rolls = [4,2,1,2,3,3,2,4,1], k = 4
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>所有长度为 1 的骰子子序列 [1] ,[2] ,[3] ,[4] 都可以从原数组中得到。
|
||||
所有长度为 2 的骰子子序列 [1, 1] ,[1, 2] ,... ,[4, 4] 都可以从原数组中得到。
|
||||
子序列 [1, 4, 2] 无法从原数组中得到,所以我们返回 3 。
|
||||
还有别的子序列也无法从原数组中得到。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>rolls = [1,1,2,2], k = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>所有长度为 1 的子序列 [1] ,[2] 都可以从原数组中得到。
|
||||
子序列 [2, 1] 无法从原数组中得到,所以我们返回 2 。
|
||||
还有别的子序列也无法从原数组中得到,但 [2, 1] 是最短的子序列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>rolls = [1,1,3,2,2,2,3,3], k = 4
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>子序列 [4] 无法从原数组中得到,所以我们返回 1 。
|
||||
还有别的子序列也无法从原数组中得到,但 [4] 是最短的子序列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == rolls.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= rolls[i] <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的正整数数组 <code>nums</code> 和一个正整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果满足下述条件,则数对 <code>(num1, num2)</code> 是 <strong>优质数对</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1</code> 和 <code>num2</code> <strong>都</strong> 在数组 <code>nums</code> 中存在。</li>
|
||||
<li><code>num1 OR num2</code> 和 <code>num1 AND num2</code> 的二进制表示中值为 <strong>1</strong> 的位数之和大于等于 <code>k</code> ,其中 <code>OR</code> 是按位 <strong>或</strong> 操作,而 <code>AND</code> 是按位 <strong>与</strong> 操作。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <strong>不同</strong> 优质数对的数目。</p>
|
||||
|
||||
<p>如果 <code>a != c</code> 或者 <code>b != d</code> ,则认为 <code>(a, b)</code> 和 <code>(c, d)</code> 是不同的两个数对。例如,<code>(1, 2)</code> 和 <code>(2, 1)</code> 不同。</p>
|
||||
|
||||
<p><strong>注意:</strong>如果 <code>num1</code> 在数组中至少出现 <strong>一次</strong> ,则满足 <code>num1 == num2</code> 的数对 <code>(num1, num2)</code> 也可以是优质数对。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,1], k = 3
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>有如下几个优质数对:
|
||||
- (3, 3):(3 AND 3) 和 (3 OR 3) 的二进制表示都等于 (11) 。值为 1 的位数和等于 2 + 2 = 4 ,大于等于 k = 3 。
|
||||
- (2, 3) 和 (3, 2): (2 AND 3) 的二进制表示等于 (10) ,(2 OR 3) 的二进制表示等于 (11) 。值为 1 的位数和等于 1 + 2 = 3 。
|
||||
- (1, 3) 和 (3, 1): (1 AND 3) 的二进制表示等于 (01) ,(1 OR 3) 的二进制表示等于 (11) 。值为 1 的位数和等于 1 + 2 = 3 。
|
||||
所以优质数对的数目是 5 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,1,1], k = 10
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>该数组中不存在优质数对。
|
||||
</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>
|
||||
<li><code>1 <= k <= 60</code></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,返回全部为 <code>0</code> 的 <strong>子数组</strong> 数目。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是一个数组中一段连续非空元素组成的序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,3,0,0,2,0,0,4]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>
|
||||
子数组 [0] 出现了 4 次。
|
||||
子数组 [0,0] 出现了 2 次。
|
||||
不存在长度大于 2 的全 0 子数组,所以我们返回 6 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [0,0,0,2,0,0]
|
||||
<b>输出:</b>9
|
||||
<strong>解释:
|
||||
</strong>子数组 [0] 出现了 5 次。
|
||||
子数组 [0,0] 出现了 3 次。
|
||||
子数组 [0,0,0] 出现了 1 次。
|
||||
不存在长度大于 3 的全 0 子数组,所以我们返回 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [2,10,2019]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>没有全 0 子数组,所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
50
leetcode-cn/problem (Chinese)/最好的扑克手牌 [best-poker-hand].html
Normal file
50
leetcode-cn/problem (Chinese)/最好的扑克手牌 [best-poker-hand].html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>给你一个整数数组 <code>ranks</code> 和一个字符数组 <code>suit</code> 。你有 <code>5</code> 张扑克牌,第 <code>i</code> 张牌大小为 <code>ranks[i]</code> ,花色为 <code>suits[i]</code> 。</p>
|
||||
|
||||
<p>下述是从好到坏你可能持有的 <strong>手牌类型 </strong>:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>"Flush"</code>:同花,五张相同花色的扑克牌。</li>
|
||||
<li><code>"Three of a Kind"</code>:三条,有 3 张大小相同的扑克牌。</li>
|
||||
<li><code>"Pair"</code>:对子,两张大小一样的扑克牌。</li>
|
||||
<li><code>"High Card"</code>:高牌,五张大小互不相同的扑克牌。</li>
|
||||
</ol>
|
||||
|
||||
<p>请你返回一个字符串,表示给定的 5 张牌中,你能组成的 <strong>最好手牌类型</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong>返回的字符串 <strong>大小写</strong> 需与题目描述相同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
|
||||
<b>输出:</b>"Flush"
|
||||
<b>解释:</b>5 张扑克牌的花色相同,所以返回 "Flush" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
|
||||
<b>输出:</b>"Three of a Kind"
|
||||
<b>解释:</b>第一、二和四张牌组成三张相同大小的扑克牌,所以得到 "Three of a Kind" 。
|
||||
注意我们也可以得到 "Pair" ,但是 "Three of a Kind" 是更好的手牌类型。
|
||||
有其他的 3 张牌也可以组成 "Three of a Kind" 手牌类型。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
|
||||
<b>输出:</b>"Pair"
|
||||
<b>解释:</b>第一和第二张牌大小相同,所以得到 "Pair" 。
|
||||
我们无法得到 "Flush" 或者 "Three of a Kind" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>ranks.length == suits.length == 5</code></li>
|
||||
<li><code>1 <= ranks[i] <= 13</code></li>
|
||||
<li><code>'a' <= suits[i] <= 'd'</code></li>
|
||||
<li>任意两张扑克牌不会同时有相同的大小和花色。</li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>n x n</code> 的整数矩阵 <code>grid</code> ,返回满足 <code>R<sub>i</sub></code><em> </em>行和<em> </em><code>C<sub>j</sub></code><em> </em>列相等的行列对<em> </em><code>(R<sub>i</sub>, C<sub>j</sub>)</code><em> </em>的数目<em>。</em></p>
|
||||
|
||||
<p>如果行和列以相同的顺序包含相同的元素(即相等的数组),则认为二者是相等的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg" style="width: 150px; height: 153px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[3,2,1],[1,7,6],[2,7,7]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>存在一对相等行列对:
|
||||
- (第 2 行,第 1 列):[2,7,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg" style="width: 200px; height: 209px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>存在三对相等行列对:
|
||||
- (第 0 行,第 0 列):[3,1,2,2]
|
||||
- (第 2 行, 第 2 列):[2,4,2,2]
|
||||
- (第 3 行, 第 2 列):[2,4,2,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>给你一个由小写英文字母组成的字符串 <code>s</code> ,请你找出并返回第一个出现 <strong>两次</strong> 的字母。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>a</code> 的 <strong>第二次</strong> 出现比 <code>b</code> 的 <strong>第二次</strong> 出现在字符串中的位置更靠前,则认为字母 <code>a</code> 在字母 <code>b</code> 之前出现两次。</li>
|
||||
<li><code>s</code> 包含至少一个出现两次的字母。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abccbaacz"
|
||||
<strong>输出:</strong>"c"
|
||||
<strong>解释:</strong>
|
||||
字母 'a' 在下标 0 、5 和 6 处出现。
|
||||
字母 'b' 在下标 1 和 4 处出现。
|
||||
字母 'c' 在下标 2 、3 和 7 处出现。
|
||||
字母 'z' 在下标 8 处出现。
|
||||
字母 'c' 是第一个出现两次的字母,因为在所有字母中,'c' 第二次出现的下标是最小的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abcdd"
|
||||
<strong>输出:</strong>"d"
|
||||
<strong>解释:</strong>
|
||||
只有字母 'd' 出现两次,所以返回 'd' 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 由小写英文字母组成</li>
|
||||
<li><code>s</code> 包含至少一个重复字母</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>设计一个数字容器系统,可以实现以下功能:</p>
|
||||
|
||||
<ul>
|
||||
<li>在系统中给定下标处 <strong>插入</strong> 或者 <strong>替换</strong> 一个数字。</li>
|
||||
<li><strong>返回</strong> 系统中给定数字的最小下标。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你实现一个 <code>NumberContainers</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>NumberContainers()</code> 初始化数字容器系统。</li>
|
||||
<li><code>void change(int index, int number)</code> 在下标 <code>index</code> 处填入 <code>number</code> 。如果该下标 <code>index</code> 处已经有数字了,那么用 <code>number</code> 替换该数字。</li>
|
||||
<li><code>int find(int number)</code> 返回给定数字 <code>number</code> 在系统中的最小下标。如果系统中没有 <code>number</code> ,那么返回 <code>-1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
|
||||
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
|
||||
<strong>输出:</strong>
|
||||
[null, -1, null, null, null, null, 1, null, 2]
|
||||
|
||||
<strong>解释:</strong>
|
||||
NumberContainers nc = new NumberContainers();
|
||||
nc.find(10); // 没有数字 10 ,所以返回 -1 。
|
||||
nc.change(2, 10); // 容器中下标为 2 处填入数字 10 。
|
||||
nc.change(1, 10); // 容器中下标为 1 处填入数字 10 。
|
||||
nc.change(3, 10); // 容器中下标为 3 处填入数字 10 。
|
||||
nc.change(5, 10); // 容器中下标为 5 处填入数字 10 。
|
||||
nc.find(10); // 数字 10 所在的下标为 1 ,2 ,3 和 5 。因为最小下标为 1 ,所以返回 1 。
|
||||
nc.change(1, 20); // 容器中下标为 1 处填入数字 20 。注意,下标 1 处之前为 10 ,现在被替换为 20 。
|
||||
nc.find(10); // 数字 10 所在下标为 2 ,3 和 5 。最小下标为 2 ,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= index, number <= 10<sup>9</sup></code></li>
|
||||
<li>调用 <code>change</code> 和 <code>find</code> 的 <strong>总次数</strong> 不超过 <code>10<sup>5</sup></code> 次。</li>
|
||||
</ul>
|
@ -0,0 +1,64 @@
|
||||
<p>设计一个支持下述操作的食物评分系统:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>修改</strong> 系统中列出的某种食物的评分。</li>
|
||||
<li>返回系统中某一类烹饪方式下评分最高的食物。</li>
|
||||
</ul>
|
||||
|
||||
<p>实现 <code>FoodRatings</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>FoodRatings(String[] foods, String[] cuisines, int[] ratings)</code> 初始化系统。食物由 <code>foods</code>、<code>cuisines</code> 和 <code>ratings</code> 描述,长度均为 <code>n</code> 。
|
||||
|
||||
<ul>
|
||||
<li><code>foods[i]</code> 是第 <code>i</code> 种食物的名字。</li>
|
||||
<li><code>cuisines[i]</code> 是第 <code>i</code> 种食物的烹饪方式。</li>
|
||||
<li><code>ratings[i]</code> 是第 <code>i</code> 种食物的最初评分。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>void changeRating(String food, int newRating)</code> 修改名字为 <code>food</code> 的食物的评分。</li>
|
||||
<li><code>String highestRated(String cuisine)</code> 返回指定烹饪方式 <code>cuisine</code> 下评分最高的食物的名字。如果存在并列,返回 <strong>字典序较小</strong> 的名字。</li>
|
||||
</ul>
|
||||
|
||||
<p>注意,字符串 <code>x</code> 的字典序比字符串 <code>y</code> 更小的前提是:<code>x</code> 在字典中出现的位置在 <code>y</code> 之前,也就是说,要么 <code>x</code> 是 <code>y</code> 的前缀,或者在满足 <code>x[i] != y[i]</code> 的第一个位置 <code>i</code> 处,<code>x[i]</code> 在字母表中出现的位置在 <code>y[i]</code> 之前。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入</strong>
|
||||
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
|
||||
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
|
||||
<strong>输出</strong>
|
||||
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
|
||||
|
||||
<strong>解释</strong>
|
||||
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
|
||||
foodRatings.highestRated("korean"); // 返回 "kimchi"
|
||||
// "kimchi" 是分数最高的韩式料理,评分为 9 。
|
||||
foodRatings.highestRated("japanese"); // 返回 "ramen"
|
||||
// "ramen" 是分数最高的日式料理,评分为 14 。
|
||||
foodRatings.changeRating("sushi", 16); // "sushi" 现在评分变更为 16 。
|
||||
foodRatings.highestRated("japanese"); // 返回 "sushi"
|
||||
// "sushi" 是分数最高的日式料理,评分为 16 。
|
||||
foodRatings.changeRating("ramen", 16); // "ramen" 现在评分变更为 16 。
|
||||
foodRatings.highestRated("japanese"); // 返回 "ramen"
|
||||
// "sushi" 和 "ramen" 的评分都是 16 。
|
||||
// 但是,"ramen" 的字典序比 "sushi" 更小。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n == foods.length == cuisines.length == ratings.length</code></li>
|
||||
<li><code>1 <= foods[i].length, cuisines[i].length <= 10</code></li>
|
||||
<li><code>foods[i]</code>、<code>cuisines[i]</code> 由小写英文字母组成</li>
|
||||
<li><code>1 <= ratings[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>foods</code> 中的所有字符串 <strong>互不相同</strong></li>
|
||||
<li>在对 <code>changeRating</code> 的所有调用中,<code>food</code> 是系统中食物的名字。</li>
|
||||
<li>在对 <code>highestRated</code> 的所有调用中,<code>cuisine</code> 是系统中 <strong>至少一种</strong> 食物的烹饪方式。</li>
|
||||
<li>最多调用 <code>changeRating</code> 和 <code>highestRated</code> <strong>总计</strong> <code>2 * 10<sup>4</sup></code> 次</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>rolls</code> of length <code>n</code> and an integer <code>k</code>. You roll a <code>k</code> sided dice numbered from <code>1</code> to <code>k</code>, <code>n</code> times, where the result of the <code>i<sup>th</sup></code> roll is <code>rolls[i]</code>.</p>
|
||||
|
||||
<p>Return<em> the length of the <strong>shortest</strong> sequence of rolls that <strong>cannot</strong> be taken from </em><code>rolls</code>.</p>
|
||||
|
||||
<p>A <strong>sequence of rolls</strong> of length <code>len</code> is the result of rolling a <code>k</code> sided dice <code>len</code> times.</p>
|
||||
|
||||
<p><strong>Note</strong> that the sequence taken does not have to be consecutive as long as it is in order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [4,2,1,2,3,3,2,4,1], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], [3], [4], can be taken from rolls.
|
||||
Every sequence of rolls of length 2, [1, 1], [1, 2], ..., [4, 4], can be taken from rolls.
|
||||
The sequence [1, 4, 2] cannot be taken from rolls, so we return 3.
|
||||
Note that there are other sequences that cannot be taken from rolls.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [1,1,2,2], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], can be taken from rolls.
|
||||
The sequence [2, 1] cannot be taken from rolls, so we return 2.
|
||||
Note that there are other sequences that cannot be taken from rolls but [2, 1] is the shortest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [1,1,3,2,2,2,3,3], k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The sequence [4] cannot be taken from rolls, so we return 1.
|
||||
Note that there are other sequences that cannot be taken from rolls but [4] is the shortest.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == rolls.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= rolls[i] <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> positive integer array <code>nums</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>A pair of numbers <code>(num1, num2)</code> is called <strong>excellent</strong> if the following conditions are satisfied:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Both</strong> the numbers <code>num1</code> and <code>num2</code> exist in the array <code>nums</code>.</li>
|
||||
<li>The sum of the number of set bits in <code>num1 OR num2</code> and <code>num1 AND num2</code> is greater than or equal to <code>k</code>, where <code>OR</code> is the bitwise <strong>OR</strong> operation and <code>AND</code> is the bitwise <strong>AND</strong> operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> excellent pairs</em>.</p>
|
||||
|
||||
<p>Two pairs <code>(a, b)</code> and <code>(c, d)</code> are considered distinct if either <code>a != c</code> or <code>b != d</code>. For example, <code>(1, 2)</code> and <code>(2, 1)</code> are distinct.</p>
|
||||
|
||||
<p><strong>Note</strong> that a pair <code>(num1, num2)</code> such that <code>num1 == num2</code> can also be excellent if you have at least <strong>one</strong> occurrence of <code>num1</code> in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,1], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The excellent pairs are the following:
|
||||
- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
|
||||
- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
|
||||
- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
|
||||
So the number of excellent pairs is 5.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,1,1], k = 10
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no excellent pairs for this array.
|
||||
</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>
|
||||
<li><code>1 <= k <= 60</code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the number of <strong>subarrays</strong> filled with </em><code>0</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,0,0,2,0,0,4]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
There are 4 occurrences of [0] as a subarray.
|
||||
There are 2 occurrences of [0,0] as a subarray.
|
||||
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0,2,0,0]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:
|
||||
</strong>There are 5 occurrences of [0] as a subarray.
|
||||
There are 3 occurrences of [0,0] as a subarray.
|
||||
There is 1 occurrence of [0,0,0] as a subarray.
|
||||
There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,10,2019]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no subarray filled with 0. 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>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given an integer array <code>ranks</code> and a character array <code>suits</code>. You have <code>5</code> cards where the <code>i<sup>th</sup></code> card has a rank of <code>ranks[i]</code> and a suit of <code>suits[i]</code>.</p>
|
||||
|
||||
<p>The following are the types of <strong>poker hands</strong> you can make from best to worst:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>"Flush"</code>: Five cards of the same suit.</li>
|
||||
<li><code>"Three of a Kind"</code>: Three cards of the same rank.</li>
|
||||
<li><code>"Pair"</code>: Two cards of the same rank.</li>
|
||||
<li><code>"High Card"</code>: Any single card.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>a string representing the <strong>best</strong> type of <strong>poker hand</strong> you can make with the given cards.</em></p>
|
||||
|
||||
<p><strong>Note</strong> that the return values are <strong>case-sensitive</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
|
||||
<strong>Output:</strong> "Flush"
|
||||
<strong>Explanation:</strong> The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
|
||||
<strong>Output:</strong> "Three of a Kind"
|
||||
<strong>Explanation:</strong> The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
|
||||
Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
|
||||
Also note that other cards could be used to make the "Three of a Kind" hand.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
|
||||
<strong>Output:</strong> "Pair"
|
||||
<strong>Explanation:</strong> The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
|
||||
Note that we cannot make a "Flush" or a "Three of a Kind".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>ranks.length == suits.length == 5</code></li>
|
||||
<li><code>1 <= ranks[i] <= 13</code></li>
|
||||
<li><code>'a' <= suits[i] <= 'd'</code></li>
|
||||
<li>No two cards have the same rank and suit.</li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>0-indexed</strong> <code>n x n</code> integer matrix <code>grid</code>, <em>return the number of pairs </em><code>(R<sub>i</sub>, C<sub>j</sub>)</code><em> such that row </em><code>R<sub>i</sub></code><em> and column </em><code>C<sub>j</sub></code><em> are equal</em>.</p>
|
||||
|
||||
<p>A row and column pair is considered equal if they contain the same elements in the same order (i.e. an equal array).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg" style="width: 150px; height: 153px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,2,1],[1,7,6],[2,7,7]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is 1 equal row and column pair:
|
||||
- (Row 2, Column 1): [2,7,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg" style="width: 200px; height: 209px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 equal row and column pairs:
|
||||
- (Row 0, Column 0): [3,1,2,2]
|
||||
- (Row 2, Column 2): [2,4,2,2]
|
||||
- (Row 3, Column 2): [2,4,2,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>Given a string <code>s</code> consisting of lowercase English letters, return <em>the first letter to appear <strong>twice</strong></em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A letter <code>a</code> appears twice before another letter <code>b</code> if the <strong>second</strong> occurrence of <code>a</code> is before the <strong>second</strong> occurrence of <code>b</code>.</li>
|
||||
<li><code>s</code> will contain at least one letter that appears twice.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abccbaacz"
|
||||
<strong>Output:</strong> "c"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'a' appears on the indexes 0, 5 and 6.
|
||||
The letter 'b' appears on the indexes 1 and 4.
|
||||
The letter 'c' appears on the indexes 2, 3 and 7.
|
||||
The letter 'z' appears on the index 8.
|
||||
The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdd"
|
||||
<strong>Output:</strong> "d"
|
||||
<strong>Explanation:</strong>
|
||||
The only letter that appears twice is 'd' so we return 'd'.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
<li><code>s</code> has at least one repeated letter.</li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>Design a number container system that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Insert </strong>or <strong>Replace</strong> a number at the given index in the system.</li>
|
||||
<li><strong>Return </strong>the smallest index for the given number in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>NumberContainers</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>NumberContainers()</code> Initializes the number container system.</li>
|
||||
<li><code>void change(int index, int number)</code> Fills the container at <code>index</code> with the <code>number</code>. If there is already a number at that <code>index</code>, replace it.</li>
|
||||
<li><code>int find(int number)</code> Returns the smallest index for the given <code>number</code>, or <code>-1</code> if there is no index that is filled by <code>number</code> in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
|
||||
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
|
||||
<strong>Output</strong>
|
||||
[null, -1, null, null, null, null, 1, null, 2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
NumberContainers nc = new NumberContainers();
|
||||
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
|
||||
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
|
||||
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
|
||||
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
|
||||
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
|
||||
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
|
||||
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
|
||||
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= index, number <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>10<sup>5</sup></code> calls will be made <strong>in total</strong> to <code>change</code> and <code>find</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,63 @@
|
||||
<p>Design a food rating system that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Modify</strong> the rating of a food item listed in the system.</li>
|
||||
<li>Return the highest-rated food item for a type of cuisine in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>FoodRatings</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>FoodRatings(String[] foods, String[] cuisines, int[] ratings)</code> Initializes the system. The food items are described by <code>foods</code>, <code>cuisines</code> and <code>ratings</code>, all of which have a length of <code>n</code>.
|
||||
|
||||
<ul>
|
||||
<li><code>foods[i]</code> is the name of the <code>i<sup>th</sup></code> food,</li>
|
||||
<li><code>cuisines[i]</code> is the type of cuisine of the <code>i<sup>th</sup></code> food, and</li>
|
||||
<li><code>ratings[i]</code> is the initial rating of the <code>i<sup>th</sup></code> food.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>void changeRating(String food, int newRating)</code> Changes the rating of the food item with the name <code>food</code>.</li>
|
||||
<li><code>String highestRated(String cuisine)</code> Returns the name of the food item that has the highest rating for the given type of <code>cuisine</code>. If there is a tie, return the item with the <strong>lexicographically smaller</strong> name.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that a string <code>x</code> is lexicographically smaller than string <code>y</code> if <code>x</code> comes before <code>y</code> in dictionary order, that is, either <code>x</code> is a prefix of <code>y</code>, or if <code>i</code> is the first position such that <code>x[i] != y[i]</code>, then <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
|
||||
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
|
||||
<strong>Output</strong>
|
||||
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
|
||||
foodRatings.highestRated("korean"); // return "kimchi"
|
||||
// "kimchi" is the highest rated korean food with a rating of 9.
|
||||
foodRatings.highestRated("japanese"); // return "ramen"
|
||||
// "ramen" is the highest rated japanese food with a rating of 14.
|
||||
foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
|
||||
foodRatings.highestRated("japanese"); // return "sushi"
|
||||
// "sushi" is the highest rated japanese food with a rating of 16.
|
||||
foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
|
||||
foodRatings.highestRated("japanese"); // return "ramen"
|
||||
// Both "sushi" and "ramen" have a rating of 16.
|
||||
// However, "ramen" is lexicographically smaller than "sushi".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n == foods.length == cuisines.length == ratings.length</code></li>
|
||||
<li><code>1 <= foods[i].length, cuisines[i].length <= 10</code></li>
|
||||
<li><code>foods[i]</code>, <code>cuisines[i]</code> consist of lowercase English letters.</li>
|
||||
<li><code>1 <= ratings[i] <= 10<sup>8</sup></code></li>
|
||||
<li>All the strings in <code>foods</code> are <strong>distinct</strong>.</li>
|
||||
<li><code>food</code> will be the name of a food item in the system across all calls to <code>changeRating</code>.</li>
|
||||
<li><code>cuisine</code> will be a type of cuisine of <strong>at least one</strong> food item in the system across all calls to <code>highestRated</code>.</li>
|
||||
<li>At most <code>2 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>changeRating</code> and <code>highestRated</code>.</li>
|
||||
</ul>
|
108170
leetcode/origin-data.json
108170
leetcode/origin-data.json
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -0,0 +1,53 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2495",
|
||||
"questionFrontendId": "2356",
|
||||
"boundTopicId": null,
|
||||
"title": "Number of Unique Subjects Taught by Each Teacher",
|
||||
"titleSlug": "number-of-unique-subjects-taught-by-each-teacher",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Easy",
|
||||
"likes": 2,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": null,
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"76\", \"totalSubmission\": \"86\", \"totalAcceptedRaw\": 76, \"totalSubmissionRaw\": 86, \"acRate\": \"88.4%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Teacher\":[\"teacher_id\",\"subject_id\",\"dept_id\"]},\"rows\":{\"Teacher\":[[1,2,3],[1,2,4],[1,3,3],[2,1,1],[2,2,1],[2,3,1],[2,4,1]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)\"\n ],\n \"mssql\": [\n \"Create table Teacher (teacher_id int, subject_id int, dept_id int)\"\n ],\n \"oraclesql\": [\n \"Create table Teacher (teacher_id int, subject_id int, dept_id int)\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)",
|
||||
"Truncate table Teacher",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '3')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '4')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '3', '3')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '1', '1')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '2', '1')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '3', '1')",
|
||||
"insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '4', '1')"
|
||||
],
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
179
leetcode/originData/best-poker-hand.json
Normal file
179
leetcode/originData/best-poker-hand.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/design-a-food-rating-system.json
Normal file
187
leetcode/originData/design-a-food-rating-system.json
Normal file
File diff suppressed because one or more lines are too long
186
leetcode/originData/design-a-number-container-system.json
Normal file
186
leetcode/originData/design-a-number-container-system.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/equal-row-and-column-pairs.json
Normal file
187
leetcode/originData/equal-row-and-column-pairs.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/first-letter-to-appear-twice.json
Normal file
180
leetcode/originData/first-letter-to-appear-twice.json
Normal file
File diff suppressed because one or more lines are too long
186
leetcode/originData/number-of-excellent-pairs.json
Normal file
186
leetcode/originData/number-of-excellent-pairs.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/number-of-zero-filled-subarrays.json
Normal file
174
leetcode/originData/number-of-zero-filled-subarrays.json
Normal file
File diff suppressed because one or more lines are too long
181
leetcode/originData/shortest-impossible-sequence-of-rolls.json
Normal file
181
leetcode/originData/shortest-impossible-sequence-of-rolls.json
Normal file
File diff suppressed because one or more lines are too long
51
leetcode/problem/best-poker-hand.html
Normal file
51
leetcode/problem/best-poker-hand.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given an integer array <code>ranks</code> and a character array <code>suits</code>. You have <code>5</code> cards where the <code>i<sup>th</sup></code> card has a rank of <code>ranks[i]</code> and a suit of <code>suits[i]</code>.</p>
|
||||
|
||||
<p>The following are the types of <strong>poker hands</strong> you can make from best to worst:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>"Flush"</code>: Five cards of the same suit.</li>
|
||||
<li><code>"Three of a Kind"</code>: Three cards of the same rank.</li>
|
||||
<li><code>"Pair"</code>: Two cards of the same rank.</li>
|
||||
<li><code>"High Card"</code>: Any single card.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>a string representing the <strong>best</strong> type of <strong>poker hand</strong> you can make with the given cards.</em></p>
|
||||
|
||||
<p><strong>Note</strong> that the return values are <strong>case-sensitive</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
|
||||
<strong>Output:</strong> "Flush"
|
||||
<strong>Explanation:</strong> The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
|
||||
<strong>Output:</strong> "Three of a Kind"
|
||||
<strong>Explanation:</strong> The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
|
||||
Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
|
||||
Also note that other cards could be used to make the "Three of a Kind" hand.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
|
||||
<strong>Output:</strong> "Pair"
|
||||
<strong>Explanation:</strong> The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
|
||||
Note that we cannot make a "Flush" or a "Three of a Kind".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>ranks.length == suits.length == 5</code></li>
|
||||
<li><code>1 <= ranks[i] <= 13</code></li>
|
||||
<li><code>'a' <= suits[i] <= 'd'</code></li>
|
||||
<li>No two cards have the same rank and suit.</li>
|
||||
</ul>
|
63
leetcode/problem/design-a-food-rating-system.html
Normal file
63
leetcode/problem/design-a-food-rating-system.html
Normal file
@ -0,0 +1,63 @@
|
||||
<p>Design a food rating system that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Modify</strong> the rating of a food item listed in the system.</li>
|
||||
<li>Return the highest-rated food item for a type of cuisine in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>FoodRatings</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>FoodRatings(String[] foods, String[] cuisines, int[] ratings)</code> Initializes the system. The food items are described by <code>foods</code>, <code>cuisines</code> and <code>ratings</code>, all of which have a length of <code>n</code>.
|
||||
|
||||
<ul>
|
||||
<li><code>foods[i]</code> is the name of the <code>i<sup>th</sup></code> food,</li>
|
||||
<li><code>cuisines[i]</code> is the type of cuisine of the <code>i<sup>th</sup></code> food, and</li>
|
||||
<li><code>ratings[i]</code> is the initial rating of the <code>i<sup>th</sup></code> food.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>void changeRating(String food, int newRating)</code> Changes the rating of the food item with the name <code>food</code>.</li>
|
||||
<li><code>String highestRated(String cuisine)</code> Returns the name of the food item that has the highest rating for the given type of <code>cuisine</code>. If there is a tie, return the item with the <strong>lexicographically smaller</strong> name.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that a string <code>x</code> is lexicographically smaller than string <code>y</code> if <code>x</code> comes before <code>y</code> in dictionary order, that is, either <code>x</code> is a prefix of <code>y</code>, or if <code>i</code> is the first position such that <code>x[i] != y[i]</code>, then <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
|
||||
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
|
||||
<strong>Output</strong>
|
||||
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
|
||||
foodRatings.highestRated("korean"); // return "kimchi"
|
||||
// "kimchi" is the highest rated korean food with a rating of 9.
|
||||
foodRatings.highestRated("japanese"); // return "ramen"
|
||||
// "ramen" is the highest rated japanese food with a rating of 14.
|
||||
foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
|
||||
foodRatings.highestRated("japanese"); // return "sushi"
|
||||
// "sushi" is the highest rated japanese food with a rating of 16.
|
||||
foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
|
||||
foodRatings.highestRated("japanese"); // return "ramen"
|
||||
// Both "sushi" and "ramen" have a rating of 16.
|
||||
// However, "ramen" is lexicographically smaller than "sushi".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n == foods.length == cuisines.length == ratings.length</code></li>
|
||||
<li><code>1 <= foods[i].length, cuisines[i].length <= 10</code></li>
|
||||
<li><code>foods[i]</code>, <code>cuisines[i]</code> consist of lowercase English letters.</li>
|
||||
<li><code>1 <= ratings[i] <= 10<sup>8</sup></code></li>
|
||||
<li>All the strings in <code>foods</code> are <strong>distinct</strong>.</li>
|
||||
<li><code>food</code> will be the name of a food item in the system across all calls to <code>changeRating</code>.</li>
|
||||
<li><code>cuisine</code> will be a type of cuisine of <strong>at least one</strong> food item in the system across all calls to <code>highestRated</code>.</li>
|
||||
<li>At most <code>2 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>changeRating</code> and <code>highestRated</code>.</li>
|
||||
</ul>
|
44
leetcode/problem/design-a-number-container-system.html
Normal file
44
leetcode/problem/design-a-number-container-system.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>Design a number container system that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Insert </strong>or <strong>Replace</strong> a number at the given index in the system.</li>
|
||||
<li><strong>Return </strong>the smallest index for the given number in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>NumberContainers</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>NumberContainers()</code> Initializes the number container system.</li>
|
||||
<li><code>void change(int index, int number)</code> Fills the container at <code>index</code> with the <code>number</code>. If there is already a number at that <code>index</code>, replace it.</li>
|
||||
<li><code>int find(int number)</code> Returns the smallest index for the given <code>number</code>, or <code>-1</code> if there is no index that is filled by <code>number</code> in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
|
||||
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
|
||||
<strong>Output</strong>
|
||||
[null, -1, null, null, null, null, 1, null, 2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
NumberContainers nc = new NumberContainers();
|
||||
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
|
||||
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
|
||||
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
|
||||
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
|
||||
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
|
||||
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
|
||||
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
|
||||
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= index, number <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>10<sup>5</sup></code> calls will be made <strong>in total</strong> to <code>change</code> and <code>find</code>.</li>
|
||||
</ul>
|
33
leetcode/problem/equal-row-and-column-pairs.html
Normal file
33
leetcode/problem/equal-row-and-column-pairs.html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>0-indexed</strong> <code>n x n</code> integer matrix <code>grid</code>, <em>return the number of pairs </em><code>(R<sub>i</sub>, C<sub>j</sub>)</code><em> such that row </em><code>R<sub>i</sub></code><em> and column </em><code>C<sub>j</sub></code><em> are equal</em>.</p>
|
||||
|
||||
<p>A row and column pair is considered equal if they contain the same elements in the same order (i.e. an equal array).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg" style="width: 150px; height: 153px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,2,1],[1,7,6],[2,7,7]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is 1 equal row and column pair:
|
||||
- (Row 2, Column 1): [2,7,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg" style="width: 200px; height: 209px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 equal row and column pairs:
|
||||
- (Row 0, Column 0): [3,1,2,2]
|
||||
- (Row 2, Column 2): [2,4,2,2]
|
||||
- (Row 3, Column 2): [2,4,2,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
40
leetcode/problem/first-letter-to-appear-twice.html
Normal file
40
leetcode/problem/first-letter-to-appear-twice.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Given a string <code>s</code> consisting of lowercase English letters, return <em>the first letter to appear <strong>twice</strong></em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A letter <code>a</code> appears twice before another letter <code>b</code> if the <strong>second</strong> occurrence of <code>a</code> is before the <strong>second</strong> occurrence of <code>b</code>.</li>
|
||||
<li><code>s</code> will contain at least one letter that appears twice.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abccbaacz"
|
||||
<strong>Output:</strong> "c"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'a' appears on the indexes 0, 5 and 6.
|
||||
The letter 'b' appears on the indexes 1 and 4.
|
||||
The letter 'c' appears on the indexes 2, 3 and 7.
|
||||
The letter 'z' appears on the index 8.
|
||||
The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdd"
|
||||
<strong>Output:</strong> "d"
|
||||
<strong>Explanation:</strong>
|
||||
The only letter that appears twice is 'd' so we return 'd'.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
<li><code>s</code> has at least one repeated letter.</li>
|
||||
</ul>
|
43
leetcode/problem/number-of-excellent-pairs.html
Normal file
43
leetcode/problem/number-of-excellent-pairs.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> positive integer array <code>nums</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>A pair of numbers <code>(num1, num2)</code> is called <strong>excellent</strong> if the following conditions are satisfied:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Both</strong> the numbers <code>num1</code> and <code>num2</code> exist in the array <code>nums</code>.</li>
|
||||
<li>The sum of the number of set bits in <code>num1 OR num2</code> and <code>num1 AND num2</code> is greater than or equal to <code>k</code>, where <code>OR</code> is the bitwise <strong>OR</strong> operation and <code>AND</code> is the bitwise <strong>AND</strong> operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> excellent pairs</em>.</p>
|
||||
|
||||
<p>Two pairs <code>(a, b)</code> and <code>(c, d)</code> are considered distinct if either <code>a != c</code> or <code>b != d</code>. For example, <code>(1, 2)</code> and <code>(2, 1)</code> are distinct.</p>
|
||||
|
||||
<p><strong>Note</strong> that a pair <code>(num1, num2)</code> such that <code>num1 == num2</code> can also be excellent if you have at least <strong>one</strong> occurrence of <code>num1</code> in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,1], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The excellent pairs are the following:
|
||||
- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
|
||||
- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
|
||||
- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
|
||||
So the number of excellent pairs is 5.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,1,1], k = 10
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no excellent pairs for this array.
|
||||
</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>
|
||||
<li><code>1 <= k <= 60</code></li>
|
||||
</ul>
|
42
leetcode/problem/number-of-zero-filled-subarrays.html
Normal file
42
leetcode/problem/number-of-zero-filled-subarrays.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the number of <strong>subarrays</strong> filled with </em><code>0</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,0,0,2,0,0,4]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
There are 4 occurrences of [0] as a subarray.
|
||||
There are 2 occurrences of [0,0] as a subarray.
|
||||
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0,2,0,0]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:
|
||||
</strong>There are 5 occurrences of [0] as a subarray.
|
||||
There are 3 occurrences of [0,0] as a subarray.
|
||||
There is 1 occurrence of [0,0,0] as a subarray.
|
||||
There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,10,2019]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no subarray filled with 0. 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>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
46
leetcode/problem/shortest-impossible-sequence-of-rolls.html
Normal file
46
leetcode/problem/shortest-impossible-sequence-of-rolls.html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given an integer array <code>rolls</code> of length <code>n</code> and an integer <code>k</code>. You roll a <code>k</code> sided dice numbered from <code>1</code> to <code>k</code>, <code>n</code> times, where the result of the <code>i<sup>th</sup></code> roll is <code>rolls[i]</code>.</p>
|
||||
|
||||
<p>Return<em> the length of the <strong>shortest</strong> sequence of rolls that <strong>cannot</strong> be taken from </em><code>rolls</code>.</p>
|
||||
|
||||
<p>A <strong>sequence of rolls</strong> of length <code>len</code> is the result of rolling a <code>k</code> sided dice <code>len</code> times.</p>
|
||||
|
||||
<p><strong>Note</strong> that the sequence taken does not have to be consecutive as long as it is in order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [4,2,1,2,3,3,2,4,1], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], [3], [4], can be taken from rolls.
|
||||
Every sequence of rolls of length 2, [1, 1], [1, 2], ..., [4, 4], can be taken from rolls.
|
||||
The sequence [1, 4, 2] cannot be taken from rolls, so we return 3.
|
||||
Note that there are other sequences that cannot be taken from rolls.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [1,1,2,2], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], can be taken from rolls.
|
||||
The sequence [2, 1] cannot be taken from rolls, so we return 2.
|
||||
Note that there are other sequences that cannot be taken from rolls but [2, 1] is the shortest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [1,1,3,2,2,2,3,3], k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The sequence [4] cannot be taken from rolls, so we return 1.
|
||||
Note that there are other sequences that cannot be taken from rolls but [4] is the shortest.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == rolls.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= rolls[i] <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user