mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-04-18 08:39:41 +08:00
update
This commit is contained in:
parent
8891b38740
commit
210ba37692
README.md
leetcode-cn
origin-data.json
originData
[no content]jump-game-viii.json[no content]minimum-flips-in-binary-tree-to-get-result.json[no content]the-first-day-of-the-maximum-recorded-degree-in-each-city.jsoncheck-if-matrix-is-x-matrix.jsoncount-asterisks.jsoncount-number-of-ways-to-place-houses.jsoncount-unreachable-pairs-of-nodes-in-an-undirected-graph.jsongreatest-english-letter-in-upper-and-lower-case.jsonlongest-binary-subsequence-less-than-or-equal-to-k.jsonmaximum-score-of-spliced-array.jsonmaximum-xor-after-operations.jsonminimum-score-after-removals-on-a-tree.jsonnumber-of-distinct-roll-sequences.jsonselling-pieces-of-wood.jsonsum-of-numbers-with-units-digit-k.json
problem (Chinese)
不同骰子序列的数目 [number-of-distinct-roll-sequences].html个位数字为 K 的整数之和 [sum-of-numbers-with-units-digit-k].html从树中删除边的最小分数 [minimum-score-after-removals-on-a-tree].html兼具大小写的最好英文字母 [greatest-english-letter-in-upper-and-lower-case].html判断矩阵是否是一个 X 矩阵 [check-if-matrix-is-x-matrix].html卖木头块 [selling-pieces-of-wood].html小于等于 K 的最长二进制子序列 [longest-binary-subsequence-less-than-or-equal-to-k].html拼接数组的最大分数 [maximum-score-of-spliced-array].html操作后的最大异或和 [maximum-xor-after-operations].html统计放置房子的方式数 [count-number-of-ways-to-place-houses].html统计无向图中无法互相到达点对数 [count-unreachable-pairs-of-nodes-in-an-undirected-graph].html统计星号 [count-asterisks].html
problem (English)
不同骰子序列的数目(English) [number-of-distinct-roll-sequences].html个位数字为 K 的整数之和(English) [sum-of-numbers-with-units-digit-k].html从树中删除边的最小分数(English) [minimum-score-after-removals-on-a-tree].html兼具大小写的最好英文字母(English) [greatest-english-letter-in-upper-and-lower-case].html判断矩阵是否是一个 X 矩阵(English) [check-if-matrix-is-x-matrix].html卖木头块(English) [selling-pieces-of-wood].html小于等于 K 的最长二进制子序列(English) [longest-binary-subsequence-less-than-or-equal-to-k].html拼接数组的最大分数(English) [maximum-score-of-spliced-array].html操作后的最大异或和(English) [maximum-xor-after-operations].html统计放置房子的方式数(English) [count-number-of-ways-to-place-houses].html统计无向图中无法互相到达点对数(English) [count-unreachable-pairs-of-nodes-in-an-undirected-graph].html统计星号(English) [count-asterisks].html
leetcode
origin-data.json
originData
[no content]minimum-flips-in-binary-tree-to-get-result.json[no content]the-first-day-of-the-maximum-recorded-degree-in-each-city.jsoncheck-if-matrix-is-x-matrix.jsoncount-asterisks.jsoncount-number-of-ways-to-place-houses.jsoncount-unreachable-pairs-of-nodes-in-an-undirected-graph.jsongreatest-english-letter-in-upper-and-lower-case.jsonlongest-binary-subsequence-less-than-or-equal-to-k.jsonmaximum-score-of-spliced-array.jsonmaximum-xor-after-operations.jsonminimum-score-after-removals-on-a-tree.jsonnumber-of-distinct-roll-sequences.jsonselling-pieces-of-wood.jsonsum-of-numbers-with-units-digit-k.json
problem
check-if-matrix-is-x-matrix.htmlcount-asterisks.htmlcount-number-of-ways-to-place-houses.htmlcount-unreachable-pairs-of-nodes-in-an-undirected-graph.htmlgreatest-english-letter-in-upper-and-lower-case.htmllongest-binary-subsequence-less-than-or-equal-to-k.htmlmaximum-score-of-spliced-array.htmlmaximum-xor-after-operations.htmlminimum-score-after-removals-on-a-tree.htmlnumber-of-distinct-roll-sequences.htmlselling-pieces-of-wood.htmlsum-of-numbers-with-units-digit-k.html
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.06.18**
|
||||
> 最后更新日期: **2022.06.29**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
87
leetcode-cn/originData/[no content]jump-game-viii.json
Normal file
87
leetcode-cn/originData/[no content]jump-game-viii.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
63
leetcode-cn/originData/[no content]the-first-day-of-the-maximum-recorded-degree-in-each-city.json
Normal file
63
leetcode-cn/originData/[no content]the-first-day-of-the-maximum-recorded-degree-in-each-city.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2446",
|
||||
"questionFrontendId": "2314",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 1620675,
|
||||
"title": "The First Day of the Maximum Recorded Degree in Each City",
|
||||
"titleSlug": "the-first-day-of-the-maximum-recorded-degree-in-each-city",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 1,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": "数据库",
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"61\", \"totalSubmission\": \"82\", \"totalAcceptedRaw\": 61, \"totalSubmissionRaw\": 82, \"acRate\": \"74.4%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Weather\": [\"city_id\", \"day\", \"degree\"]}, \"rows\": {\"Weather\": [[1, \"2022-01-07\", -12], [1, \"2022-03-07\", 5], [1, \"2022-07-07\", 24], [2, \"2022-08-07\", 37], [2, \"2022-08-17\", 37], [3, \"2022-02-07\", -7], [3, \"2022-12-07\", -6]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Weather (city_id int, day date, degree int)\"\n ],\n \"mssql\": [\n \"Create table Weather (city_id int, day date, degree int)\"\n ],\n \"oraclesql\": [\n \"Create table Weather (city_id int, day date, degree int)\",\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 Weather (city_id int, day date, degree int)",
|
||||
"Truncate table Weather",
|
||||
"insert into Weather (city_id, day, degree) values ('1', '2022-01-07', '-12')",
|
||||
"insert into Weather (city_id, day, degree) values ('1', '2022-03-07', '5')",
|
||||
"insert into Weather (city_id, day, degree) values ('1', '2022-07-07', '24')",
|
||||
"insert into Weather (city_id, day, degree) values ('2', '2022-08-07', '37')",
|
||||
"insert into Weather (city_id, day, degree) values ('2', '2022-08-17', '37')",
|
||||
"insert into Weather (city_id, day, degree) values ('3', '2022-02-07', '-7')",
|
||||
"insert into Weather (city_id, day, degree) values ('3', '2022-12-07', '-6')"
|
||||
],
|
||||
"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\": {\"Weather\": [\"city_id\", \"day\", \"degree\"]}, \"rows\": {\"Weather\": [[1, \"2022-01-07\", -12], [1, \"2022-03-07\", 5], [1, \"2022-07-07\", 24], [2, \"2022-08-07\", 37], [2, \"2022-08-17\", 37], [3, \"2022-02-07\", -7], [3, \"2022-12-07\", -6]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
171
leetcode-cn/originData/check-if-matrix-is-x-matrix.json
Normal file
171
leetcode-cn/originData/check-if-matrix-is-x-matrix.json
Normal file
File diff suppressed because one or more lines are too long
165
leetcode-cn/originData/count-asterisks.json
Normal file
165
leetcode-cn/originData/count-asterisks.json
Normal file
File diff suppressed because one or more lines are too long
166
leetcode-cn/originData/count-number-of-ways-to-place-houses.json
Normal file
166
leetcode-cn/originData/count-number-of-ways-to-place-houses.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
172
leetcode-cn/originData/maximum-score-of-spliced-array.json
Normal file
172
leetcode-cn/originData/maximum-score-of-spliced-array.json
Normal file
File diff suppressed because one or more lines are too long
179
leetcode-cn/originData/maximum-xor-after-operations.json
Normal file
179
leetcode-cn/originData/maximum-xor-after-operations.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
172
leetcode-cn/originData/number-of-distinct-roll-sequences.json
Normal file
172
leetcode-cn/originData/number-of-distinct-roll-sequences.json
Normal file
File diff suppressed because one or more lines are too long
178
leetcode-cn/originData/selling-pieces-of-wood.json
Normal file
178
leetcode-cn/originData/selling-pieces-of-wood.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/sum-of-numbers-with-units-digit-k.json
Normal file
183
leetcode-cn/originData/sum-of-numbers-with-units-digit-k.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,41 @@
|
||||
<p>给你一个整数 <code>n</code> 。你需要掷一个 6 面的骰子 <code>n</code> 次。请你在满足以下要求的前提下,求出 <strong>不同</strong> 骰子序列的数目:</p>
|
||||
|
||||
<ol>
|
||||
<li>序列中任意 <strong>相邻</strong> 数字的 <strong>最大公约数</strong> 为 <code>1</code> 。</li>
|
||||
<li>序列中 <strong>相等</strong> 的值之间,至少有 <code>2</code> 个其他值的数字。正式地,如果第 <code>i</code> 次掷骰子的值 <strong>等于</strong> 第 <code>j</code> 次的值,那么 <code>abs(i - j) > 2</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>请你返回不同序列的 <strong>总数目</strong> 。由于答案可能很大,请你将答案对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p>如果两个序列中至少有一个元素不同,那么它们被视为不同的序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 4
|
||||
<b>输出:</b>184
|
||||
<b>解释:</b>一些可行的序列为 (1, 2, 3, 4) ,(6, 1, 2, 3) ,(1, 2, 3, 1) 等等。
|
||||
一些不可行的序列为 (1, 2, 1, 3) ,(1, 2, 3, 6) 。
|
||||
(1, 2, 1, 3) 是不可行的,因为第一个和第三个骰子值相等且 abs(1 - 3) = 2 (下标从 1 开始表示)。
|
||||
(1, 2, 3, 6) i是不可行的,因为 3 和 6 的最大公约数是 3 。
|
||||
总共有 184 个不同的可行序列,所以我们返回 184 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 2
|
||||
<b>输出:</b>22
|
||||
<b>解释:</b>一些可行的序列为 (1, 2) ,(2, 1) ,(3, 2) 。
|
||||
一些不可行的序列为 (3, 6) ,(2, 4) ,因为最大公约数不为 1 。
|
||||
总共有 22 个不同的可行序列,所以我们返回 22 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>给你两个整数 <code>num</code> 和 <code>k</code> ,考虑具有以下属性的正整数多重集:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个整数个位数字都是 <code>k</code> 。</li>
|
||||
<li>所有整数之和是 <code>num</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回该多重集的最小大小,如果不存在这样的多重集,返回<em> </em><code>-1</code> 。</p>
|
||||
|
||||
<p>注意:</p>
|
||||
|
||||
<ul>
|
||||
<li>多重集与集合类似,但多重集可以包含多个同一整数,空多重集的和为 <code>0</code> 。</li>
|
||||
<li><strong>个位数字</strong> 是数字最右边的数位。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 58, k = 9
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
多重集 [9,49] 满足题目条件,和为 58 且每个整数的个位数字是 9 。
|
||||
另一个满足条件的多重集是 [19,39] 。
|
||||
可以证明 2 是满足题目条件的多重集的最小长度。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 37, k = 2
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>个位数字为 2 的整数无法相加得到 37 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 0, k = 7
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>空多重集的和为 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num <= 3000</code></li>
|
||||
<li><code>0 <= k <= 9</code></li>
|
||||
</ul>
|
57
leetcode-cn/problem (Chinese)/从树中删除边的最小分数 [minimum-score-after-removals-on-a-tree].html
Normal file
57
leetcode-cn/problem (Chinese)/从树中删除边的最小分数 [minimum-score-after-removals-on-a-tree].html
Normal file
@ -0,0 +1,57 @@
|
||||
<p>存在一棵无向连通树,树中有编号从 <code>0</code> 到 <code>n - 1</code> 的 <code>n</code> 个节点, 以及 <code>n - 1</code> 条边。</p>
|
||||
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,长度为 <code>n</code> ,其中 <code>nums[i]</code> 表示第 <code>i</code> 个节点的值。另给你一个二维整数数组 <code>edges</code> ,长度为 <code>n - 1</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示树中存在一条位于节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间的边。</p>
|
||||
|
||||
<p>删除树中两条 <strong>不同</strong> 的边以形成三个连通组件。对于一种删除边方案,定义如下步骤以计算其分数:</p>
|
||||
|
||||
<ol>
|
||||
<li>分别获取三个组件 <strong>每个</strong> 组件中所有节点值的异或值。</li>
|
||||
<li><strong>最大</strong> 异或值和 <strong>最小</strong> 异或值的 <strong>差值</strong> 就是这一种删除边方案的分数。</li>
|
||||
</ol>
|
||||
|
||||
<ul>
|
||||
<li>例如,三个组件的节点值分别是:<code>[4,5,7]</code>、<code>[1,9]</code> 和 <code>[3,3,3]</code> 。三个异或值分别是 <code>4 ^ 5 ^ 7 = <em><strong>6</strong></em></code>、<code>1 ^ 9 = <em><strong>8</strong></em></code> 和 <code>3 ^ 3 ^ 3 = <em><strong>3</strong></em></code> 。最大异或值是 <code>8</code> ,最小异或值是 <code>3</code> ,分数是 <code>8 - 3 = 5</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回在给定树上执行任意删除边方案可能的 <strong>最小</strong> 分数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png" style="width: 193px; height: 190px;">
|
||||
<pre><strong>输入:</strong>nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>上图展示了一种删除边方案。
|
||||
- 第 1 个组件的节点是 [1,3,4] ,值是 [5,4,11] 。异或值是 5 ^ 4 ^ 11 = 10 。
|
||||
- 第 2 个组件的节点是 [0] ,值是 [1] 。异或值是 1 = 1 。
|
||||
- 第 3 个组件的节点是 [2] ,值是 [5] 。异或值是 5 = 5 。
|
||||
分数是最大异或值和最小异或值的差值,10 - 1 = 9 。
|
||||
可以证明不存在分数比 9 小的删除边方案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png" style="width: 287px; height: 150px;">
|
||||
<pre><strong>输入:</strong>nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>上图展示了一种删除边方案。
|
||||
- 第 1 个组件的节点是 [3,4] ,值是 [4,4] 。异或值是 4 ^ 4 = 0 。
|
||||
- 第 2 个组件的节点是 [1,0] ,值是 [5,5] 。异或值是 5 ^ 5 = 0 。
|
||||
- 第 3 个组件的节点是 [2,5] ,值是 [2,2] 。异或值是 2 ^ 2 = 0 。
|
||||
分数是最大异或值和最小异或值的差值,0 - 0 = 0 。
|
||||
无法获得比 0 更小的分数 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> 表示一棵有效的树</li>
|
||||
</ul>
|
43
leetcode-cn/problem (Chinese)/兼具大小写的最好英文字母 [greatest-english-letter-in-upper-and-lower-case].html
Normal file
43
leetcode-cn/problem (Chinese)/兼具大小写的最好英文字母 [greatest-english-letter-in-upper-and-lower-case].html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>给你一个由英文字母组成的字符串 <code>s</code> ,请你找出并返回 <code>s</code> 中的 <strong>最好</strong> 英文字母。返回的字母必须为大写形式。如果不存在满足条件的字母,则返回一个空字符串。</p>
|
||||
|
||||
<p><strong>最好</strong> 英文字母的大写和小写形式必须 <strong>都</strong> 在 <code>s</code> 中出现。</p>
|
||||
|
||||
<p>英文字母 <code>b</code> 比另一个英文字母 <code>a</code> <strong>更好</strong> 的前提是:英文字母表中,<code>b</code> 在 <code>a</code> 之 <strong>后</strong> 出现。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "l<em><strong>Ee</strong></em>TcOd<em><strong>E</strong></em>"
|
||||
<strong>输出:</strong>"E"
|
||||
<strong>解释:</strong>
|
||||
字母 'E' 是唯一一个大写和小写形式都出现的字母。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "a<em><strong>rR</strong></em>AzFif"
|
||||
<strong>输出:</strong>"R"
|
||||
<strong>解释:</strong>
|
||||
字母 'R' 是大写和小写形式都出现的最好英文字母。
|
||||
注意 'A' 和 'F' 的大写和小写形式也都出现了,但是 'R' 比 'F' 和 'A' 更好。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "AbCdEfGhIjK"
|
||||
<strong>输出:</strong>""
|
||||
<strong>解释:</strong>
|
||||
不存在大写和小写形式都出现的字母。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 由小写和大写英文字母组成</li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>如果一个正方形矩阵满足下述 <strong>全部</strong> 条件,则称之为一个 <strong>X 矩阵</strong> :</p>
|
||||
|
||||
<ol>
|
||||
<li>矩阵对角线上的所有元素都 <strong>不是 0</strong></li>
|
||||
<li>矩阵中所有其他元素都是 <strong>0</strong></li>
|
||||
</ol>
|
||||
|
||||
<p>给你一个大小为 <code>n x n</code> 的二维整数数组 <code>grid</code> ,表示一个正方形矩阵。如果<em> </em><code>grid</code><em> </em>是一个 <strong>X 矩阵 </strong>,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg" style="width: 311px; height: 320px;">
|
||||
<pre><strong>输入:</strong>grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>矩阵如上图所示。
|
||||
X 矩阵应该满足:绿色元素(对角线上)都不是 0 ,红色元素都是 0 。
|
||||
因此,grid 是一个 X 矩阵。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg" style="width: 238px; height: 246px;">
|
||||
<pre><strong>输入:</strong>grid = [[5,7,0],[0,3,1],[0,5,0]]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>矩阵如上图所示。
|
||||
X 矩阵应该满足:绿色元素(对角线上)都不是 0 ,红色元素都是 0 。
|
||||
因此,grid 不是一个 X 矩阵。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 100</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,59 @@
|
||||
<p>给你两个整数 <code>m</code> 和 <code>n</code> ,分别表示一块矩形木块的高和宽。同时给你一个二维整数数组 <code>prices</code> ,其中 <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> 表示你可以以 <code>price<sub>i</sub></code> 元的价格卖一块高为 <code>h<sub>i</sub></code> 宽为 <code>w<sub>i</sub></code> 的矩形木块。</p>
|
||||
|
||||
<p>每一次操作中,你必须按下述方式之一执行切割操作,以得到两块更小的矩形木块:</p>
|
||||
|
||||
<ul>
|
||||
<li>沿垂直方向按高度 <strong>完全</strong> 切割木块,或</li>
|
||||
<li>沿水平方向按宽度 <strong>完全</strong> 切割木块</li>
|
||||
</ul>
|
||||
|
||||
<p>在将一块木块切成若干小木块后,你可以根据 <code>prices</code> 卖木块。你可以卖多块同样尺寸的木块。你不需要将所有小木块都卖出去。你 <strong>不能</strong> 旋转切好后木块的高和宽。</p>
|
||||
|
||||
<p>请你返回切割一块大小为<em> </em><code>m x n</code><em> </em>的木块后,能得到的 <strong>最多</strong> 钱数。</p>
|
||||
|
||||
<p>注意你可以切割木块任意次。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex1.png" style="width: 239px; height: 150px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
|
||||
<b>输出:</b>19
|
||||
<b>解释:</b>上图展示了一个可行的方案。包括:
|
||||
- 2 块 2 x 2 的小木块,售出 2 * 7 = 14 元。
|
||||
- 1 块 2 x 1 的小木块,售出 1 * 3 = 3 元。
|
||||
- 1 块 1 x 4 的小木块,售出 1 * 2 = 2 元。
|
||||
总共售出 14 + 3 + 2 = 19 元。
|
||||
19 元是最多能得到的钱数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex2new.png" style="width: 250px; height: 175px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
|
||||
<b>输出:</b>32
|
||||
<b>解释:</b>上图展示了一个可行的方案。包括:
|
||||
- 3 块 3 x 2 的小木块,售出 3 * 10 = 30 元。
|
||||
- 1 块 1 x 4 的小木块,售出 1 * 2 = 2 元。
|
||||
总共售出 30 + 2 = 32 元。
|
||||
32 元是最多能得到的钱数。
|
||||
注意我们不能旋转 1 x 4 的木块来得到 4 x 1 的木块。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>1 <= prices.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>prices[i].length == 3</code></li>
|
||||
<li><code>1 <= h<sub>i</sub> <= m</code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= n</code></li>
|
||||
<li><code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li>所有 <code>(h<sub>i</sub>, w<sub>i</sub>)</code> <strong>互不相同</strong> 。</li>
|
||||
</ul>
|
40
leetcode-cn/problem (Chinese)/小于等于 K 的最长二进制子序列 [longest-binary-subsequence-less-than-or-equal-to-k].html
Normal file
40
leetcode-cn/problem (Chinese)/小于等于 K 的最长二进制子序列 [longest-binary-subsequence-less-than-or-equal-to-k].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>给你一个二进制字符串 <code>s</code> 和一个正整数 <code>k</code> 。</p>
|
||||
|
||||
<p>请你返回 <code>s</code> 的 <strong>最长</strong> 子序列,且该子序列对应的 <strong>二进制</strong> 数字小于等于 <code>k</code> 。</p>
|
||||
|
||||
<p>注意:</p>
|
||||
|
||||
<ul>
|
||||
<li>子序列可以有 <strong>前导 0</strong> 。</li>
|
||||
<li>空字符串视为 <code>0</code> 。</li>
|
||||
<li><strong>子序列</strong> 是指从一个字符串中删除零个或者多个字符后,不改变顺序得到的剩余字符序列。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "1001010", k = 5
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>s 中小于等于 5 的最长子序列是 "00010" ,对应的十进制数字是 2 。
|
||||
注意 "00100" 和 "00101" 也是可行的最长子序列,十进制分别对应 4 和 5 。
|
||||
最长子序列的长度为 5 ,所以返回 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "00101001", k = 1
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>"000001" 是 s 中小于等于 1 的最长子序列,对应的十进制数字是 1 。
|
||||
最长子序列的长度为 6 ,所以返回 6 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,53 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ,长度都是 <code>n</code> 。</p>
|
||||
|
||||
<p>你可以选择两个整数 <code>left</code> 和 <code>right</code> ,其中 <code>0 <= left <= right < n</code> ,接着 <strong>交换</strong> 两个子数组 <code>nums1[left...right]</code> 和 <code>nums2[left...right]</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,设 <code>nums1 = [1,2,3,4,5]</code> 和 <code>nums2 = [11,12,13,14,15]</code> ,整数选择 <code>left = 1</code> 和 <code>right = 2</code>,那么 <code>nums1</code> 会变为 <code>[1,<strong><em>12</em>,<em>13</em></strong>,4,5]</code> 而 <code>nums2</code> 会变为 <code>[11,<em><strong>2,3</strong></em>,14,15]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>你可以选择执行上述操作 <strong>一次</strong> 或不执行任何操作。</p>
|
||||
|
||||
<p>数组的 <strong>分数</strong> 取 <code>sum(nums1)</code> 和 <code>sum(nums2)</code> 中的最大值,其中 <code>sum(arr)</code> 是数组 <code>arr</code> 中所有元素之和。</p>
|
||||
|
||||
<p>返回 <strong>可能的最大分数</strong> 。</p>
|
||||
|
||||
<p><strong>子数组 </strong>是数组中连续的一个元素序列。<code>arr[left...right]</code> 表示子数组包含 <code>nums</code> 中下标 <code>left</code> 和 <code>right</code> 之间的元素<strong>(含</strong> 下标 <code>left</code> 和 <code>right</code> 对应元素<strong>)</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [60,60,60], nums2 = [10,90,10]
|
||||
<strong>输出:</strong>210
|
||||
<strong>解释:</strong>选择 left = 1 和 right = 1 ,得到 nums1 = [60,<em><strong>90</strong></em>,60] 和 nums2 = [10,<em><strong>60</strong></em>,10] 。
|
||||
分数为 max(sum(nums1), sum(nums2)) = max(210, 80) = 210 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
|
||||
<strong>输出:</strong>220
|
||||
<strong>解释:</strong>选择 left = 3 和 right = 4 ,得到 nums1 = [20,40,20,<em><strong>40,20</strong></em>] 和 nums2 = [50,20,50,<em><strong>70,30</strong></em>] 。
|
||||
分数为 max(sum(nums1), sum(nums2)) = max(140, 220) = 220 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [7,11,13], nums2 = [1,1,1]
|
||||
<strong>输出:</strong>31
|
||||
<strong>解释:</strong>选择不交换任何子数组。
|
||||
分数为 max(sum(nums1), sum(nums2)) = max(31, 3) = 31 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。一次操作中,选择 <strong>任意</strong> 非负整数 <code>x</code> 和一个下标 <code>i</code> ,<strong>更新</strong> <code>nums[i]</code> 为 <code>nums[i] AND (nums[i] XOR x)</code> 。</p>
|
||||
|
||||
<p>注意,<code>AND</code> 是逐位与运算,<code>XOR</code> 是逐位异或运算。</p>
|
||||
|
||||
<p>请你执行 <strong>任意次</strong> 更新操作,并返回 <code>nums</code> 中所有元素 <strong>最大</strong> 逐位异或和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,2,4,6]
|
||||
<b>输出:</b>7
|
||||
<b>解释:</b>选择 x = 4 和 i = 3 进行操作,num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2 。
|
||||
现在,nums = [3, 2, 4, 2] 且所有元素逐位异或得到 3 XOR 2 XOR 4 XOR 2 = 7 。
|
||||
可知 7 是能得到的最大逐位异或和。
|
||||
注意,其他操作可能也能得到逐位异或和 7 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,2,3,9,2]
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>执行 0 次操作。
|
||||
所有元素的逐位异或和为 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11 。
|
||||
可知 11 是能得到的最大逐位异或和。</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>8</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>一条街道上共有 <code>n * 2</code> 个 <strong>地块</strong> ,街道的两侧各有 <code>n</code> 个地块。每一边的地块都按从 <code>1</code> 到 <code>n</code> 编号。每个地块上都可以放置一所房子。</p>
|
||||
|
||||
<p>现要求街道同一侧不能存在两所房子相邻的情况,请你计算并返回放置房屋的方式数目。由于答案可能很大,需要对 <code>10<sup>9</sup> + 7</code> 取余后再返回。</p>
|
||||
|
||||
<p>注意,如果一所房子放置在这条街某一侧上的第 <code>i</code> 个地块,不影响在另一侧的第 <code>i</code> 个地块放置房子。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
可能的放置方式:
|
||||
1. 所有地块都不放置房子。
|
||||
2. 一所房子放在街道的某一侧。
|
||||
3. 一所房子放在街道的另一侧。
|
||||
4. 放置两所房子,街道两侧各放置一所。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/12/arrangements.png" style="width: 500px; height: 500px;">
|
||||
<pre><strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>如上图所示,共有 9 种可能的放置方式。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
38
leetcode-cn/problem (Chinese)/统计无向图中无法互相到达点对数 [count-unreachable-pairs-of-nodes-in-an-undirected-graph].html
Normal file
38
leetcode-cn/problem (Chinese)/统计无向图中无法互相到达点对数 [count-unreachable-pairs-of-nodes-in-an-undirected-graph].html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>给你一个整数 <code>n</code> ,表示一张<strong> 无向图</strong> 中有 <code>n</code> 个节点,编号为 <code>0</code> 到 <code>n - 1</code> 。同时给你一个二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条 <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/05/05/tc-3.png" style="width: 267px; height: 169px;"></p>
|
||||
|
||||
<pre><b>输入:</b>n = 3, edges = [[0,1],[0,2],[1,2]]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>所有点都能互相到达,意味着没有点对无法互相到达,所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-2.png" style="width: 295px; height: 269px;"></p>
|
||||
|
||||
<pre><b>输入:</b>n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>总共有 14 个点对互相无法到达:
|
||||
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]]
|
||||
所以我们返回 14 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>不会有重复边。</li>
|
||||
</ul>
|
39
leetcode-cn/problem (Chinese)/统计星号 [count-asterisks].html
Normal file
39
leetcode-cn/problem (Chinese)/统计星号 [count-asterisks].html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>给你一个字符串 <code>s</code> ,每 <strong>两个</strong> 连续竖线 <code>'|'</code> 为 <strong>一对</strong> 。换言之,第一个和第二个 <code>'|'</code> 为一对,第三个和第四个 <code>'|'</code> 为一对,以此类推。</p>
|
||||
|
||||
<p>请你返回 <strong>不在</strong> 竖线对之间,<code>s</code> 中 <code>'*'</code> 的数目。</p>
|
||||
|
||||
<p><strong>注意</strong>,每个竖线 <code>'|'</code> 都会 <strong>恰好</strong> 属于一个对。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "l|*e*et|c**o|*de|"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>不在竖线对之间的字符加粗加斜体后,得到字符串:"<strong><em>l</em></strong>|*e*et|<strong><em>c**o</em></strong>|*de|" 。
|
||||
第一和第二条竖线 '|' 之间的字符不计入答案。
|
||||
同时,第三条和第四条竖线 '|' 之间的字符也不计入答案。
|
||||
不在竖线对之间总共有 2 个星号,所以我们返回 2 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "iamprogrammer"
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>在这个例子中,s 中没有星号。所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "yo|uar|e**|b|e***au|tifu|l"
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>需要考虑的字符加粗加斜体后:"<strong><em>yo</em></strong>|uar|<strong><em>e**</em></strong>|b|<strong><em>e***au</em></strong>|tifu|<strong><em>l</em></strong>" 。不在竖线对之间总共有 5 个星号。所以我们返回 5 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母,竖线 <code>'|'</code> 和星号 <code>'*'</code> 。</li>
|
||||
<li><code>s</code> 包含 <strong>偶数</strong> 个竖线 <code>'|'</code> 。</li>
|
||||
</ul>
|
39
leetcode-cn/problem (English)/不同骰子序列的数目(English) [number-of-distinct-roll-sequences].html
Normal file
39
leetcode-cn/problem (English)/不同骰子序列的数目(English) [number-of-distinct-roll-sequences].html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given an integer <code>n</code>. You roll a fair 6-sided dice <code>n</code> times. Determine the total number of <strong>distinct</strong> sequences of rolls possible such that the following conditions are satisfied:</p>
|
||||
|
||||
<ol>
|
||||
<li>The <strong>greatest common divisor</strong> of any <strong>adjacent</strong> values in the sequence is equal to <code>1</code>.</li>
|
||||
<li>There is <strong>at least</strong> a gap of <code>2</code> rolls between <strong>equal</strong> valued rolls. More formally, if the value of the <code>i<sup>th</sup></code> roll is <strong>equal</strong> to the value of the <code>j<sup>th</sup></code> roll, then <code>abs(i - j) > 2</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the<strong> total number</strong> of distinct sequences possible</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Two sequences are considered distinct if at least one element is different.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 184
|
||||
<strong>Explanation:</strong> Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc.
|
||||
Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6).
|
||||
(1, 2, 1, 3) is invalid since the first and third roll have an equal value and abs(1 - 3) = 2 (i and j are 1-indexed).
|
||||
(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3.
|
||||
There are a total of 184 distinct sequences possible, so we return 184.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> Some of the possible sequences are (1, 2), (2, 1), (3, 2).
|
||||
Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1.
|
||||
There are a total of 22 distinct sequences possible, so we return 22.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
51
leetcode-cn/problem (English)/个位数字为 K 的整数之和(English) [sum-of-numbers-with-units-digit-k].html
Normal file
51
leetcode-cn/problem (English)/个位数字为 K 的整数之和(English) [sum-of-numbers-with-units-digit-k].html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>Given two integers <code>num</code> and <code>k</code>, consider a set of positive integers with the following properties:</p>
|
||||
|
||||
<ul>
|
||||
<li>The units digit of each integer is <code>k</code>.</li>
|
||||
<li>The sum of the integers is <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible size of such a set, or </em><code>-1</code><em> if no such set exists.</em></p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>The set can contain multiple instances of the same integer, and the sum of an empty set is considered <code>0</code>.</li>
|
||||
<li>The <strong>units digit</strong> of a number is the rightmost digit of the number.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 58, k = 9
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
|
||||
Another valid set is [19,39].
|
||||
It can be shown that 2 is the minimum possible size of a valid set.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 37, k = 2
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 0, k = 7
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The sum of an empty set is considered 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num <= 3000</code></li>
|
||||
<li><code>0 <= k <= 9</code></li>
|
||||
</ul>
|
57
leetcode-cn/problem (English)/从树中删除边的最小分数(English) [minimum-score-after-removals-on-a-tree].html
Normal file
57
leetcode-cn/problem (English)/从树中删除边的最小分数(English) [minimum-score-after-removals-on-a-tree].html
Normal file
@ -0,0 +1,57 @@
|
||||
<p>There is an undirected connected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> where <code>nums[i]</code> represents the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Remove two <strong>distinct</strong> edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:</p>
|
||||
|
||||
<ol>
|
||||
<li>Get the XOR of all the values of the nodes for <strong>each</strong> of the three components respectively.</li>
|
||||
<li>The <strong>difference</strong> between the <strong>largest</strong> XOR value and the <strong>smallest</strong> XOR value is the <strong>score</strong> of the pair.</li>
|
||||
</ol>
|
||||
|
||||
<ul>
|
||||
<li>For example, say the three components have the node values: <code>[4,5,7]</code>, <code>[1,9]</code>, and <code>[3,3,3]</code>. The three XOR values are <code>4 ^ 5 ^ 7 = <u><strong>6</strong></u></code>, <code>1 ^ 9 = <u><strong>8</strong></u></code>, and <code>3 ^ 3 ^ 3 = <u><strong>3</strong></u></code>. The largest XOR value is <code>8</code> and the smallest XOR value is <code>3</code>. The score is then <code>8 - 3 = 5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> score of any possible pair of edge removals on the given tree</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png" style="width: 193px; height: 190px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The diagram above shows a way to make a pair of removals.
|
||||
- The 1<sup>st</sup> component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10.
|
||||
- The 2<sup>nd</sup> component has node [0] with value [1]. Its XOR value is 1 = 1.
|
||||
- The 3<sup>rd</sup> component has node [2] with value [5]. Its XOR value is 5 = 5.
|
||||
The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9.
|
||||
It can be shown that no other pair of removals will obtain a smaller score than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png" style="width: 287px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The diagram above shows a way to make a pair of removals.
|
||||
- The 1<sup>st</sup> component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0.
|
||||
- The 2<sup>nd</sup> component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0.
|
||||
- The 3<sup>rd</sup> component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0.
|
||||
The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0.
|
||||
We cannot obtain a smaller score than 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/兼具大小写的最好英文字母(English) [greatest-english-letter-in-upper-and-lower-case].html
Normal file
40
leetcode-cn/problem (English)/兼具大小写的最好英文字母(English) [greatest-english-letter-in-upper-and-lower-case].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Given a string of English letters <code>s</code>, return <em>the <strong>greatest </strong>English letter which occurs as <strong>both</strong> a lowercase and uppercase letter in</em> <code>s</code>. The returned letter should be in <strong>uppercase</strong>. If no such letter exists, return <em>an empty string</em>.</p>
|
||||
|
||||
<p>An English letter <code>b</code> is <strong>greater</strong> than another letter <code>a</code> if <code>b</code> appears <strong>after</strong> <code>a</code> in the English alphabet.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "l<strong><u>Ee</u></strong>TcOd<u><strong>E</strong></u>"
|
||||
<strong>Output:</strong> "E"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'E' is the only letter to appear in both lower and upper case.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "a<strong><u>rR</u></strong>AzFif"
|
||||
<strong>Output:</strong> "R"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'R' is the greatest letter to appear in both lower and upper case.
|
||||
Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "AbCdEfGhIjK"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong>
|
||||
There is no letter that appears in both lower and upper case.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of lowercase and uppercase English letters.</li>
|
||||
</ul>
|
38
leetcode-cn/problem (English)/判断矩阵是否是一个 X 矩阵(English) [check-if-matrix-is-x-matrix].html
Normal file
38
leetcode-cn/problem (English)/判断矩阵是否是一个 X 矩阵(English) [check-if-matrix-is-x-matrix].html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>A square matrix is said to be an <strong>X-Matrix</strong> if <strong>both</strong> of the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li>All the elements in the diagonals of the matrix are <strong>non-zero</strong>.</li>
|
||||
<li>All other elements are 0.</li>
|
||||
</ol>
|
||||
|
||||
<p>Given a 2D integer array <code>grid</code> of size <code>n x n</code> representing a square matrix, return <code>true</code><em> if </em><code>grid</code><em> is an X-Matrix</em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg" style="width: 311px; height: 320px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Refer to the diagram above.
|
||||
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
|
||||
Thus, grid is an X-Matrix.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg" style="width: 238px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[5,7,0],[0,3,1],[0,5,0]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Refer to the diagram above.
|
||||
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
|
||||
Thus, grid is not an X-Matrix.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 100</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given two integers <code>m</code> and <code>n</code> that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array <code>prices</code>, where <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> indicates you can sell a rectangular piece of wood of height <code>h<sub>i</sub></code> and width <code>w<sub>i</sub></code> for <code>price<sub>i</sub></code> dollars.</p>
|
||||
|
||||
<p>To cut a piece of wood, you must make a vertical or horizontal cut across the <strong>entire</strong> height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to <code>prices</code>. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you <strong>cannot</strong> rotate a piece to swap its height and width.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> money you can earn after cutting an </em><code>m x n</code><em> piece of wood</em>.</p>
|
||||
|
||||
<p>Note that you can cut the piece of wood as many times as you want.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex1.png" style="width: 239px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
|
||||
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
|
||||
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
|
||||
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
|
||||
This obtains a total of 14 + 3 + 2 = 19 money earned.
|
||||
It can be shown that 19 is the maximum amount of money that can be earned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex2new.png" style="width: 250px; height: 175px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
|
||||
<strong>Output:</strong> 32
|
||||
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
|
||||
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 = 30.
|
||||
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
|
||||
This obtains a total of 30 + 2 = 32 money earned.
|
||||
It can be shown that 32 is the maximum amount of money that can be earned.
|
||||
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>1 <= prices.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>prices[i].length == 3</code></li>
|
||||
<li><code>1 <= h<sub>i</sub> <= m</code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= n</code></li>
|
||||
<li><code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li>All the shapes of wood <code>(h<sub>i</sub>, w<sub>i</sub>)</code> are pairwise <strong>distinct</strong>.</li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/小于等于 K 的最长二进制子序列(English) [longest-binary-subsequence-less-than-or-equal-to-k].html
Normal file
40
leetcode-cn/problem (English)/小于等于 K 的最长二进制子序列(English) [longest-binary-subsequence-less-than-or-equal-to-k].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a binary string <code>s</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> subsequence of </em><code>s</code><em> that makes up a <strong>binary</strong> number less than or equal to</em> <code>k</code>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>The subsequence can contain <strong>leading zeroes</strong>.</li>
|
||||
<li>The empty string is considered to be equal to <code>0</code>.</li>
|
||||
<li>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001010", k = 5
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
|
||||
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
|
||||
The length of this subsequence is 5, so 5 is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "00101001", k = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
|
||||
The length of this subsequence is 6, so 6 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
51
leetcode-cn/problem (English)/拼接数组的最大分数(English) [maximum-score-of-spliced-array].html
Normal file
51
leetcode-cn/problem (English)/拼接数组的最大分数(English) [maximum-score-of-spliced-array].html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>
|
||||
|
||||
<p>You can choose two integers <code>left</code> and <code>right</code> where <code>0 <= left <= right < n</code> and <strong>swap</strong> the subarray <code>nums1[left...right]</code> with the subarray <code>nums2[left...right]</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums1 = [1,2,3,4,5]</code> and <code>nums2 = [11,12,13,14,15]</code> and you choose <code>left = 1</code> and <code>right = 2</code>, <code>nums1</code> becomes <code>[1,<strong><u>12,13</u></strong>,4,5]</code> and <code>nums2</code> becomes <code>[11,<strong><u>2,3</u></strong>,14,15]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You may choose to apply the mentioned operation <strong>once</strong> or not do anything.</p>
|
||||
|
||||
<p>The <strong>score</strong> of the arrays is the <strong>maximum</strong> of <code>sum(nums1)</code> and <code>sum(nums2)</code>, where <code>sum(arr)</code> is the sum of all the elements in the array <code>arr</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible score</strong></em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array. <code>arr[left...right]</code> denotes the subarray that contains the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [60,60,60], nums2 = [10,90,10]
|
||||
<strong>Output:</strong> 210
|
||||
<strong>Explanation:</strong> Choosing left = 1 and right = 1, we have nums1 = [60,<u><strong>90</strong></u>,60] and nums2 = [10,<u><strong>60</strong></u>,10].
|
||||
The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
|
||||
<strong>Output:</strong> 220
|
||||
<strong>Explanation:</strong> Choosing left = 3, right = 4, we have nums1 = [20,40,20,<u><strong>40,20</strong></u>] and nums2 = [50,20,50,<u><strong>70,30</strong></u>].
|
||||
The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [7,11,13], nums2 = [1,1,1]
|
||||
<strong>Output:</strong> 31
|
||||
<strong>Explanation:</strong> We choose not to swap any subarray.
|
||||
The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, select <strong>any</strong> non-negative integer <code>x</code> and an index <code>i</code>, then <strong>update</strong> <code>nums[i]</code> to be equal to <code>nums[i] AND (nums[i] XOR x)</code>.</p>
|
||||
|
||||
<p>Note that <code>AND</code> is the bitwise AND operation and <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible bitwise XOR of all elements of </em><code>nums</code><em> after applying the operation <strong>any number</strong> of times</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,4,6]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
|
||||
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
|
||||
It can be shown that 7 is the maximum possible bitwise XOR.
|
||||
Note that other operations may be used to achieve a bitwise XOR of 7.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,9,2]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> Apply the operation zero times.
|
||||
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
|
||||
It can be shown that 11 is the maximum possible bitwise XOR.</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>8</sup></code></li>
|
||||
</ul>
|
34
leetcode-cn/problem (English)/统计放置房子的方式数(English) [count-number-of-ways-to-place-houses].html
Normal file
34
leetcode-cn/problem (English)/统计放置房子的方式数(English) [count-number-of-ways-to-place-houses].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>There is a street with <code>n * 2</code> <strong>plots</strong>, where there are <code>n</code> plots on each side of the street. The plots on each side are numbered from <code>1</code> to <code>n</code>. On each plot, a house can be placed.</p>
|
||||
|
||||
<p>Return <em>the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that if a house is placed on the <code>i<sup>th</sup></code> plot on one side of the street, a house can also be placed on the <code>i<sup>th</sup></code> plot on the other side of the street.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
Possible arrangements:
|
||||
1. All plots are empty.
|
||||
2. A house is placed on one side of the street.
|
||||
3. A house is placed on the other side of the street.
|
||||
4. Two houses are placed, one on each side of the street.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/12/arrangements.png" style="width: 500px; height: 500px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The 9 possible arrangements are shown in the diagram above.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
34
leetcode-cn/problem (English)/统计无向图中无法互相到达点对数(English) [count-unreachable-pairs-of-nodes-in-an-undirected-graph].html
Normal file
34
leetcode-cn/problem (English)/统计无向图中无法互相到达点对数(English) [count-unreachable-pairs-of-nodes-in-an-undirected-graph].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of pairs</strong> of different nodes that are <strong>unreachable</strong> from each other</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-3.png" style="width: 267px; height: 169px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[0,2],[1,2]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-2.png" style="width: 295px; height: 269px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> There are 14 pairs of nodes that are unreachable from each other:
|
||||
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
|
||||
Therefore, we return 14.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a string <code>s</code>, where every <strong>two</strong> consecutive vertical bars <code>'|'</code> are grouped into a <strong>pair</strong>. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> <code>'|'</code> make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> <code>'|'</code> make a pair, and so forth.</p>
|
||||
|
||||
<p>Return <em>the number of </em><code>'*'</code><em> in </em><code>s</code><em>, <strong>excluding</strong> the </em><code>'*'</code><em> between each pair of </em><code>'|'</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that each <code>'|'</code> will belong to <strong>exactly</strong> one pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "l|*e*et|c**o|*de|"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The considered characters are underlined: "<u>l</u>|*e*et|<u>c**o</u>|*de|".
|
||||
The characters between the first and second '|' are excluded from the answer.
|
||||
Also, the characters between the third and fourth '|' are excluded from the answer.
|
||||
There are 2 asterisks considered. Therefore, we return 2.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "iamprogrammer"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> In this example, there are no asterisks in s. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "yo|uar|e**|b|e***au|tifu|l"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The considered characters are underlined: "<u>yo</u>|uar|<u>e**</u>|b|<u>e***au</u>|tifu|<u>l</u>". There are 5 asterisks considered. Therefore, we return 5.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters, vertical bars <code>'|'</code>, and asterisks <code>'*'</code>.</li>
|
||||
<li><code>s</code> contains an <strong>even</strong> number of vertical bars <code>'|'</code>.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
60
leetcode/originData/[no content]the-first-day-of-the-maximum-recorded-degree-in-each-city.json
Normal file
60
leetcode/originData/[no content]the-first-day-of-the-maximum-recorded-degree-in-each-city.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2446",
|
||||
"questionFrontendId": "2314",
|
||||
"boundTopicId": null,
|
||||
"title": "The First Day of the Maximum Recorded Degree in Each City",
|
||||
"titleSlug": "the-first-day-of-the-maximum-recorded-degree-in-each-city",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 5,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\": {\"Weather\": [\"city_id\", \"day\", \"degree\"]}, \"rows\": {\"Weather\": [[1, \"2022-01-07\", -12], [1, \"2022-03-07\", 5], [1, \"2022-07-07\", 24], [2, \"2022-08-07\", 37], [2, \"2022-08-17\", 37], [3, \"2022-02-07\", -7], [3, \"2022-12-07\", -6]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"242\", \"totalSubmission\": \"306\", \"totalAcceptedRaw\": 242, \"totalSubmissionRaw\": 306, \"acRate\": \"79.1%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Weather\": [\"city_id\", \"day\", \"degree\"]}, \"rows\": {\"Weather\": [[1, \"2022-01-07\", -12], [1, \"2022-03-07\", 5], [1, \"2022-07-07\", 24], [2, \"2022-08-07\", 37], [2, \"2022-08-17\", 37], [3, \"2022-02-07\", -7], [3, \"2022-12-07\", -6]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Weather (city_id int, day date, degree int)\"\n ],\n \"mssql\": [\n \"Create table Weather (city_id int, day date, degree int)\"\n ],\n \"oraclesql\": [\n \"Create table Weather (city_id int, day date, degree int)\",\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 Weather (city_id int, day date, degree int)",
|
||||
"Truncate table Weather",
|
||||
"insert into Weather (city_id, day, degree) values ('1', '2022-01-07', '-12')",
|
||||
"insert into Weather (city_id, day, degree) values ('1', '2022-03-07', '5')",
|
||||
"insert into Weather (city_id, day, degree) values ('1', '2022-07-07', '24')",
|
||||
"insert into Weather (city_id, day, degree) values ('2', '2022-08-07', '37')",
|
||||
"insert into Weather (city_id, day, degree) values ('2', '2022-08-17', '37')",
|
||||
"insert into Weather (city_id, day, degree) values ('3', '2022-02-07', '-7')",
|
||||
"insert into Weather (city_id, day, degree) values ('3', '2022-12-07', '-6')"
|
||||
],
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
168
leetcode/originData/check-if-matrix-is-x-matrix.json
Normal file
168
leetcode/originData/check-if-matrix-is-x-matrix.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/count-asterisks.json
Normal file
162
leetcode/originData/count-asterisks.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/count-number-of-ways-to-place-houses.json
Normal file
163
leetcode/originData/count-number-of-ways-to-place-houses.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
169
leetcode/originData/maximum-score-of-spliced-array.json
Normal file
169
leetcode/originData/maximum-score-of-spliced-array.json
Normal file
File diff suppressed because one or more lines are too long
176
leetcode/originData/maximum-xor-after-operations.json
Normal file
176
leetcode/originData/maximum-xor-after-operations.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/minimum-score-after-removals-on-a-tree.json
Normal file
180
leetcode/originData/minimum-score-after-removals-on-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
169
leetcode/originData/number-of-distinct-roll-sequences.json
Normal file
169
leetcode/originData/number-of-distinct-roll-sequences.json
Normal file
File diff suppressed because one or more lines are too long
175
leetcode/originData/selling-pieces-of-wood.json
Normal file
175
leetcode/originData/selling-pieces-of-wood.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/sum-of-numbers-with-units-digit-k.json
Normal file
180
leetcode/originData/sum-of-numbers-with-units-digit-k.json
Normal file
File diff suppressed because one or more lines are too long
38
leetcode/problem/check-if-matrix-is-x-matrix.html
Normal file
38
leetcode/problem/check-if-matrix-is-x-matrix.html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>A square matrix is said to be an <strong>X-Matrix</strong> if <strong>both</strong> of the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li>All the elements in the diagonals of the matrix are <strong>non-zero</strong>.</li>
|
||||
<li>All other elements are 0.</li>
|
||||
</ol>
|
||||
|
||||
<p>Given a 2D integer array <code>grid</code> of size <code>n x n</code> representing a square matrix, return <code>true</code><em> if </em><code>grid</code><em> is an X-Matrix</em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg" style="width: 311px; height: 320px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Refer to the diagram above.
|
||||
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
|
||||
Thus, grid is an X-Matrix.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg" style="width: 238px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[5,7,0],[0,3,1],[0,5,0]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Refer to the diagram above.
|
||||
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
|
||||
Thus, grid is not an X-Matrix.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 100</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
40
leetcode/problem/count-asterisks.html
Normal file
40
leetcode/problem/count-asterisks.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a string <code>s</code>, where every <strong>two</strong> consecutive vertical bars <code>'|'</code> are grouped into a <strong>pair</strong>. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> <code>'|'</code> make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> <code>'|'</code> make a pair, and so forth.</p>
|
||||
|
||||
<p>Return <em>the number of </em><code>'*'</code><em> in </em><code>s</code><em>, <strong>excluding</strong> the </em><code>'*'</code><em> between each pair of </em><code>'|'</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that each <code>'|'</code> will belong to <strong>exactly</strong> one pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "l|*e*et|c**o|*de|"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The considered characters are underlined: "<u>l</u>|*e*et|<u>c**o</u>|*de|".
|
||||
The characters between the first and second '|' are excluded from the answer.
|
||||
Also, the characters between the third and fourth '|' are excluded from the answer.
|
||||
There are 2 asterisks considered. Therefore, we return 2.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "iamprogrammer"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> In this example, there are no asterisks in s. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "yo|uar|e**|b|e***au|tifu|l"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The considered characters are underlined: "<u>yo</u>|uar|<u>e**</u>|b|<u>e***au</u>|tifu|<u>l</u>". There are 5 asterisks considered. Therefore, we return 5.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters, vertical bars <code>'|'</code>, and asterisks <code>'*'</code>.</li>
|
||||
<li><code>s</code> contains an <strong>even</strong> number of vertical bars <code>'|'</code>.</li>
|
||||
</ul>
|
34
leetcode/problem/count-number-of-ways-to-place-houses.html
Normal file
34
leetcode/problem/count-number-of-ways-to-place-houses.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>There is a street with <code>n * 2</code> <strong>plots</strong>, where there are <code>n</code> plots on each side of the street. The plots on each side are numbered from <code>1</code> to <code>n</code>. On each plot, a house can be placed.</p>
|
||||
|
||||
<p>Return <em>the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that if a house is placed on the <code>i<sup>th</sup></code> plot on one side of the street, a house can also be placed on the <code>i<sup>th</sup></code> plot on the other side of the street.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
Possible arrangements:
|
||||
1. All plots are empty.
|
||||
2. A house is placed on one side of the street.
|
||||
3. A house is placed on the other side of the street.
|
||||
4. Two houses are placed, one on each side of the street.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/12/arrangements.png" style="width: 500px; height: 500px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The 9 possible arrangements are shown in the diagram above.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of pairs</strong> of different nodes that are <strong>unreachable</strong> from each other</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-3.png" style="width: 267px; height: 169px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[0,2],[1,2]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-2.png" style="width: 295px; height: 269px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> There are 14 pairs of nodes that are unreachable from each other:
|
||||
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
|
||||
Therefore, we return 14.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>Given a string of English letters <code>s</code>, return <em>the <strong>greatest </strong>English letter which occurs as <strong>both</strong> a lowercase and uppercase letter in</em> <code>s</code>. The returned letter should be in <strong>uppercase</strong>. If no such letter exists, return <em>an empty string</em>.</p>
|
||||
|
||||
<p>An English letter <code>b</code> is <strong>greater</strong> than another letter <code>a</code> if <code>b</code> appears <strong>after</strong> <code>a</code> in the English alphabet.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "l<strong><u>Ee</u></strong>TcOd<u><strong>E</strong></u>"
|
||||
<strong>Output:</strong> "E"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'E' is the only letter to appear in both lower and upper case.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "a<strong><u>rR</u></strong>AzFif"
|
||||
<strong>Output:</strong> "R"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'R' is the greatest letter to appear in both lower and upper case.
|
||||
Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "AbCdEfGhIjK"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong>
|
||||
There is no letter that appears in both lower and upper case.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of lowercase and uppercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a binary string <code>s</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> subsequence of </em><code>s</code><em> that makes up a <strong>binary</strong> number less than or equal to</em> <code>k</code>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>The subsequence can contain <strong>leading zeroes</strong>.</li>
|
||||
<li>The empty string is considered to be equal to <code>0</code>.</li>
|
||||
<li>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001010", k = 5
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
|
||||
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
|
||||
The length of this subsequence is 5, so 5 is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "00101001", k = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
|
||||
The length of this subsequence is 6, so 6 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
51
leetcode/problem/maximum-score-of-spliced-array.html
Normal file
51
leetcode/problem/maximum-score-of-spliced-array.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>
|
||||
|
||||
<p>You can choose two integers <code>left</code> and <code>right</code> where <code>0 <= left <= right < n</code> and <strong>swap</strong> the subarray <code>nums1[left...right]</code> with the subarray <code>nums2[left...right]</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums1 = [1,2,3,4,5]</code> and <code>nums2 = [11,12,13,14,15]</code> and you choose <code>left = 1</code> and <code>right = 2</code>, <code>nums1</code> becomes <code>[1,<strong><u>12,13</u></strong>,4,5]</code> and <code>nums2</code> becomes <code>[11,<strong><u>2,3</u></strong>,14,15]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You may choose to apply the mentioned operation <strong>once</strong> or not do anything.</p>
|
||||
|
||||
<p>The <strong>score</strong> of the arrays is the <strong>maximum</strong> of <code>sum(nums1)</code> and <code>sum(nums2)</code>, where <code>sum(arr)</code> is the sum of all the elements in the array <code>arr</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible score</strong></em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array. <code>arr[left...right]</code> denotes the subarray that contains the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [60,60,60], nums2 = [10,90,10]
|
||||
<strong>Output:</strong> 210
|
||||
<strong>Explanation:</strong> Choosing left = 1 and right = 1, we have nums1 = [60,<u><strong>90</strong></u>,60] and nums2 = [10,<u><strong>60</strong></u>,10].
|
||||
The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
|
||||
<strong>Output:</strong> 220
|
||||
<strong>Explanation:</strong> Choosing left = 3, right = 4, we have nums1 = [20,40,20,<u><strong>40,20</strong></u>] and nums2 = [50,20,50,<u><strong>70,30</strong></u>].
|
||||
The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [7,11,13], nums2 = [1,1,1]
|
||||
<strong>Output:</strong> 31
|
||||
<strong>Explanation:</strong> We choose not to swap any subarray.
|
||||
The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
33
leetcode/problem/maximum-xor-after-operations.html
Normal file
33
leetcode/problem/maximum-xor-after-operations.html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, select <strong>any</strong> non-negative integer <code>x</code> and an index <code>i</code>, then <strong>update</strong> <code>nums[i]</code> to be equal to <code>nums[i] AND (nums[i] XOR x)</code>.</p>
|
||||
|
||||
<p>Note that <code>AND</code> is the bitwise AND operation and <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible bitwise XOR of all elements of </em><code>nums</code><em> after applying the operation <strong>any number</strong> of times</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,4,6]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
|
||||
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
|
||||
It can be shown that 7 is the maximum possible bitwise XOR.
|
||||
Note that other operations may be used to achieve a bitwise XOR of 7.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,9,2]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> Apply the operation zero times.
|
||||
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
|
||||
It can be shown that 11 is the maximum possible bitwise XOR.</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>8</sup></code></li>
|
||||
</ul>
|
57
leetcode/problem/minimum-score-after-removals-on-a-tree.html
Normal file
57
leetcode/problem/minimum-score-after-removals-on-a-tree.html
Normal file
@ -0,0 +1,57 @@
|
||||
<p>There is an undirected connected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> where <code>nums[i]</code> represents the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Remove two <strong>distinct</strong> edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:</p>
|
||||
|
||||
<ol>
|
||||
<li>Get the XOR of all the values of the nodes for <strong>each</strong> of the three components respectively.</li>
|
||||
<li>The <strong>difference</strong> between the <strong>largest</strong> XOR value and the <strong>smallest</strong> XOR value is the <strong>score</strong> of the pair.</li>
|
||||
</ol>
|
||||
|
||||
<ul>
|
||||
<li>For example, say the three components have the node values: <code>[4,5,7]</code>, <code>[1,9]</code>, and <code>[3,3,3]</code>. The three XOR values are <code>4 ^ 5 ^ 7 = <u><strong>6</strong></u></code>, <code>1 ^ 9 = <u><strong>8</strong></u></code>, and <code>3 ^ 3 ^ 3 = <u><strong>3</strong></u></code>. The largest XOR value is <code>8</code> and the smallest XOR value is <code>3</code>. The score is then <code>8 - 3 = 5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> score of any possible pair of edge removals on the given tree</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png" style="width: 193px; height: 190px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The diagram above shows a way to make a pair of removals.
|
||||
- The 1<sup>st</sup> component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10.
|
||||
- The 2<sup>nd</sup> component has node [0] with value [1]. Its XOR value is 1 = 1.
|
||||
- The 3<sup>rd</sup> component has node [2] with value [5]. Its XOR value is 5 = 5.
|
||||
The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9.
|
||||
It can be shown that no other pair of removals will obtain a smaller score than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png" style="width: 287px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The diagram above shows a way to make a pair of removals.
|
||||
- The 1<sup>st</sup> component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0.
|
||||
- The 2<sup>nd</sup> component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0.
|
||||
- The 3<sup>rd</sup> component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0.
|
||||
The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0.
|
||||
We cannot obtain a smaller score than 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
39
leetcode/problem/number-of-distinct-roll-sequences.html
Normal file
39
leetcode/problem/number-of-distinct-roll-sequences.html
Normal file
@ -0,0 +1,39 @@
|
||||
<p>You are given an integer <code>n</code>. You roll a fair 6-sided dice <code>n</code> times. Determine the total number of <strong>distinct</strong> sequences of rolls possible such that the following conditions are satisfied:</p>
|
||||
|
||||
<ol>
|
||||
<li>The <strong>greatest common divisor</strong> of any <strong>adjacent</strong> values in the sequence is equal to <code>1</code>.</li>
|
||||
<li>There is <strong>at least</strong> a gap of <code>2</code> rolls between <strong>equal</strong> valued rolls. More formally, if the value of the <code>i<sup>th</sup></code> roll is <strong>equal</strong> to the value of the <code>j<sup>th</sup></code> roll, then <code>abs(i - j) > 2</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the<strong> total number</strong> of distinct sequences possible</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Two sequences are considered distinct if at least one element is different.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 184
|
||||
<strong>Explanation:</strong> Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc.
|
||||
Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6).
|
||||
(1, 2, 1, 3) is invalid since the first and third roll have an equal value and abs(1 - 3) = 2 (i and j are 1-indexed).
|
||||
(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3.
|
||||
There are a total of 184 distinct sequences possible, so we return 184.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> Some of the possible sequences are (1, 2), (2, 1), (3, 2).
|
||||
Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1.
|
||||
There are a total of 22 distinct sequences possible, so we return 22.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
46
leetcode/problem/selling-pieces-of-wood.html
Normal file
46
leetcode/problem/selling-pieces-of-wood.html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given two integers <code>m</code> and <code>n</code> that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array <code>prices</code>, where <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> indicates you can sell a rectangular piece of wood of height <code>h<sub>i</sub></code> and width <code>w<sub>i</sub></code> for <code>price<sub>i</sub></code> dollars.</p>
|
||||
|
||||
<p>To cut a piece of wood, you must make a vertical or horizontal cut across the <strong>entire</strong> height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to <code>prices</code>. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you <strong>cannot</strong> rotate a piece to swap its height and width.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> money you can earn after cutting an </em><code>m x n</code><em> piece of wood</em>.</p>
|
||||
|
||||
<p>Note that you can cut the piece of wood as many times as you want.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex1.png" style="width: 239px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
|
||||
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
|
||||
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
|
||||
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
|
||||
This obtains a total of 14 + 3 + 2 = 19 money earned.
|
||||
It can be shown that 19 is the maximum amount of money that can be earned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex2new.png" style="width: 250px; height: 175px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
|
||||
<strong>Output:</strong> 32
|
||||
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
|
||||
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 = 30.
|
||||
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
|
||||
This obtains a total of 30 + 2 = 32 money earned.
|
||||
It can be shown that 32 is the maximum amount of money that can be earned.
|
||||
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>1 <= prices.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>prices[i].length == 3</code></li>
|
||||
<li><code>1 <= h<sub>i</sub> <= m</code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= n</code></li>
|
||||
<li><code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li>All the shapes of wood <code>(h<sub>i</sub>, w<sub>i</sub>)</code> are pairwise <strong>distinct</strong>.</li>
|
||||
</ul>
|
51
leetcode/problem/sum-of-numbers-with-units-digit-k.html
Normal file
51
leetcode/problem/sum-of-numbers-with-units-digit-k.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>Given two integers <code>num</code> and <code>k</code>, consider a set of positive integers with the following properties:</p>
|
||||
|
||||
<ul>
|
||||
<li>The units digit of each integer is <code>k</code>.</li>
|
||||
<li>The sum of the integers is <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible size of such a set, or </em><code>-1</code><em> if no such set exists.</em></p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>The set can contain multiple instances of the same integer, and the sum of an empty set is considered <code>0</code>.</li>
|
||||
<li>The <strong>units digit</strong> of a number is the rightmost digit of the number.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 58, k = 9
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
|
||||
Another valid set is [19,39].
|
||||
It can be shown that 2 is the minimum possible size of a valid set.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 37, k = 2
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 0, k = 7
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The sum of an empty set is considered 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num <= 3000</code></li>
|
||||
<li><code>0 <= k <= 9</code></li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user