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
b43009ddf3
commit
ee13c73307
README.md
leetcode-cn
origin-data.json
originData
1GxJYY.json600YaG.jsonKnLfVT.jsonXxZZjK.json[no content]maximize-total-tastiness-of-purchased-fruits.json[no content]merge-operations-to-turn-array-into-a-palindrome.jsonbitwise-xor-of-all-pairings.jsonkskhHQ.jsonlongest-uploaded-prefix.jsonmaximum-deletions-on-a-string.jsonmaximum-sum-of-an-hourglass.jsonminimize-xor.jsonnumber-of-common-factors.jsonnumber-of-pairs-satisfying-inequality.jsonrMeRt2.jsonremove-letter-to-equalize-frequency.json
problem (Chinese)
Hello LeetCode! [rMeRt2].md公因子的数目 [number-of-common-factors].html删除字符使频率相同 [remove-letter-to-equalize-frequency].html对字母串可执行的最大删除数 [maximum-deletions-on-a-string].html所有数对的异或和 [bitwise-xor-of-all-pairings].html最小 XOR [minimize-xor].html最小展台数量 [600YaG].md最长上传前缀 [longest-uploaded-prefix].html沙地治理 [XxZZjK].md沙漏的最大总和 [maximum-sum-of-an-hourglass].html满足不等式的数对数目 [number-of-pairs-satisfying-inequality].html美观的花束 [1GxJYY].md装饰树 [KnLfVT].md集水器 [kskhHQ].md
problem (English)
公因子的数目(English) [number-of-common-factors].html删除字符使频率相同(English) [remove-letter-to-equalize-frequency].html对字母串可执行的最大删除数(English) [maximum-deletions-on-a-string].html所有数对的异或和(English) [bitwise-xor-of-all-pairings].html最小 XOR(English) [minimize-xor].html最长上传前缀(English) [longest-uploaded-prefix].html沙漏的最大总和(English) [maximum-sum-of-an-hourglass].html满足不等式的数对数目(English) [number-of-pairs-satisfying-inequality].html
leetcode
origin-data.json
originData
[no content]maximize-total-tastiness-of-purchased-fruits.json[no content]merge-operations-to-turn-array-into-a-palindrome.jsonbitwise-xor-of-all-pairings.jsonlongest-uploaded-prefix.jsonmaximum-deletions-on-a-string.jsonmaximum-sum-of-an-hourglass.jsonminimize-xor.jsonnumber-of-common-factors.jsonnumber-of-pairs-satisfying-inequality.jsonremove-letter-to-equalize-frequency.json
problem
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.09.27**
|
||||
> 最后更新日期: **2022.10.07**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
162
leetcode-cn/originData/1GxJYY.json
Normal file
162
leetcode-cn/originData/1GxJYY.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode-cn/originData/600YaG.json
Normal file
162
leetcode-cn/originData/600YaG.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode-cn/originData/KnLfVT.json
Normal file
162
leetcode-cn/originData/KnLfVT.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode-cn/originData/XxZZjK.json
Normal file
162
leetcode-cn/originData/XxZZjK.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
69
leetcode-cn/originData/[no content]merge-operations-to-turn-array-into-a-palindrome.json
Normal file
69
leetcode-cn/originData/[no content]merge-operations-to-turn-array-into-a-palindrome.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/bitwise-xor-of-all-pairings.json
Normal file
183
leetcode-cn/originData/bitwise-xor-of-all-pairings.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode-cn/originData/kskhHQ.json
Normal file
162
leetcode-cn/originData/kskhHQ.json
Normal file
File diff suppressed because one or more lines are too long
206
leetcode-cn/originData/longest-uploaded-prefix.json
Normal file
206
leetcode-cn/originData/longest-uploaded-prefix.json
Normal file
File diff suppressed because one or more lines are too long
197
leetcode-cn/originData/maximum-deletions-on-a-string.json
Normal file
197
leetcode-cn/originData/maximum-deletions-on-a-string.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/maximum-sum-of-an-hourglass.json
Normal file
183
leetcode-cn/originData/maximum-sum-of-an-hourglass.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/minimize-xor.json
Normal file
177
leetcode-cn/originData/minimize-xor.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode-cn/originData/number-of-common-factors.json
Normal file
182
leetcode-cn/originData/number-of-common-factors.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
162
leetcode-cn/originData/rMeRt2.json
Normal file
162
leetcode-cn/originData/rMeRt2.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/remove-letter-to-equalize-frequency.json
Normal file
183
leetcode-cn/originData/remove-letter-to-equalize-frequency.json
Normal file
File diff suppressed because one or more lines are too long
42
leetcode-cn/problem (Chinese)/Hello LeetCode! [rMeRt2].md
Normal file
42
leetcode-cn/problem (Chinese)/Hello LeetCode! [rMeRt2].md
Normal file
@ -0,0 +1,42 @@
|
||||
力扣嘉年华同样准备了纪念品展位,参观者只需要集齐 `helloleetcode` 的 `13` 张字母卡片即可获得力扣纪念章。
|
||||
|
||||
在展位上有一些由字母卡片拼成的单词,`words[i][j]` 表示第 `i` 个单词的第 `j` 个字母。
|
||||
|
||||
你可以从这些单词中取出一些卡片,但每次拿取卡片都需要消耗游戏代币,规则如下:
|
||||
|
||||
- 从一个单词中取一个字母所需要的代币数量,为该字母左边和右边字母数量之积
|
||||
|
||||
- 可以从一个单词中多次取字母,每个字母仅可被取一次
|
||||
|
||||
> 例如:从 `example` 中取出字母 `a`,需要消耗代币 `2*4=8`,字母取出后单词变为 `exmple`;
|
||||
再从中取出字母 `m`,需要消耗代币 `2*3=6`,字母取出后单词变为 `exple`;
|
||||
|
||||
请返回取得 `helloleetcode` 这些字母需要消耗代币的 **最少** 数量。如果无法取得,返回 `-1`。
|
||||
|
||||
**注意:**
|
||||
- 取出字母的顺序没有要求
|
||||
- 取出的所有字母恰好可以拼成 `helloleetcode`
|
||||
|
||||
**示例 1:**
|
||||
>输入:`words = ["hold","engineer","cost","level"]`
|
||||
>
|
||||
>输出:`5`
|
||||
>
|
||||
>解释:最优方法为:
|
||||
>从 `hold` 依次取出 `h`、`o`、`l`、`d`, 代价均为 `0`
|
||||
>从 `engineer` 依次取出第 `1` 个 `e` 与最后一个 `e`, 代价为 `0` 和 `5*1=5`
|
||||
>从 `cost` 取出 `c`、`o`、`t`, 代价均为 `0`
|
||||
>从 `level` 依次取出 `l`、`l`、`e`、`e`, 代价均为 `0`
|
||||
>所有字母恰好可以拼成 `helloleetcode`,因此最小的代价为 `5`
|
||||
|
||||
**示例 2:**
|
||||
>输入:`words = ["hello","leetcode"]`
|
||||
>
|
||||
>输出:`0`
|
||||
|
||||
**提示:**
|
||||
+ `n == words.length`
|
||||
+ `m == words[i].length`
|
||||
+ `1 <= n <= 24`
|
||||
+ `1 <= m <= 8`
|
||||
+ `words[i][j]` 仅为小写字母
|
@ -0,0 +1,26 @@
|
||||
<p>给你两个正整数 <code>a</code> 和 <code>b</code> ,返回 <code>a</code> 和 <code>b</code> 的 <strong>公</strong> 因子的数目。</p>
|
||||
|
||||
<p>如果 <code>x</code> 可以同时整除 <code>a</code> 和 <code>b</code> ,则认为 <code>x</code> 是 <code>a</code> 和 <code>b</code> 的一个 <strong>公因子</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>a = 12, b = 6
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>12 和 6 的公因子是 1、2、3、6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>a = 25, b = 30
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>25 和 30 的公因子是 1、5 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> ,字符串只包含小写英文字母。你需要选择 <strong>一个</strong> 下标并 <strong>删除</strong> 下标处的字符,使得 <code>word</code> 中剩余每个字母出现 <strong>频率</strong> 相同。</p>
|
||||
|
||||
<p>如果删除一个字母后,<code>word</code> 中剩余所有字母的出现频率都相同,那么返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>字母 <code>x</code> 的 <strong>频率</strong><strong> </strong>是这个字母在字符串中出现的次数。</li>
|
||||
<li>你 <strong>必须</strong> 恰好删除一个字母,不能一个字母都不删除。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>word = "abcc"
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>选择下标 3 并删除该字母,word 变成 "abc" 且每个字母出现频率都为 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>word = "aazz"
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>我们必须删除一个字母,所以要么 "a" 的频率变为 1 且 "z" 的频率为 2 ,要么两个字母频率反过来。所以不可能让剩余所有字母出现频率相同。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,54 @@
|
||||
<p>给你一个仅由小写英文字母组成的字符串 <code>s</code> 。在一步操作中,你可以:</p>
|
||||
|
||||
<ul>
|
||||
<li>删除 <strong>整个字符串</strong> <code>s</code> ,或者</li>
|
||||
<li>对于满足 <code>1 <= i <= s.length / 2</code> 的任意 <code>i</code> ,如果 <code>s</code> 中的 <strong>前</strong> <code>i</code> 个字母和接下来的 <code>i</code> 个字母 <strong>相等</strong> ,删除 <strong>前</strong> <code>i</code> 个字母。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,如果 <code>s = "ababc"</code> ,那么在一步操作中,你可以删除 <code>s</code> 的前两个字母得到 <code>"abc"</code> ,因为 <code>s</code> 的前两个字母和接下来的两个字母都等于 <code>"ab"</code> 。</p>
|
||||
|
||||
<p>返回删除 <code>s</code> 所需的最大操作数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcabcdabc"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
- 删除前 3 个字母("abc"),因为它们和接下来 3 个字母相等。现在,s = "abcdabc"。
|
||||
- 删除全部字母。
|
||||
一共用了 2 步操作,所以返回 2 。可以证明 2 是所需的最大操作数。
|
||||
注意,在第二步操作中无法再次删除 "abc" ,因为 "abc" 的下一次出现并不是位于接下来的 3 个字母。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaabaab"
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
- 删除第一个字母("a"),因为它和接下来的字母相等。现在,s = "aabaab"。
|
||||
- 删除前 3 个字母("aab"),因为它们和接下来 3 个字母相等。现在,s = "aab"。
|
||||
- 删除第一个字母("a"),因为它和接下来的字母相等。现在,s = "ab"。
|
||||
- 删除全部字母。
|
||||
一共用了 4 步操作,所以返回 4 。可以证明 4 是所需的最大操作数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaaaa"
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>在每一步操作中,都可以仅删除 s 的第一个字母。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 4000</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的数组 <code>nums1</code> 和 <code>nums2</code> ,两个数组都只包含非负整数。请你求出另外一个数组 <code>nums3</code> ,包含 <code>nums1</code> 和 <code>nums2</code> 中 <strong>所有数对</strong> 的异或和(<code>nums1</code> 中每个整数都跟 <code>nums2</code> 中每个整数 <strong>恰好</strong> 匹配一次)。</p>
|
||||
|
||||
<p>请你返回 <code>nums3</code> 中所有整数的 <strong>异或和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [2,1,3], nums2 = [10,2,5,0]
|
||||
<b>输出:</b>13
|
||||
<strong>解释:</strong>
|
||||
一个可能的 nums3 数组是 [8,0,7,2,11,3,4,1,9,1,6,3] 。
|
||||
所有这些数字的异或和是 13 ,所以我们返回 13 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [1,2], nums2 = [3,4]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
所有数对异或和的结果分别为 nums1[0] ^ nums2[0] ,nums1[0] ^ nums2[1] ,nums1[1] ^ nums2[0] 和 nums1[1] ^ nums2[1] 。
|
||||
所以,一个可能的 nums3 数组是 [2,5,1,6] 。
|
||||
2 ^ 5 ^ 1 ^ 6 = 0 ,所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (Chinese)/最小 XOR [minimize-xor].html
Normal file
40
leetcode-cn/problem (Chinese)/最小 XOR [minimize-xor].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>给你两个正整数 <code>num1</code> 和 <code>num2</code> ,找出满足下述条件的整数 <code>x</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>x</code> 的置位数和 <code>num2</code> 相同,且</li>
|
||||
<li><code>x XOR num1</code> 的值 <strong>最小</strong></li>
|
||||
</ul>
|
||||
|
||||
<p>注意 <code>XOR</code> 是按位异或运算。</p>
|
||||
|
||||
<p>返回整数<em> </em><code>x</code> 。题目保证,对于生成的测试用例, <code>x</code> 是 <strong>唯一确定</strong> 的。</p>
|
||||
|
||||
<p>整数的 <strong>置位数</strong> 是其二进制表示中 <code>1</code> 的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num1 = 3, num2 = 5
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
num1 和 num2 的二进制表示分别是 0011 和 0101 。
|
||||
整数 <strong>3</strong> 的置位数与 num2 相同,且 <code>3 XOR 3 = 0</code> 是最小的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num1 = 1, num2 = 12
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
num1 和 num2 的二进制表示分别是 0001 和 1100 。
|
||||
整数 <strong>3</strong> 的置位数与 num2 相同,且 <code>3 XOR 1 = 2</code> 是最小的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1, num2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
27
leetcode-cn/problem (Chinese)/最小展台数量 [600YaG].md
Normal file
27
leetcode-cn/problem (Chinese)/最小展台数量 [600YaG].md
Normal file
@ -0,0 +1,27 @@
|
||||
力扣嘉年华将举办一系列展览活动,后勤部将负责为每场展览提供所需要的展台。
|
||||
已知后勤部得到了一份需求清单,记录了近期展览所需要的展台类型, `demand[i][j]` 表示第 `i` 天展览时第 `j` 个展台的类型。
|
||||
在满足每一天展台需求的基础上,请返回后勤部需要准备的 **最小** 展台数量。
|
||||
|
||||
**注意:**
|
||||
- 同一展台在不同天中可以重复使用。
|
||||
|
||||
**示例 1:**
|
||||
>输入:`demand = ["acd","bed","accd"]`
|
||||
>
|
||||
>输出:`6`
|
||||
>
|
||||
>解释:
|
||||
>第 `0` 天需要展台 `a、c、d`;
|
||||
>第 `1` 天需要展台 `b、e、d`;
|
||||
>第 `2` 天需要展台 `a、c、c、d`;
|
||||
>因此,后勤部准备 `abccde` 的展台,可以满足每天的展览需求;
|
||||
|
||||
**示例 2:**
|
||||
>输入:`demand = ["abc","ab","ac","b"]`
|
||||
>
|
||||
>输出:`3`
|
||||
|
||||
|
||||
**提示:**
|
||||
- `1 <= demand.length,demand[i].length <= 100`
|
||||
- `demand[i][j]` 仅为小写字母
|
@ -0,0 +1,43 @@
|
||||
<p>给你一个 <code>n</code> 个视频的上传序列,每个视频编号为 <code>1</code> 到 <code>n</code> 之间的 <strong>不同</strong> 数字,你需要依次将这些视频上传到服务器。请你实现一个数据结构,在上传的过程中计算 <strong>最长上传前缀</strong> 。</p>
|
||||
|
||||
<p>如果 <strong>闭区间</strong> <code>1</code> 到 <code>i</code> 之间的视频全部都已经被上传到服务器,那么我们称 <code>i</code> 是上传前缀。最长上传前缀指的是符合定义的 <code>i</code> 中的 <strong>最大值</strong> 。<br>
|
||||
<br>
|
||||
请你实现 <code>LUPrefix</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>LUPrefix(int n)</code> 初始化一个 <code>n</code> 个视频的流对象。</li>
|
||||
<li><code>void upload(int video)</code> 上传 <code>video</code> 到服务器。</li>
|
||||
<li><code>int longest()</code> 返回上述定义的 <strong>最长上传前缀</strong> 的长度。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>
|
||||
["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
|
||||
[[4], [3], [], [1], [], [2], []]
|
||||
<strong>输出:</strong>
|
||||
[null, null, 0, null, 1, null, 3]
|
||||
|
||||
<strong>解释:</strong>
|
||||
LUPrefix server = new LUPrefix(4); // 初始化 4个视频的上传流
|
||||
server.upload(3); // 上传视频 3 。
|
||||
server.longest(); // 由于视频 1 还没有被上传,最长上传前缀是 0 。
|
||||
server.upload(1); // 上传视频 1 。
|
||||
server.longest(); // 前缀 [1] 是最长上传前缀,所以我们返回 1 。
|
||||
server.upload(2); // 上传视频 2 。
|
||||
server.longest(); // 前缀 [1,2,3] 是最长上传前缀,所以我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= video <= 10<sup>5</sup></code></li>
|
||||
<li><code>video</code> 中所有值 <strong>互不相同</strong> 。</li>
|
||||
<li><code>upload</code> 和 <code>longest</code> <strong>总调用</strong> 次数至多不超过 <code>2 * 10<sup>5</sup></code> 次。</li>
|
||||
<li>至少会调用 <code>longest</code> 一次。</li>
|
||||
</ul>
|
34
leetcode-cn/problem (Chinese)/沙地治理 [XxZZjK].md
Normal file
34
leetcode-cn/problem (Chinese)/沙地治理 [XxZZjK].md
Normal file
@ -0,0 +1,34 @@
|
||||
在力扣城的沙漠分会场展示了一种沙柳树,这种沙柳树能够将沙地转化为坚实的绿地。
|
||||
展示的区域为正三角形,这片区域可以拆分为若干个子区域,每个子区域都是边长为 `1` 的小三角形,其中第 `i` 行有 `2i - 1` 个小三角形。
|
||||
|
||||
初始情况下,区域中的所有位置都为沙地,你需要指定一些子区域种植沙柳树成为绿地,以达到转化整片区域为绿地的最终目的,规则如下:
|
||||
- 若两个子区域共用一条边,则视为相邻;
|
||||
>如下图所示,(1,1)和(2,2)相邻,(3,2)和(3,3)相邻;(2,2)和(3,3)不相邻,因为它们没有共用边。
|
||||
- 若至少有两片绿地与同一片沙地相邻,则这片沙地也会转化为绿地
|
||||
- 转化为绿地的区域会影响其相邻的沙地
|
||||

|
||||
|
||||
现要将一片边长为 `size` 的沙地全部转化为绿地,请找到任意一种初始指定 **最少** 数量子区域种植沙柳的方案,并返回所有初始种植沙柳树的绿地坐标。
|
||||
|
||||
**示例 1:**
|
||||
>输入:`size = 3`
|
||||
>输出:`[[1,1],[2,1],[2,3],[3,1],[3,5]]`
|
||||
>解释:如下图所示,一种方案为:
|
||||
>指定所示的 5 个子区域为绿地。
|
||||
>相邻至少两片绿地的 (2,2),(3,2) 和 (3,4) 演变为绿地。
|
||||
>相邻两片绿地的 (3,3) 演变为绿地。
|
||||
{:width=500px}
|
||||
|
||||
|
||||
**示例 2:**
|
||||
>输入:`size = 2`
|
||||
>输出:`[[1,1],[2,1],[2,3]]`
|
||||
>解释:如下图所示:
|
||||
>指定所示的 3 个子区域为绿地。
|
||||
>相邻三片绿地的 (2,2) 演变为绿地。
|
||||
{:width=276px}
|
||||
|
||||
|
||||
|
||||
**提示:**
|
||||
- `1 <= size <= 1000`
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的整数矩阵 <code>grid</code> 。</p>
|
||||
|
||||
<p>按以下形式将矩阵的一部分定义为一个 <strong>沙漏</strong> :</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/img.jpg" style="width: 243px; height: 243px;">
|
||||
<p>返回沙漏中元素的 <strong>最大</strong> 总和。</p>
|
||||
|
||||
<p><strong>注意:</strong>沙漏无法旋转且必须整个包含在矩阵中。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/1.jpg" style="width: 323px; height: 323px;">
|
||||
<pre><strong>输入:</strong>grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
|
||||
<strong>输出:</strong>30
|
||||
<strong>解释:</strong>上图中的单元格表示元素总和最大的沙漏:6 + 2 + 1 + 2 + 9 + 2 + 8 = 30 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/2.jpg" style="width: 243px; height: 243px;">
|
||||
<pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>输出:</strong>35
|
||||
<strong>解释:</strong>上图中的单元格表示元素总和最大的沙漏:1 + 2 + 3 + 5 + 7 + 8 + 9 = 35 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>3 <= m, n <= 150</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
41
leetcode-cn/problem (Chinese)/满足不等式的数对数目 [number-of-pairs-satisfying-inequality].html
Normal file
41
leetcode-cn/problem (Chinese)/满足不等式的数对数目 [number-of-pairs-satisfying-inequality].html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>给你两个下标从 <strong>0</strong> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ,两个数组的大小都为 <code>n</code> ,同时给你一个整数 <code>diff</code> ,统计满足以下条件的 <strong>数对 </strong><code>(i, j)</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j <= n - 1</code> <b>且</b></li>
|
||||
<li><code>nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足条件的 <strong>数对数目</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>
|
||||
总共有 3 个满足条件的数对:
|
||||
1. i = 0, j = 1:3 - 2 <= 2 - 2 + 1 。因为 i < j 且 1 <= 1 ,这个数对满足条件。
|
||||
2. i = 0, j = 2:3 - 5 <= 2 - 1 + 1 。因为 i < j 且 -2 <= 2 ,这个数对满足条件。
|
||||
3. i = 1, j = 2:2 - 5 <= 2 - 1 + 1 。因为 i < j 且 -3 <= 2 ,这个数对满足条件。
|
||||
所以,我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums1 = [3,-1], nums2 = [-2,2], diff = -1
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
没有满足条件的任何数对,所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= diff <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
30
leetcode-cn/problem (Chinese)/美观的花束 [1GxJYY].md
Normal file
30
leetcode-cn/problem (Chinese)/美观的花束 [1GxJYY].md
Normal file
@ -0,0 +1,30 @@
|
||||
力扣嘉年华的花店中从左至右摆放了一排鲜花,记录于整型一维矩阵 `flowers` 中每个数字表示该位置所种鲜花的品种编号。你可以选择一段区间的鲜花做成插花,且不能丢弃。
|
||||
在你选择的插花中,如果每一品种的鲜花数量都不超过 `cnt` 朵,那么我们认为这束插花是 「美观的」。
|
||||
> - 例如:`[5,5,5,6,6]` 中品种为 `5` 的花有 `3` 朵, 品种为 `6` 的花有 `2` 朵,**每一品种** 的数量均不超过 `3`
|
||||
|
||||
请返回在这一排鲜花中,共有多少种可选择的区间,使得插花是「美观的」。
|
||||
|
||||
**注意:**
|
||||
- 答案需要以 `1e9 + 7 (1000000007)` 为底取模,如:计算初始结果为:`1000000008`,请返回 `1`
|
||||
|
||||
**示例 1:**
|
||||
>输入:`flowers = [1,2,3,2], cnt = 1`
|
||||
>
|
||||
>输出:`8`
|
||||
>
|
||||
>解释:相同的鲜花不超过 `1` 朵,共有 `8` 种花束是美观的;
|
||||
>长度为 `1` 的区间 `[1]、[2]、[3]、[2]` 均满足条件,共 `4` 种可选择区间
|
||||
>长度为 `2` 的区间 `[1,2]、[2,3]、[3,2]` 均满足条件,共 `3` 种可选择区间
|
||||
>长度为 `3` 的区间 `[1,2,3]` 满足条件,共 `1` 种可选择区间。
|
||||
>区间 `[2,3,2],[1,2,3,2]` 都包含了 `2` 朵鲜花 `2` ,不满足条件。
|
||||
>返回总数 `4+3+1 = 8`
|
||||
|
||||
**示例 2:**
|
||||
>输入:`flowers = [5,3,3,3], cnt = 2`
|
||||
>
|
||||
>输出:`8`
|
||||
|
||||
**提示:**
|
||||
- `1 <= flowers.length <= 10^5`
|
||||
- `1 <= flowers[i] <= 10^5`
|
||||
- `1 <= cnt <= 10^5`
|
29
leetcode-cn/problem (Chinese)/装饰树 [KnLfVT].md
Normal file
29
leetcode-cn/problem (Chinese)/装饰树 [KnLfVT].md
Normal file
@ -0,0 +1,29 @@
|
||||
力扣嘉年华上的 DIY 手工展位准备了一棵缩小版的 **二叉** 装饰树 `root` 和灯饰,你需要将灯饰逐一插入装饰树中,要求如下:
|
||||
|
||||
- 完成装饰的二叉树根结点与 `root` 的根结点值相同
|
||||
- 若一个节点拥有父节点,则在该节点和他的父节点之间插入一个灯饰(即插入一个值为 `-1` 的节点)。具体地:
|
||||
- 在一个 父节点 x 与其左子节点 y 之间添加 -1 节点, 节点 -1、节点 y 为各自父节点的左子节点,
|
||||
- 在一个 父节点 x 与其右子节点 y 之间添加 -1 节点, 节点 -1、节点 y 为各自父节点的右子节点,
|
||||
|
||||
现给定二叉树的根节点 `root` ,请返回完成装饰后的树的根节点。
|
||||
**示例 1:**
|
||||
>输入:
|
||||
>`root = [7,5,6]`
|
||||
>
|
||||
>输出:`[7,-1,-1,5,null,null,6]`
|
||||
>
|
||||
>解释:如下图所示,
|
||||
>{:width=400px}
|
||||
|
||||
**示例 2:**
|
||||
>输入:
|
||||
>`root = [3,1,7,3,8,null,4]`
|
||||
>
|
||||
>输出:`[3,-1,-1,1,null,null,7,-1,-1,null,-1,3,null,null,8,null,4]`
|
||||
>
|
||||
>解释:如下图所示
|
||||
{:width=500px}
|
||||
|
||||
**提示:**
|
||||
>`0 <= root.Val <= 1000`
|
||||
>`root` 节点数量范围为 `[1, 10^5]`
|
56
leetcode-cn/problem (Chinese)/集水器 [kskhHQ].md
Normal file
56
leetcode-cn/problem (Chinese)/集水器 [kskhHQ].md
Normal file
@ -0,0 +1,56 @@
|
||||
字符串数组 `shape` 描述了一个二维平面中的矩阵形式的集水器,`shape[i][j]` 表示集水器的第 `i` 行 `j` 列为:
|
||||
- `'l'`表示向左倾斜的隔板(即从左上到右下);
|
||||
- `'r'`表示向右倾斜的隔板(即从左下到右上);
|
||||
- `'.'` 表示此位置没有隔板
|
||||
{:width=200px}
|
||||
|
||||
已知当隔板构成存储容器可以存水,每个方格代表的蓄水量为 `2`。集水器初始浸泡在水中,除内部密闭空间外,所有位置均被水填满。
|
||||
现将其从水中竖直向上取出,请返回集水器最终的蓄水量。
|
||||
|
||||
**注意:**
|
||||
- 隔板具有良好的透气性,因此空气可以穿过隔板,但水无法穿过
|
||||
|
||||
**示例 1:**
|
||||
> 输入:
|
||||
> `shape = ["....rl","l.lr.r",".l..r.","..lr.."]`
|
||||
>
|
||||
> 输出:`18`
|
||||
>
|
||||
> 解释:如下图所示,由于空气会穿过隔板,因此红框区域没有水
|
||||
{:width="280px"}
|
||||
|
||||
|
||||
**示例 2:**
|
||||
> 输入:
|
||||
> `shape = [".rlrlrlrl","ll..rl..r",".llrrllrr","..lr..lr."]`
|
||||
> 输出:`18`
|
||||
>
|
||||
> 解释:如图所示。由于红框右侧未闭合,因此多余的水会从该处流走。
|
||||
{:width="400px"}
|
||||
|
||||
|
||||
**示例 3:**
|
||||
> 输入:
|
||||
> `shape = ["rlrr","llrl","llr."]`
|
||||
> 输出:`6`
|
||||
>
|
||||
> 解释:如图所示。
|
||||
{:width="230px"}
|
||||
|
||||
|
||||
|
||||
|
||||
**示例 4:**
|
||||
> 输入:
|
||||
> `shape = ["...rl...","..r..l..",".r.rl.l.","r.r..l.l","l.l..rl.",".l.lr.r.","..l..r..","...lr..."]`
|
||||
>
|
||||
> 输出:`30`
|
||||
>
|
||||
> 解释:如下图所示。由于中间为内部密闭空间,无法蓄水。
|
||||
{:width="350px"}
|
||||
|
||||
|
||||
**提示**:
|
||||
- `1 <= shape.length <= 50`
|
||||
- `1 <= shape[i].length <= 50`
|
||||
- `shape[i][j]` 仅为 `'l'`、`'r'` 或 `'.'`
|
@ -0,0 +1,27 @@
|
||||
<p>Given two positive integers <code>a</code> and <code>b</code>, return <em>the number of <strong>common</strong> factors of </em><code>a</code><em> and </em><code>b</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is a <strong>common factor</strong> of <code>a</code> and <code>b</code> if <code>x</code> divides both <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 12, b = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The common factors of 12 and 6 are 1, 2, 3, 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 25, b = 30
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The common factors of 25 and 30 are 1, 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b <= 1000</code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (English)/删除字符使频率相同(English) [remove-letter-to-equalize-frequency].html
Normal file
35
leetcode-cn/problem (English)/删除字符使频率相同(English) [remove-letter-to-equalize-frequency].html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code>, consisting of lowercase English letters. You need to select <strong>one</strong> index and <strong>remove</strong> the letter at that index from <code>word</code> so that the <strong>frequency</strong> of every letter present in <code>word</code> is equal.</p>
|
||||
|
||||
<p>Return<em> </em><code>true</code><em> if it is possible to remove one letter so that the frequency of all letters in </em><code>word</code><em> are equal, and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <b>frequency</b> of a letter <code>x</code> is the number of times it occurs in the string.</li>
|
||||
<li>You <strong>must</strong> remove exactly one letter and cannot chose to do nothing.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcc"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aazz"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists of lowercase English letters only.</li>
|
||||
</ul>
|
52
leetcode-cn/problem (English)/对字母串可执行的最大删除数(English) [maximum-deletions-on-a-string].html
Normal file
52
leetcode-cn/problem (English)/对字母串可执行的最大删除数(English) [maximum-deletions-on-a-string].html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Delete <strong>the entire string</strong> <code>s</code>, or</li>
|
||||
<li>Delete the <strong>first</strong> <code>i</code> letters of <code>s</code> if the first <code>i</code> letters of <code>s</code> are <strong>equal</strong> to the following <code>i</code> letters in <code>s</code>, for any <code>i</code> in the range <code>1 <= i <= s.length / 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, if <code>s = "ababc"</code>, then in one operation, you could delete the first two letters of <code>s</code> to get <code>"abc"</code>, since the first two letters of <code>s</code> and the following two letters of <code>s</code> are both equal to <code>"ab"</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations needed to delete all of </em><code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcabcdabc"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- Delete the first 3 letters ("abc") since the next 3 letters are equal. Now, s = "abcdabc".
|
||||
- Delete all the letters.
|
||||
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
|
||||
Note that in the second operation we cannot delete "abc" again because the next occurrence of "abc" does not happen in the next 3 letters.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaabaab"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- Delete the first letter ("a") since the next letter is equal. Now, s = "aabaab".
|
||||
- Delete the first 3 letters ("aab") since the next 3 letters are equal. Now, s = "aab".
|
||||
- Delete the first letter ("a") since the next letter is equal. Now, s = "ab".
|
||||
- Delete all the letters.
|
||||
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaaaa"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> In each operation, we can delete the first letter of s.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 4000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given two <strong>0-indexed</strong> arrays, <code>nums1</code> and <code>nums2</code>, consisting of non-negative integers. There exists another array, <code>nums3</code>, which contains the bitwise XOR of <strong>all pairings</strong> of integers between <code>nums1</code> and <code>nums2</code> (every integer in <code>nums1</code> is paired with every integer in <code>nums2</code> <strong>exactly once</strong>).</p>
|
||||
|
||||
<p>Return<em> the <strong>bitwise XOR</strong> of all integers in </em><code>nums3</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,1,3], nums2 = [10,2,5,0]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong>
|
||||
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
|
||||
The bitwise XOR of all these numbers is 13, so we return 13.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2], nums2 = [3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
|
||||
and nums1[1] ^ nums2[1].
|
||||
Thus, one possible nums3 array is [2,5,1,6].
|
||||
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>Given two positive integers <code>num1</code> and <code>num2</code>, find the integer <code>x</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>x</code> has the same number of set bits as <code>num2</code>, and</li>
|
||||
<li>The value <code>x XOR num1</code> is <strong>minimal</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p>Return <em>the integer </em><code>x</code>. The test cases are generated such that <code>x</code> is <strong>uniquely determined</strong>.</p>
|
||||
|
||||
<p>The number of <strong>set bits</strong> of an integer is the number of <code>1</code>'s in its binary representation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 3, num2 = 5
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The binary representations of num1 and num2 are 0011 and 0101, respectively.
|
||||
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 3 = 0</code> is minimal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 1, num2 = 12
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The binary representations of num1 and num2 are 0001 and 1100, respectively.
|
||||
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 1 = 2</code> is minimal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1, num2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a stream of <code>n</code> videos, each represented by a <strong>distinct</strong> number from <code>1</code> to <code>n</code> that you need to "upload" to a server. You need to implement a data structure that calculates the length of the <strong>longest uploaded prefix</strong> at various points in the upload process.</p>
|
||||
|
||||
<p>We consider <code>i</code> to be an uploaded prefix if all videos in the range <code>1</code> to <code>i</code> (<strong>inclusive</strong>) have been uploaded to the server. The longest uploaded prefix is the <strong>maximum </strong>value of <code>i</code> that satisfies this definition.<br />
|
||||
<br />
|
||||
Implement the <code>LUPrefix </code>class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>LUPrefix(int n)</code> Initializes the object for a stream of <code>n</code> videos.</li>
|
||||
<li><code>void upload(int video)</code> Uploads <code>video</code> to the server.</li>
|
||||
<li><code>int longest()</code> Returns the length of the <strong>longest uploaded prefix</strong> defined above.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
|
||||
[[4], [3], [], [1], [], [2], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, 0, null, 1, null, 3]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
LUPrefix server = new LUPrefix(4); // Initialize a stream of 4 videos.
|
||||
server.upload(3); // Upload video 3.
|
||||
server.longest(); // Since video 1 has not been uploaded yet, there is no prefix.
|
||||
// So, we return 0.
|
||||
server.upload(1); // Upload video 1.
|
||||
server.longest(); // The prefix [1] is the longest uploaded prefix, so we return 1.
|
||||
server.upload(2); // Upload video 2.
|
||||
server.longest(); // The prefix [1,2,3] is the longest uploaded prefix, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= video <= n</code></li>
|
||||
<li>All values of <code>video</code> are <strong>distinct</strong>.</li>
|
||||
<li>At most <code>2 * 10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>upload</code> and <code>longest</code>.</li>
|
||||
<li>At least one call will be made to <code>longest</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>.</p>
|
||||
|
||||
<p>We define an <strong>hourglass</strong> as a part of the matrix with the following form:</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/img.jpg" style="width: 243px; height: 243px;" />
|
||||
<p>Return <em>the <strong>maximum</strong> sum of the elements of an hourglass</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that an hourglass cannot be rotated and must be entirely contained within the matrix.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/1.jpg" style="width: 323px; height: 323px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/2.jpg" style="width: 243px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>Output:</strong> 35
|
||||
<strong>Explanation:</strong> There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>3 <= m, n <= 150</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
41
leetcode-cn/problem (English)/满足不等式的数对数目(English) [number-of-pairs-satisfying-inequality].html
Normal file
41
leetcode-cn/problem (English)/满足不等式的数对数目(English) [number-of-pairs-satisfying-inequality].html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of size <code>n</code>, and an integer <code>diff</code>. Find the number of <strong>pairs</strong> <code>(i, j)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j <= n - 1</code> <strong>and</strong></li>
|
||||
<li><code>nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>number of pairs</strong> that satisfy the conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
There are 3 pairs that satisfy the conditions:
|
||||
1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions.
|
||||
2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions.
|
||||
3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions.
|
||||
Therefore, we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,-1], nums2 = [-2,2], diff = -1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
Since there does not exist any pair that satisfies the conditions, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= diff <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
66
leetcode/originData/[no content]merge-operations-to-turn-array-into-a-palindrome.json
Normal file
66
leetcode/originData/[no content]merge-operations-to-turn-array-into-a-palindrome.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/bitwise-xor-of-all-pairings.json
Normal file
180
leetcode/originData/bitwise-xor-of-all-pairings.json
Normal file
File diff suppressed because one or more lines are too long
203
leetcode/originData/longest-uploaded-prefix.json
Normal file
203
leetcode/originData/longest-uploaded-prefix.json
Normal file
File diff suppressed because one or more lines are too long
194
leetcode/originData/maximum-deletions-on-a-string.json
Normal file
194
leetcode/originData/maximum-deletions-on-a-string.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/maximum-sum-of-an-hourglass.json
Normal file
180
leetcode/originData/maximum-sum-of-an-hourglass.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/minimize-xor.json
Normal file
174
leetcode/originData/minimize-xor.json
Normal file
File diff suppressed because one or more lines are too long
179
leetcode/originData/number-of-common-factors.json
Normal file
179
leetcode/originData/number-of-common-factors.json
Normal file
File diff suppressed because one or more lines are too long
205
leetcode/originData/number-of-pairs-satisfying-inequality.json
Normal file
205
leetcode/originData/number-of-pairs-satisfying-inequality.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/remove-letter-to-equalize-frequency.json
Normal file
180
leetcode/originData/remove-letter-to-equalize-frequency.json
Normal file
File diff suppressed because one or more lines are too long
34
leetcode/problem/bitwise-xor-of-all-pairings.html
Normal file
34
leetcode/problem/bitwise-xor-of-all-pairings.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>You are given two <strong>0-indexed</strong> arrays, <code>nums1</code> and <code>nums2</code>, consisting of non-negative integers. There exists another array, <code>nums3</code>, which contains the bitwise XOR of <strong>all pairings</strong> of integers between <code>nums1</code> and <code>nums2</code> (every integer in <code>nums1</code> is paired with every integer in <code>nums2</code> <strong>exactly once</strong>).</p>
|
||||
|
||||
<p>Return<em> the <strong>bitwise XOR</strong> of all integers in </em><code>nums3</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,1,3], nums2 = [10,2,5,0]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong>
|
||||
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
|
||||
The bitwise XOR of all these numbers is 13, so we return 13.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2], nums2 = [3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
|
||||
and nums1[1] ^ nums2[1].
|
||||
Thus, one possible nums3 array is [2,5,1,6].
|
||||
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
43
leetcode/problem/longest-uploaded-prefix.html
Normal file
43
leetcode/problem/longest-uploaded-prefix.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>You are given a stream of <code>n</code> videos, each represented by a <strong>distinct</strong> number from <code>1</code> to <code>n</code> that you need to "upload" to a server. You need to implement a data structure that calculates the length of the <strong>longest uploaded prefix</strong> at various points in the upload process.</p>
|
||||
|
||||
<p>We consider <code>i</code> to be an uploaded prefix if all videos in the range <code>1</code> to <code>i</code> (<strong>inclusive</strong>) have been uploaded to the server. The longest uploaded prefix is the <strong>maximum </strong>value of <code>i</code> that satisfies this definition.<br />
|
||||
<br />
|
||||
Implement the <code>LUPrefix </code>class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>LUPrefix(int n)</code> Initializes the object for a stream of <code>n</code> videos.</li>
|
||||
<li><code>void upload(int video)</code> Uploads <code>video</code> to the server.</li>
|
||||
<li><code>int longest()</code> Returns the length of the <strong>longest uploaded prefix</strong> defined above.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
|
||||
[[4], [3], [], [1], [], [2], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, 0, null, 1, null, 3]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
LUPrefix server = new LUPrefix(4); // Initialize a stream of 4 videos.
|
||||
server.upload(3); // Upload video 3.
|
||||
server.longest(); // Since video 1 has not been uploaded yet, there is no prefix.
|
||||
// So, we return 0.
|
||||
server.upload(1); // Upload video 1.
|
||||
server.longest(); // The prefix [1] is the longest uploaded prefix, so we return 1.
|
||||
server.upload(2); // Upload video 2.
|
||||
server.longest(); // The prefix [1,2,3] is the longest uploaded prefix, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= video <= n</code></li>
|
||||
<li>All values of <code>video</code> are <strong>distinct</strong>.</li>
|
||||
<li>At most <code>2 * 10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>upload</code> and <code>longest</code>.</li>
|
||||
<li>At least one call will be made to <code>longest</code>.</li>
|
||||
</ul>
|
52
leetcode/problem/maximum-deletions-on-a-string.html
Normal file
52
leetcode/problem/maximum-deletions-on-a-string.html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Delete <strong>the entire string</strong> <code>s</code>, or</li>
|
||||
<li>Delete the <strong>first</strong> <code>i</code> letters of <code>s</code> if the first <code>i</code> letters of <code>s</code> are <strong>equal</strong> to the following <code>i</code> letters in <code>s</code>, for any <code>i</code> in the range <code>1 <= i <= s.length / 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, if <code>s = "ababc"</code>, then in one operation, you could delete the first two letters of <code>s</code> to get <code>"abc"</code>, since the first two letters of <code>s</code> and the following two letters of <code>s</code> are both equal to <code>"ab"</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations needed to delete all of </em><code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcabcdabc"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- Delete the first 3 letters ("abc") since the next 3 letters are equal. Now, s = "abcdabc".
|
||||
- Delete all the letters.
|
||||
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
|
||||
Note that in the second operation we cannot delete "abc" again because the next occurrence of "abc" does not happen in the next 3 letters.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaabaab"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- Delete the first letter ("a") since the next letter is equal. Now, s = "aabaab".
|
||||
- Delete the first 3 letters ("aab") since the next 3 letters are equal. Now, s = "aab".
|
||||
- Delete the first letter ("a") since the next letter is equal. Now, s = "ab".
|
||||
- Delete all the letters.
|
||||
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaaaa"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> In each operation, we can delete the first letter of s.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 4000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
34
leetcode/problem/maximum-sum-of-an-hourglass.html
Normal file
34
leetcode/problem/maximum-sum-of-an-hourglass.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>.</p>
|
||||
|
||||
<p>We define an <strong>hourglass</strong> as a part of the matrix with the following form:</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/img.jpg" style="width: 243px; height: 243px;" />
|
||||
<p>Return <em>the <strong>maximum</strong> sum of the elements of an hourglass</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that an hourglass cannot be rotated and must be entirely contained within the matrix.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/1.jpg" style="width: 323px; height: 323px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/2.jpg" style="width: 243px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>Output:</strong> 35
|
||||
<strong>Explanation:</strong> There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>3 <= m, n <= 150</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
40
leetcode/problem/minimize-xor.html
Normal file
40
leetcode/problem/minimize-xor.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Given two positive integers <code>num1</code> and <code>num2</code>, find the integer <code>x</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>x</code> has the same number of set bits as <code>num2</code>, and</li>
|
||||
<li>The value <code>x XOR num1</code> is <strong>minimal</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p>Return <em>the integer </em><code>x</code>. The test cases are generated such that <code>x</code> is <strong>uniquely determined</strong>.</p>
|
||||
|
||||
<p>The number of <strong>set bits</strong> of an integer is the number of <code>1</code>'s in its binary representation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 3, num2 = 5
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The binary representations of num1 and num2 are 0011 and 0101, respectively.
|
||||
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 3 = 0</code> is minimal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 1, num2 = 12
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The binary representations of num1 and num2 are 0001 and 1100, respectively.
|
||||
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 1 = 2</code> is minimal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1, num2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
27
leetcode/problem/number-of-common-factors.html
Normal file
27
leetcode/problem/number-of-common-factors.html
Normal file
@ -0,0 +1,27 @@
|
||||
<p>Given two positive integers <code>a</code> and <code>b</code>, return <em>the number of <strong>common</strong> factors of </em><code>a</code><em> and </em><code>b</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is a <strong>common factor</strong> of <code>a</code> and <code>b</code> if <code>x</code> divides both <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 12, b = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The common factors of 12 and 6 are 1, 2, 3, 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 25, b = 30
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The common factors of 25 and 30 are 1, 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b <= 1000</code></li>
|
||||
</ul>
|
41
leetcode/problem/number-of-pairs-satisfying-inequality.html
Normal file
41
leetcode/problem/number-of-pairs-satisfying-inequality.html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of size <code>n</code>, and an integer <code>diff</code>. Find the number of <strong>pairs</strong> <code>(i, j)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j <= n - 1</code> <strong>and</strong></li>
|
||||
<li><code>nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>number of pairs</strong> that satisfy the conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
There are 3 pairs that satisfy the conditions:
|
||||
1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions.
|
||||
2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions.
|
||||
3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions.
|
||||
Therefore, we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,-1], nums2 = [-2,2], diff = -1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
Since there does not exist any pair that satisfies the conditions, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= diff <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
35
leetcode/problem/remove-letter-to-equalize-frequency.html
Normal file
35
leetcode/problem/remove-letter-to-equalize-frequency.html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code>, consisting of lowercase English letters. You need to select <strong>one</strong> index and <strong>remove</strong> the letter at that index from <code>word</code> so that the <strong>frequency</strong> of every letter present in <code>word</code> is equal.</p>
|
||||
|
||||
<p>Return<em> </em><code>true</code><em> if it is possible to remove one letter so that the frequency of all letters in </em><code>word</code><em> are equal, and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <b>frequency</b> of a letter <code>x</code> is the number of times it occurs in the string.</li>
|
||||
<li>You <strong>must</strong> remove exactly one letter and cannot chose to do nothing.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcc"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aazz"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists of lowercase English letters only.</li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user