mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
32ab538e5f
commit
147005c405
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.12.24**
|
||||
> 最后更新日期: **2023.01.04**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/closest-prime-numbers-in-range.json
Normal file
177
leetcode-cn/originData/closest-prime-numbers-in-range.json
Normal file
File diff suppressed because one or more lines are too long
196
leetcode-cn/originData/count-anagrams.json
Normal file
196
leetcode-cn/originData/count-anagrams.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
185
leetcode-cn/originData/maximum-tastiness-of-candy-basket.json
Normal file
185
leetcode-cn/originData/maximum-tastiness-of-candy-basket.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/minimize-the-maximum-of-two-arrays.json
Normal file
183
leetcode-cn/originData/minimize-the-maximum-of-two-arrays.json
Normal file
File diff suppressed because one or more lines are too long
177
leetcode-cn/originData/number-of-great-partitions.json
Normal file
177
leetcode-cn/originData/number-of-great-partitions.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
196
leetcode-cn/originData/reward-top-k-students.json
Normal file
196
leetcode-cn/originData/reward-top-k-students.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
@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的 <strong>环形</strong> 字符串数组 <code>words</code> 和一个字符串 <code>target</code> 。<strong>环形数组</strong> 意味着数组首尾相连。</p>
|
||||
|
||||
<ul>
|
||||
<li>形式上, <code>words[i]</code> 的下一个元素是 <code>words[(i + 1) % n]</code> ,而 <code>words[i]</code> 的前一个元素是 <code>words[(i - 1 + n) % n]</code> ,其中 <code>n</code> 是 <code>words</code> 的长度。</li>
|
||||
</ul>
|
||||
|
||||
<p>从 <code>startIndex</code> 开始,你一次可以用 <code>1</code> 步移动到下一个或者前一个单词。</p>
|
||||
|
||||
<p>返回到达目标字符串 <code>target</code> 所需的最短距离。如果 <code>words</code> 中不存在字符串 <code>target</code> ,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>从下标 1 开始,可以经由以下步骤到达 "hello" :
|
||||
- 向右移动 3 个单位,到达下标 4 。
|
||||
- 向左移动 2 个单位,到达下标 4 。
|
||||
- 向右移动 4 个单位,到达下标 0 。
|
||||
- 向左移动 1 个单位,到达下标 0 。
|
||||
到达 "hello" 的最短距离是 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>从下标 0 开始,可以经由以下步骤到达 "leetcode" :
|
||||
- 向右移动 2 个单位,到达下标 3 。
|
||||
- 向左移动 1 个单位,到达下标 3 。
|
||||
到达 "leetcode" 的最短距离是 1 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["i","eat","leetcode"], target = "ate", startIndex = 0
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>因为 words 中不存在字符串 "ate" ,所以返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> 和 <code>target</code> 仅由小写英文字母组成</li>
|
||||
<li><code>0 <= startIndex < words.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你两个字符串数组 <code>positive_feedback</code> 和 <code>negative_feedback</code> ,分别包含表示正面的和负面的词汇。<strong>不会</strong> 有单词同时是正面的和负面的。</p>
|
||||
|
||||
<p>一开始,每位学生分数为 <code>0</code> 。每个正面的单词会给学生的分数 <strong>加 </strong><code>3</code> 分,每个负面的词会给学生的分数 <strong>减 </strong> <code>1</code> 分。</p>
|
||||
|
||||
<p>给你 <code>n</code> 个学生的评语,用一个下标从 <strong>0</strong> 开始的字符串数组 <code>report</code> 和一个下标从 <strong>0</strong> 开始的整数数组 <code>student_id</code> 表示,其中 <code>student_id[i]</code> 表示这名学生的 ID ,这名学生的评语是 <code>report[i]</code> 。每名学生的 ID <strong>互不相同</strong>。</p>
|
||||
|
||||
<p>给你一个整数 <code>k</code> ,请你返回按照得分 <strong>从高到低</strong> 最顶尖的<em> </em><code>k</code> 名学生。如果有多名学生分数相同,ID 越小排名越前。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2
|
||||
<b>输出:</b>[1,2]
|
||||
<b>解释:</b>
|
||||
两名学生都有 1 个正面词汇,都得到 3 分,学生 1 的 ID 更小所以排名更前。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2
|
||||
<b>输出:</b>[2,1]
|
||||
<b>解释:</b>
|
||||
- ID 为 1 的学生有 1 个正面词汇和 1 个负面词汇,所以得分为 3-1=2 分。
|
||||
- ID 为 2 的学生有 1 个正面词汇,得分为 3 分。
|
||||
学生 2 分数更高,所以返回 [2,1] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= positive_feedback.length, negative_feedback.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= positive_feedback[i].length, negative_feedback[j].length <= 100</code></li>
|
||||
<li><code>positive_feedback[i]</code> 和 <code>negative_feedback[j]</code> 都只包含小写英文字母。</li>
|
||||
<li><code>positive_feedback</code> 和 <code>negative_feedback</code> 中不会有相同单词。</li>
|
||||
<li><code>n == report.length == student_id.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>report[i]</code> 只包含小写英文字母和空格 <code>' '</code> 。</li>
|
||||
<li><code>report[i]</code> 中连续单词之间有单个空格隔开。</li>
|
||||
<li><code>1 <= report[i].length <= 100</code></li>
|
||||
<li><code>1 <= student_id[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>student_id[i]</code> 的值 <strong>互不相同</strong> 。</li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p><strong>分区</strong> 的定义是:将数组划分成两个有序的 <strong>组</strong> ,并满足每个元素 <strong>恰好</strong> 存在于 <strong>某一个</strong> 组中。如果分区中每个组的元素和都大于等于 <code>k</code> ,则认为分区是一个好分区。</p>
|
||||
|
||||
<p>返回 <strong>不同</strong> 的好分区的数目。由于答案可能很大,请返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后的结果。</p>
|
||||
|
||||
<p>如果在两个分区中,存在某个元素 <code>nums[i]</code> 被分在不同的组中,则认为这两个分区不同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4], k = 4
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>好分区的情况是 ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) 和 ([4], [1,2,3]) 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,3,3], k = 4
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>数组中不存在好分区。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [6,6], k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以将 nums[0] 放入第一个分区或第二个分区中。
|
||||
好分区的情况是 ([6], [6]) 和 ([6], [6]) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, k <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个字符串 <code>s</code> ,它每一位都是 <code>1</code> 到 <code>9</code> 之间的数字组成,同时给你一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果一个字符串 <code>s</code> 的分割满足以下条件,我们称它是一个 <strong>好</strong> 分割:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s</code> 中每个数位 <strong>恰好</strong> 属于一个子字符串。</li>
|
||||
<li>每个子字符串的值都小于等于 <code>k</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>s</code> 所有的 <strong>好</strong> 分割中,子字符串的 <strong>最少</strong> 数目。如果不存在 <code>s</code> 的<strong> 好</strong> 分割,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><b>注意:</b></p>
|
||||
|
||||
<ul>
|
||||
<li>一个字符串的 <strong>值</strong> 是这个字符串对应的整数。比方说,<code>"123"</code> 的值为 <code>123</code> ,<code>"1"</code> 的值是 <code>1</code> 。</li>
|
||||
<li><strong>子字符串</strong> 是字符串中一段连续的字符序列。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "165462", k = 60
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>我们将字符串分割成子字符串 "16" ,"54" ,"6" 和 "2" 。每个子字符串的值都小于等于 k = 60 。
|
||||
不存在小于 4 个子字符串的好分割。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "238182", k = 5
|
||||
<b>输出:</b>-1
|
||||
<strong>解释:</strong>这个字符串不存在好分割。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 是 <code>'1'</code> 到 <code>'9'</code> 之间的数字。</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>给你一个正整数数组 <code>nums</code> ,对 <code>nums</code> 所有元素求积之后,找出并返回乘积中 <strong>不同质因数</strong> 的数目。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><strong>质数</strong> 是指大于 <code>1</code> 且仅能被 <code>1</code> 及自身整除的数字。</li>
|
||||
<li>如果 <code>val2 / val1</code> 是一个整数,则整数 <code>val1</code> 是另一个整数 <code>val2</code> 的一个因数。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [2,4,3,7,10,6]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
nums 中所有元素的乘积是:2 * 4 * 3 * 7 * 10 * 6 = 10080 = 2<sup>5</sup> * 3<sup>2</sup> * 5 * 7 。
|
||||
共有 4 个不同的质因数,所以返回 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [2,4,8,16]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>
|
||||
nums 中所有元素的乘积是:2 * 4 * 8 * 16 = 1024 = 2<sup>10</sup> 。
|
||||
共有 1 个不同的质因数,所以返回 1 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>给你一个长度为 <code>n</code> ,下标从 <strong>0</strong> 开始的整数数组 <code>forts</code> ,表示一些城堡。<code>forts[i]</code> 可以是 <code>-1</code> ,<code>0</code> 或者 <code>1</code> ,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1</code> 表示第 <code>i</code> 个位置 <strong>没有</strong> 城堡。</li>
|
||||
<li><code>0</code> 表示第 <code>i</code> 个位置有一个 <strong>敌人</strong> 的城堡。</li>
|
||||
<li><code>1</code> 表示第 <code>i</code> 个位置有一个你控制的城堡。</li>
|
||||
</ul>
|
||||
|
||||
<p>现在,你需要决定,将你的军队从某个你控制的城堡位置 <code>i</code> 移动到一个空的位置 <code>j</code> ,满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i, j <= n - 1</code></li>
|
||||
<li>军队经过的位置 <strong>只有</strong> 敌人的城堡。正式的,对于所有 <code>min(i,j) < k < max(i,j)</code> 的 <code>k</code> ,都满足 <code>forts[k] == 0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>当军队移动时,所有途中经过的敌人城堡都会被 <strong>摧毁</strong> 。</p>
|
||||
|
||||
<p>请你返回 <strong>最多</strong> 可以摧毁的敌人城堡数目。如果 <strong>无法</strong> 移动你的军队,或者没有你控制的城堡,请返回 <code>0</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>forts = [1,0,0,-1,0,0,0,0,1]
|
||||
<b>输出:</b>4
|
||||
<strong>解释:</strong>
|
||||
- 将军队从位置 0 移动到位置 3 ,摧毁 2 个敌人城堡,位置分别在 1 和 2 。
|
||||
- 将军队从位置 8 移动到位置 3 ,摧毁 4 个敌人城堡。
|
||||
4 是最多可以摧毁的敌人城堡数目,所以我们返回 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>forts = [0,0,1,-1]
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>由于无法摧毁敌人的城堡,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= forts.length <= 1000</code></li>
|
||||
<li><code>-1 <= forts[i] <= 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>给你两个数组 <code>arr1</code> 和 <code>arr2</code> ,它们一开始都是空的。你需要往它们中添加正整数,使它们满足以下条件:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr1</code> 包含 <code>uniqueCnt1</code> 个<strong> 互不相同</strong> 的正整数,每个整数都 <strong>不能 </strong>被 <code>divisor1</code> <strong>整除</strong> 。</li>
|
||||
<li><code>arr2</code> 包含 <code>uniqueCnt2</code> 个<strong> 互不相同</strong> 的正整数,每个整数都 <strong>不能</strong> 被 <code>divisor2</code> <strong>整除</strong> 。</li>
|
||||
<li><code>arr1</code> 和 <code>arr2</code> 中的元素 <strong>互不相同</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你 <code>divisor1</code> ,<code>divisor2</code> ,<code>uniqueCnt1</code> 和 <code>uniqueCnt2</code> ,请你返回两个数组中 <strong>最大元素</strong> 的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>
|
||||
我们可以把前 4 个自然数划分到 arr1 和 arr2 中。
|
||||
arr1 = [1] 和 arr2 = [2,3,4] 。
|
||||
可以看出两个数组都满足条件。
|
||||
最大值是 4 ,所以返回 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>
|
||||
arr1 = [1,2] 和 arr2 = [3] 满足所有条件。
|
||||
最大值是 3 ,所以返回 3 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>
|
||||
最终数组为 arr1 = [1,3,5,7,9,11,13,15] 和 arr2 = [2,6] 。
|
||||
上述方案是满足所有条件的最优解。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= divisor1, divisor2 <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= uniqueCnt1, uniqueCnt2 < 10<sup>9</sup></code></li>
|
||||
<li><code>2 <= uniqueCnt1 + uniqueCnt2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>给你一个由字符 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成的字符串 <code>s</code> 和一个非负整数 <code>k</code> 。每分钟,你可以选择取走 <code>s</code> <strong>最左侧</strong> 还是 <strong>最右侧</strong> 的那个字符。</p>
|
||||
|
||||
<p>你必须取走每种字符 <strong>至少</strong> <code>k</code> 个,返回需要的 <strong>最少</strong> 分钟数;如果无法取到,则返回<em> </em><code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aabaaaacaabc", k = 2
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>
|
||||
从 s 的左侧取三个字符,现在共取到两个字符 'a' 、一个字符 'b' 。
|
||||
从 s 的右侧取五个字符,现在共取到四个字符 'a' 、两个字符 'b' 和两个字符 'c' 。
|
||||
共需要 3 + 5 = 8 分钟。
|
||||
可以证明需要的最少分钟数是 8 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "a", k = 1
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无法取到一个字符 'b' 或者 'c',所以返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 仅由字母 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成</li>
|
||||
<li><code>0 <= k <= s.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个正整数数组 <code>price</code> ,其中 <code>price[i]</code> 表示第 <code>i</code> 类糖果的价格,另给你一个正整数 <code>k</code> 。</p>
|
||||
|
||||
<p>商店组合 <code>k</code> 类 <strong>不同</strong> 糖果打包成礼盒出售。礼盒的 <strong>甜蜜度</strong> 是礼盒中任意两种糖果 <strong>价格</strong> 绝对差的最小值。</p>
|
||||
|
||||
<p>返回礼盒的 <strong>最大 </strong>甜蜜度<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>price = [13,5,1,8,21,2], k = 3
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>选出价格分别为 [13,5,21] 的三类糖果。
|
||||
礼盒的甜蜜度为 min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8 。
|
||||
可以证明能够取得的最大甜蜜度就是 8 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>price = [1,3,1], k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>选出价格分别为 [1,3] 的两类糖果。
|
||||
礼盒的甜蜜度为 min(|1 - 3|) = min(2) = 2 。
|
||||
可以证明能够取得的最大甜蜜度就是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>price = [7,7,7,7], k = 2
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>从现有的糖果中任选两类糖果,甜蜜度都会是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= price.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= price[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>2 <= k <= price.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个字符串 <code>s</code> ,它包含一个或者多个单词。单词之间用单个空格 <code>' '</code> 隔开。</p>
|
||||
|
||||
<p>如果字符串 <code>t</code> 中第 <code>i</code> 个单词是 <code>s</code> 中第 <code>i</code> 个单词的一个 <strong>排列</strong> ,那么我们称字符串 <code>t</code> 是字符串 <code>s</code> 的同位异构字符串。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>"acb dfe"</code> 是 <code>"abc def"</code> 的同位异构字符串,但是 <code>"def cab"</code> 和 <code>"adc bef"</code> 不是。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回<em> </em><code>s</code> 的同位异构字符串的数目,由于答案可能很大,请你将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "too hot"
|
||||
<b>输出:</b>18
|
||||
<b>解释:</b>输入字符串的一些同位异构字符串为 "too hot" ,"oot hot" ,"oto toh" ,"too toh" 以及 "too oht" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "aa"
|
||||
<b>输出:</b>1
|
||||
<strong>解释:</strong>输入字符串只有一个同位异构字符串。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母和空格 <code>' '</code> 。</li>
|
||||
<li>相邻单词之间由单个空格隔开。</li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>给你一个整数 <code>num</code> ,返回 <code>num</code> 中能整除 <code>num</code> 的数位的数目。</p>
|
||||
|
||||
<p>如果满足 <code>nums % val == 0</code> ,则认为整数 <code>val</code> 可以整除 <code>nums</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = 7
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>7 被自己整除,因此答案是 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = 121
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>121 可以被 1 整除,但无法被 2 整除。由于 1 出现两次,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = 1248
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>1248 可以被它每一位上的数字整除,因此答案是 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
<li><code>num</code> 的数位中不含 <code>0</code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>给你两个正整数 <code>left</code> 和 <code>right</code> ,请你找到两个整数 <code>num1</code> 和 <code>num2</code> ,它们满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>left <= nums1 < nums2 <= right </code> 。</li>
|
||||
<li><code>nums1</code> 和 <code>nums2</code> 都是 <strong>质数</strong> 。</li>
|
||||
<li><code>nums2 - nums1</code> 是满足上述条件的质数对中的 <strong>最小值</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回正整数数组 <code>ans = [nums1, nums2]</code> 。如果有多个整数对满足上述条件,请你返回 <code>nums1</code> 最小的质数对。如果不存在符合题意的质数对,请你返回 <code>[-1, -1]</code> 。</p>
|
||||
|
||||
<p>如果一个整数大于 <code>1</code> ,且只能被 <code>1</code> 和它自己整除,那么它是一个质数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>left = 10, right = 19
|
||||
<b>输出:</b>[11,13]
|
||||
<b>解释:</b>10 到 19 之间的质数为 11 ,13 ,17 和 19 。
|
||||
质数对的最小差值是 2 ,[11,13] 和 [17,19] 都可以得到最小差值。
|
||||
由于 11 比 17 小,我们返回第一个质数对。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>left = 4, right = 6
|
||||
<b>输出:</b>[-1,-1]
|
||||
<b>解释:</b>给定范围内只有一个质数,所以题目条件无法被满足。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <strong>circular</strong> string array <code>words</code> and a string <code>target</code>. A <strong>circular array</strong> means that the array's end connects to the array's beginning.</p>
|
||||
|
||||
<ul>
|
||||
<li>Formally, the next element of <code>words[i]</code> is <code>words[(i + 1) % n]</code> and the previous element of <code>words[i]</code> is <code>words[(i - 1 + n) % n]</code>, where <code>n</code> is the length of <code>words</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Starting from <code>startIndex</code>, you can move to either the next word or the previous word with <code>1</code> step at a time.</p>
|
||||
|
||||
<p>Return <em>the <strong>shortest</strong> distance needed to reach the string</em> <code>target</code>. If the string <code>target</code> does not exist in <code>words</code>, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We start from index 1 and can reach "hello" by
|
||||
- moving 3 units to the right to reach index 4.
|
||||
- moving 2 units to the left to reach index 4.
|
||||
- moving 4 units to the right to reach index 0.
|
||||
- moving 1 unit to the left to reach index 0.
|
||||
The shortest distance to reach "hello" is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We start from index 0 and can reach "leetcode" by
|
||||
- moving 2 units to the right to reach index 3.
|
||||
- moving 1 unit to the left to reach index 3.
|
||||
The shortest distance to reach "leetcode" is 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["i","eat","leetcode"], target = "ate", startIndex = 0
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Since "ate" does not exist in <code>words</code>, we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> and <code>target</code> consist of only lowercase English letters.</li>
|
||||
<li><code>0 <= startIndex < words.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given two string arrays <code>positive_feedback</code> and <code>negative_feedback</code>, containing the words denoting positive and negative feedback, respectively. Note that <strong>no</strong> word is both positive and negative.</p>
|
||||
|
||||
<p>Initially every student has <code>0</code> points. Each positive word in a feedback report <strong>increases</strong> the points of a student by <code>3</code>, whereas each negative word <strong>decreases</strong> the points by <code>1</code>.</p>
|
||||
|
||||
<p>You are given <code>n</code> feedback reports, represented by a <strong>0-indexed</strong> string array <code>report</code> and a <strong>0-indexed</strong> integer array <code>student_id</code>, where <code>student_id[i]</code> represents the ID of the student who has received the feedback report <code>report[i]</code>. The ID of each student is <strong>unique</strong>.</p>
|
||||
|
||||
<p>Given an integer <code>k</code>, return <em>the top </em><code>k</code><em> students after ranking them in <strong>non-increasing</strong> order by their points</em>. In case more than one student has the same points, the one with the lower ID ranks higher.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong>
|
||||
Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2
|
||||
<strong>Output:</strong> [2,1]
|
||||
<strong>Explanation:</strong>
|
||||
- The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points.
|
||||
- The student with ID 2 has 1 positive feedback, so he has 3 points.
|
||||
Since student 2 has more points, [2,1] is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= positive_feedback.length, negative_feedback.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= positive_feedback[i].length, negative_feedback[j].length <= 100</code></li>
|
||||
<li>Both <code>positive_feedback[i]</code> and <code>negative_feedback[j]</code> consists of lowercase English letters.</li>
|
||||
<li>No word is present in both <code>positive_feedback</code> and <code>negative_feedback</code>.</li>
|
||||
<li><code>n == report.length == student_id.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>report[i]</code> consists of lowercase English letters and spaces <code>' '</code>.</li>
|
||||
<li>There is a single space between consecutive words of <code>report[i]</code>.</li>
|
||||
<li><code>1 <= report[i].length <= 100</code></li>
|
||||
<li><code>1 <= student_id[i] <= 10<sup>9</sup></code></li>
|
||||
<li>All the values of <code>student_id[i]</code> are <strong>unique</strong>.</li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers and an integer <code>k</code>.</p>
|
||||
|
||||
<p><strong>Partition</strong> the array into two ordered <strong>groups</strong> such that each element is in exactly <strong>one</strong> group. A partition is called great if the <strong>sum</strong> of elements of each group is greater than or equal to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> great partitions</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Two partitions are considered distinct if some element <code>nums[i]</code> is in different groups in the two partitions.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], k = 4
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3], k = 4
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no great partitions for this array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,6], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can either put nums[0] in the first partition or in the second partition.
|
||||
The great partitions will be ([6], [6]) and ([6], [6]).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, k <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,53 @@
|
||||
<p>You are given a string <code>s</code> consisting of digits from <code>1</code> to <code>9</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A partition of a string <code>s</code> is called <strong>good</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each digit of <code>s</code> is part of <strong>exactly</strong> one substring.</li>
|
||||
<li>The value of each substring is less than or equal to <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of substrings in a <strong>good</strong> partition of</em> <code>s</code>. If no <strong>good</strong> partition of <code>s</code> exists, return <code>-1</code>.</p>
|
||||
|
||||
<p><b>Note</b> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>value</strong> of a string is its result when interpreted as an integer. For example, the value of <code>"123"</code> is <code>123</code> and the value of <code>"1"</code> is <code>1</code>.</li>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "165462", k = 60
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can partition the string into substrings "16", "54", "6", and "2". Each substring has a value less than or equal to k = 60.
|
||||
It can be shown that we cannot partition the string into less than 4 substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "238182", k = 5
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no good partition for this string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is a digit from <code>'1'</code> to <code>'9'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
@ -0,0 +1,37 @@
|
||||
<p>Given an array of positive integers <code>nums</code>, return <em>the number of <strong>distinct prime factors</strong> in the product of the elements of</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>A number greater than <code>1</code> is called <strong>prime</strong> if it is divisible by only <code>1</code> and itself.</li>
|
||||
<li>An integer <code>val1</code> is a factor of another integer <code>val2</code> if <code>val2 / val1</code> is an integer.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,3,7,10,6]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 2<sup>5</sup> * 3<sup>2</sup> * 5 * 7.
|
||||
There are 4 distinct prime factors so we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,8,16]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 2<sup>10</sup>.
|
||||
There is 1 distinct prime factor so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>forts</code> of length <code>n</code> representing the positions of several forts. <code>forts[i]</code> can be <code>-1</code>, <code>0</code>, or <code>1</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1</code> represents there is <strong>no fort</strong> at the <code>i<sup>th</sup></code> position.</li>
|
||||
<li><code>0</code> indicates there is an <strong>enemy</strong> fort at the <code>i<sup>th</sup></code> position.</li>
|
||||
<li><code>1</code> indicates the fort at the <code>i<sup>th</sup></code> the position is under your command.</li>
|
||||
</ul>
|
||||
|
||||
<p>Now you have decided to move your army from one of your forts at position <code>i</code> to an empty position <code>j</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i, j <= n - 1</code></li>
|
||||
<li>The army travels over enemy forts <strong>only</strong>. Formally, for all <code>k</code> where <code>min(i,j) < k < max(i,j)</code>, <code>forts[k] == 0.</code></li>
|
||||
</ul>
|
||||
|
||||
<p>While moving the army, all the enemy forts that come in the way are <strong>captured</strong>.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> number of enemy forts that can be captured</em>. In case it is <strong>impossible</strong> to move your army, or you do not have any fort under your command, return <code>0</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forts = [1,0,0,-1,0,0,0,0,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.
|
||||
- Moving the army from position 8 to position 3 captures 4 enemy forts.
|
||||
Since 4 is the maximum number of enemy forts that can be captured, we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forts = [0,0,1,-1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Since no enemy fort can be captured, 0 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= forts.length <= 1000</code></li>
|
||||
<li><code>-1 <= forts[i] <= 1</code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>We have two arrays <code>arr1</code> and <code>arr2</code> which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr1</code> contains <code>uniqueCnt1</code> <strong>distinct</strong> positive integers, each of which is <strong>not divisible</strong> by <code>divisor1</code>.</li>
|
||||
<li><code>arr2</code> contains <code>uniqueCnt2</code> <strong>distinct</strong> positive integers, each of which is <strong>not divisible</strong> by <code>divisor2</code>.</li>
|
||||
<li><strong>No</strong> integer is present in both <code>arr1</code> and <code>arr2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given <code>divisor1</code>, <code>divisor2</code>, <code>uniqueCnt1</code>, and <code>uniqueCnt2</code>, return <em>the <strong>minimum possible maximum</strong> integer that can be present in either array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
We can distribute the first 4 natural numbers into arr1 and arr2.
|
||||
arr1 = [1] and arr2 = [2,3,4].
|
||||
We can see that both arrays satisfy all the conditions.
|
||||
Since the maximum value is 4, we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
Here arr1 = [1,2], and arr2 = [3] satisfy all conditions.
|
||||
Since the maximum value is 3, we return it.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong>
|
||||
Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6].
|
||||
It can be shown that it is not possible to obtain a lower maximum satisfying all conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= divisor1, divisor2 <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= uniqueCnt1, uniqueCnt2 < 10<sup>9</sup></code></li>
|
||||
<li><code>2 <= uniqueCnt1 + uniqueCnt2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>You are given a string <code>s</code> consisting of the characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code> and a non-negative integer <code>k</code>. Each minute, you may take either the <strong>leftmost</strong> character of <code>s</code>, or the <strong>rightmost</strong> character of <code>s</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of minutes needed for you to take <strong>at least</strong> </em><code>k</code><em> of each character, or return </em><code>-1</code><em> if it is not possible to take </em><code>k</code><em> of each character.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabaaaacaabc", k = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
|
||||
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
|
||||
A total of 3 + 5 = 8 minutes is needed.
|
||||
It can be proven that 8 is the minimum number of minutes needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "a", k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is not possible to take one 'b' or 'c' so return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of only the letters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li>
|
||||
<li><code>0 <= k <= s.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>You are given an array of positive integers <code>price</code> where <code>price[i]</code> denotes the price of the <code>i<sup>th</sup></code> candy and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>The store sells baskets of <code>k</code> <strong>distinct</strong> candies. The <strong>tastiness</strong> of a candy basket is the smallest absolute difference of the <strong>prices</strong> of any two candies in the basket.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> tastiness of a candy basket.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> price = [13,5,1,8,21,2], k = 3
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> Choose the candies with the prices [13,5,21].
|
||||
The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8.
|
||||
It can be proven that 8 is the maximum tastiness that can be achieved.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> price = [1,3,1], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Choose the candies with the prices [1,3].
|
||||
The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2.
|
||||
It can be proven that 2 is the maximum tastiness that can be achieved.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> price = [7,7,7,7], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= k <= price.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= price[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given a string <code>s</code> containing one or more words. Every consecutive pair of words is separated by a single space <code>' '</code>.</p>
|
||||
|
||||
<p>A string <code>t</code> is an <strong>anagram</strong> of string <code>s</code> if the <code>i<sup>th</sup></code> word of <code>t</code> is a <strong>permutation</strong> of the <code>i<sup>th</sup></code> word of <code>s</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"acb dfe"</code> is an anagram of <code>"abc def"</code>, but <code>"def cab"</code> and <code>"adc bef"</code> are not.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct anagrams</strong> of </em><code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "too hot"
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> Some of the anagrams of the given string are "too hot", "oot hot", "oto toh", "too toh", and "too oht".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aa"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is only one anagram possible for the given string.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters and spaces <code>' '</code>.</li>
|
||||
<li>There is single space between consecutive words.</li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>Given an integer <code>num</code>, return <em>the number of digits in <code>num</code> that divide </em><code>num</code>.</p>
|
||||
|
||||
<p>An integer <code>val</code> divides <code>nums</code> if <code>nums % val == 0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 7
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> 7 divides itself, hence the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 121
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 1248
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> 1248 is divisible by all of its digits, hence the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
<li><code>num</code> does not contain <code>0</code> as one of its digits.</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>Given two positive integers <code>left</code> and <code>right</code>, find the two integers <code>num1</code> and <code>num2</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>left <= nums1 < nums2 <= right </code>.</li>
|
||||
<li><code>nums1</code> and <code>nums2</code> are both <strong>prime</strong> numbers.</li>
|
||||
<li><code>nums2 - nums1</code> is the <strong>minimum</strong> amongst all other pairs satisfying the above conditions.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the positive integer array</em> <code>ans = [nums1, nums2]</code>. <em>If there are multiple pairs satisfying these conditions, return the one with the minimum</em> <code>nums1</code> <em>value or</em> <code>[-1, -1]</code> <em>if such numbers do not exist.</em></p>
|
||||
|
||||
<p>A number greater than <code>1</code> is called <b>prime</b> if it is only divisible by <code>1</code> and itself.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 10, right = 19
|
||||
<strong>Output:</strong> [11,13]
|
||||
<strong>Explanation:</strong> The prime numbers between 10 and 19 are 11, 13, 17, and 19.
|
||||
The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
|
||||
Since 11 is smaller than 17, we return the first pair.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 4, right = 6
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
<strong>Explanation:</strong> There exists only one prime number in the given range, so the conditions cannot be satisfied.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
174
leetcode/originData/closest-prime-numbers-in-range.json
Normal file
174
leetcode/originData/closest-prime-numbers-in-range.json
Normal file
File diff suppressed because one or more lines are too long
193
leetcode/originData/count-anagrams.json
Normal file
193
leetcode/originData/count-anagrams.json
Normal file
File diff suppressed because one or more lines are too long
169
leetcode/originData/count-the-digits-that-divide-a-number.json
Normal file
169
leetcode/originData/count-the-digits-that-divide-a-number.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
182
leetcode/originData/maximum-tastiness-of-candy-basket.json
Normal file
182
leetcode/originData/maximum-tastiness-of-candy-basket.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/minimize-the-maximum-of-two-arrays.json
Normal file
180
leetcode/originData/minimize-the-maximum-of-two-arrays.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/number-of-great-partitions.json
Normal file
174
leetcode/originData/number-of-great-partitions.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
193
leetcode/originData/reward-top-k-students.json
Normal file
193
leetcode/originData/reward-top-k-students.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
46
leetcode/problem/closest-prime-numbers-in-range.html
Normal file
46
leetcode/problem/closest-prime-numbers-in-range.html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>Given two positive integers <code>left</code> and <code>right</code>, find the two integers <code>num1</code> and <code>num2</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>left <= nums1 < nums2 <= right </code>.</li>
|
||||
<li><code>nums1</code> and <code>nums2</code> are both <strong>prime</strong> numbers.</li>
|
||||
<li><code>nums2 - nums1</code> is the <strong>minimum</strong> amongst all other pairs satisfying the above conditions.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the positive integer array</em> <code>ans = [nums1, nums2]</code>. <em>If there are multiple pairs satisfying these conditions, return the one with the minimum</em> <code>nums1</code> <em>value or</em> <code>[-1, -1]</code> <em>if such numbers do not exist.</em></p>
|
||||
|
||||
<p>A number greater than <code>1</code> is called <b>prime</b> if it is only divisible by <code>1</code> and itself.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 10, right = 19
|
||||
<strong>Output:</strong> [11,13]
|
||||
<strong>Explanation:</strong> The prime numbers between 10 and 19 are 11, 13, 17, and 19.
|
||||
The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
|
||||
Since 11 is smaller than 17, we return the first pair.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 4, right = 6
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
<strong>Explanation:</strong> There exists only one prime number in the given range, so the conditions cannot be satisfied.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
34
leetcode/problem/count-anagrams.html
Normal file
34
leetcode/problem/count-anagrams.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>You are given a string <code>s</code> containing one or more words. Every consecutive pair of words is separated by a single space <code>' '</code>.</p>
|
||||
|
||||
<p>A string <code>t</code> is an <strong>anagram</strong> of string <code>s</code> if the <code>i<sup>th</sup></code> word of <code>t</code> is a <strong>permutation</strong> of the <code>i<sup>th</sup></code> word of <code>s</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"acb dfe"</code> is an anagram of <code>"abc def"</code>, but <code>"def cab"</code> and <code>"adc bef"</code> are not.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct anagrams</strong> of </em><code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "too hot"
|
||||
<strong>Output:</strong> 18
|
||||
<strong>Explanation:</strong> Some of the anagrams of the given string are "too hot", "oot hot", "oto toh", "too toh", and "too oht".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aa"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is only one anagram possible for the given string.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters and spaces <code>' '</code>.</li>
|
||||
<li>There is single space between consecutive words.</li>
|
||||
</ul>
|
36
leetcode/problem/count-the-digits-that-divide-a-number.html
Normal file
36
leetcode/problem/count-the-digits-that-divide-a-number.html
Normal file
@ -0,0 +1,36 @@
|
||||
<p>Given an integer <code>num</code>, return <em>the number of digits in <code>num</code> that divide </em><code>num</code>.</p>
|
||||
|
||||
<p>An integer <code>val</code> divides <code>nums</code> if <code>nums % val == 0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 7
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> 7 divides itself, hence the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 121
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 1248
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> 1248 is divisible by all of its digits, hence the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
<li><code>num</code> does not contain <code>0</code> as one of its digits.</li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>Given an array of positive integers <code>nums</code>, return <em>the number of <strong>distinct prime factors</strong> in the product of the elements of</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>A number greater than <code>1</code> is called <strong>prime</strong> if it is divisible by only <code>1</code> and itself.</li>
|
||||
<li>An integer <code>val1</code> is a factor of another integer <code>val2</code> if <code>val2 / val1</code> is an integer.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,3,7,10,6]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 2<sup>5</sup> * 3<sup>2</sup> * 5 * 7.
|
||||
There are 4 distinct prime factors so we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,8,16]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 2<sup>10</sup>.
|
||||
There is 1 distinct prime factor so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>forts</code> of length <code>n</code> representing the positions of several forts. <code>forts[i]</code> can be <code>-1</code>, <code>0</code>, or <code>1</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1</code> represents there is <strong>no fort</strong> at the <code>i<sup>th</sup></code> position.</li>
|
||||
<li><code>0</code> indicates there is an <strong>enemy</strong> fort at the <code>i<sup>th</sup></code> position.</li>
|
||||
<li><code>1</code> indicates the fort at the <code>i<sup>th</sup></code> the position is under your command.</li>
|
||||
</ul>
|
||||
|
||||
<p>Now you have decided to move your army from one of your forts at position <code>i</code> to an empty position <code>j</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i, j <= n - 1</code></li>
|
||||
<li>The army travels over enemy forts <strong>only</strong>. Formally, for all <code>k</code> where <code>min(i,j) < k < max(i,j)</code>, <code>forts[k] == 0.</code></li>
|
||||
</ul>
|
||||
|
||||
<p>While moving the army, all the enemy forts that come in the way are <strong>captured</strong>.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> number of enemy forts that can be captured</em>. In case it is <strong>impossible</strong> to move your army, or you do not have any fort under your command, return <code>0</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forts = [1,0,0,-1,0,0,0,0,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.
|
||||
- Moving the army from position 8 to position 3 captures 4 enemy forts.
|
||||
Since 4 is the maximum number of enemy forts that can be captured, we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> forts = [0,0,1,-1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Since no enemy fort can be captured, 0 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= forts.length <= 1000</code></li>
|
||||
<li><code>-1 <= forts[i] <= 1</code></li>
|
||||
</ul>
|
42
leetcode/problem/maximum-tastiness-of-candy-basket.html
Normal file
42
leetcode/problem/maximum-tastiness-of-candy-basket.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>You are given an array of positive integers <code>price</code> where <code>price[i]</code> denotes the price of the <code>i<sup>th</sup></code> candy and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>The store sells baskets of <code>k</code> <strong>distinct</strong> candies. The <strong>tastiness</strong> of a candy basket is the smallest absolute difference of the <strong>prices</strong> of any two candies in the basket.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> tastiness of a candy basket.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> price = [13,5,1,8,21,2], k = 3
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> Choose the candies with the prices [13,5,21].
|
||||
The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8.
|
||||
It can be proven that 8 is the maximum tastiness that can be achieved.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> price = [1,3,1], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Choose the candies with the prices [1,3].
|
||||
The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2.
|
||||
It can be proven that 2 is the maximum tastiness that can be achieved.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> price = [7,7,7,7], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= k <= price.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= price[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
50
leetcode/problem/minimize-the-maximum-of-two-arrays.html
Normal file
50
leetcode/problem/minimize-the-maximum-of-two-arrays.html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>We have two arrays <code>arr1</code> and <code>arr2</code> which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr1</code> contains <code>uniqueCnt1</code> <strong>distinct</strong> positive integers, each of which is <strong>not divisible</strong> by <code>divisor1</code>.</li>
|
||||
<li><code>arr2</code> contains <code>uniqueCnt2</code> <strong>distinct</strong> positive integers, each of which is <strong>not divisible</strong> by <code>divisor2</code>.</li>
|
||||
<li><strong>No</strong> integer is present in both <code>arr1</code> and <code>arr2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given <code>divisor1</code>, <code>divisor2</code>, <code>uniqueCnt1</code>, and <code>uniqueCnt2</code>, return <em>the <strong>minimum possible maximum</strong> integer that can be present in either array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
We can distribute the first 4 natural numbers into arr1 and arr2.
|
||||
arr1 = [1] and arr2 = [2,3,4].
|
||||
We can see that both arrays satisfy all the conditions.
|
||||
Since the maximum value is 4, we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
Here arr1 = [1,2], and arr2 = [3] satisfy all conditions.
|
||||
Since the maximum value is 3, we return it.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong>
|
||||
Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6].
|
||||
It can be shown that it is not possible to obtain a lower maximum satisfying all conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= divisor1, divisor2 <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= uniqueCnt1, uniqueCnt2 < 10<sup>9</sup></code></li>
|
||||
<li><code>2 <= uniqueCnt1 + uniqueCnt2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
41
leetcode/problem/number-of-great-partitions.html
Normal file
41
leetcode/problem/number-of-great-partitions.html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers and an integer <code>k</code>.</p>
|
||||
|
||||
<p><strong>Partition</strong> the array into two ordered <strong>groups</strong> such that each element is in exactly <strong>one</strong> group. A partition is called great if the <strong>sum</strong> of elements of each group is greater than or equal to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> great partitions</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Two partitions are considered distinct if some element <code>nums[i]</code> is in different groups in the two partitions.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4], k = 4
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3], k = 4
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no great partitions for this array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,6], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can either put nums[0] in the first partition or in the second partition.
|
||||
The great partitions will be ([6], [6]) and ([6], [6]).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, k <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,53 @@
|
||||
<p>You are given a string <code>s</code> consisting of digits from <code>1</code> to <code>9</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A partition of a string <code>s</code> is called <strong>good</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each digit of <code>s</code> is part of <strong>exactly</strong> one substring.</li>
|
||||
<li>The value of each substring is less than or equal to <code>k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of substrings in a <strong>good</strong> partition of</em> <code>s</code>. If no <strong>good</strong> partition of <code>s</code> exists, return <code>-1</code>.</p>
|
||||
|
||||
<p><b>Note</b> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>value</strong> of a string is its result when interpreted as an integer. For example, the value of <code>"123"</code> is <code>123</code> and the value of <code>"1"</code> is <code>1</code>.</li>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "165462", k = 60
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can partition the string into substrings "16", "54", "6", and "2". Each substring has a value less than or equal to k = 60.
|
||||
It can be shown that we cannot partition the string into less than 4 substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "238182", k = 5
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no good partition for this string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is a digit from <code>'1'</code> to <code>'9'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
46
leetcode/problem/reward-top-k-students.html
Normal file
46
leetcode/problem/reward-top-k-students.html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given two string arrays <code>positive_feedback</code> and <code>negative_feedback</code>, containing the words denoting positive and negative feedback, respectively. Note that <strong>no</strong> word is both positive and negative.</p>
|
||||
|
||||
<p>Initially every student has <code>0</code> points. Each positive word in a feedback report <strong>increases</strong> the points of a student by <code>3</code>, whereas each negative word <strong>decreases</strong> the points by <code>1</code>.</p>
|
||||
|
||||
<p>You are given <code>n</code> feedback reports, represented by a <strong>0-indexed</strong> string array <code>report</code> and a <strong>0-indexed</strong> integer array <code>student_id</code>, where <code>student_id[i]</code> represents the ID of the student who has received the feedback report <code>report[i]</code>. The ID of each student is <strong>unique</strong>.</p>
|
||||
|
||||
<p>Given an integer <code>k</code>, return <em>the top </em><code>k</code><em> students after ranking them in <strong>non-increasing</strong> order by their points</em>. In case more than one student has the same points, the one with the lower ID ranks higher.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious","the student is smart"], student_id = [1,2], k = 2
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong>
|
||||
Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious","the student is smart"], student_id = [1,2], k = 2
|
||||
<strong>Output:</strong> [2,1]
|
||||
<strong>Explanation:</strong>
|
||||
- The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points.
|
||||
- The student with ID 2 has 1 positive feedback, so he has 3 points.
|
||||
Since student 2 has more points, [2,1] is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= positive_feedback.length, negative_feedback.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= positive_feedback[i].length, negative_feedback[j].length <= 100</code></li>
|
||||
<li>Both <code>positive_feedback[i]</code> and <code>negative_feedback[j]</code> consists of lowercase English letters.</li>
|
||||
<li>No word is present in both <code>positive_feedback</code> and <code>negative_feedback</code>.</li>
|
||||
<li><code>n == report.length == student_id.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>report[i]</code> consists of lowercase English letters and spaces <code>' '</code>.</li>
|
||||
<li>There is a single space between consecutive words of <code>report[i]</code>.</li>
|
||||
<li><code>1 <= report[i].length <= 100</code></li>
|
||||
<li><code>1 <= student_id[i] <= 10<sup>9</sup></code></li>
|
||||
<li>All the values of <code>student_id[i]</code> are <strong>unique</strong>.</li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <strong>circular</strong> string array <code>words</code> and a string <code>target</code>. A <strong>circular array</strong> means that the array's end connects to the array's beginning.</p>
|
||||
|
||||
<ul>
|
||||
<li>Formally, the next element of <code>words[i]</code> is <code>words[(i + 1) % n]</code> and the previous element of <code>words[i]</code> is <code>words[(i - 1 + n) % n]</code>, where <code>n</code> is the length of <code>words</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Starting from <code>startIndex</code>, you can move to either the next word or the previous word with <code>1</code> step at a time.</p>
|
||||
|
||||
<p>Return <em>the <strong>shortest</strong> distance needed to reach the string</em> <code>target</code>. If the string <code>target</code> does not exist in <code>words</code>, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We start from index 1 and can reach "hello" by
|
||||
- moving 3 units to the right to reach index 4.
|
||||
- moving 2 units to the left to reach index 4.
|
||||
- moving 4 units to the right to reach index 0.
|
||||
- moving 1 unit to the left to reach index 0.
|
||||
The shortest distance to reach "hello" is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We start from index 0 and can reach "leetcode" by
|
||||
- moving 2 units to the right to reach index 3.
|
||||
- moving 1 unit to the left to reach index 3.
|
||||
The shortest distance to reach "leetcode" is 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["i","eat","leetcode"], target = "ate", startIndex = 0
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Since "ate" does not exist in <code>words</code>, we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> and <code>target</code> consist of only lowercase English letters.</li>
|
||||
<li><code>0 <= startIndex < words.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>You are given a string <code>s</code> consisting of the characters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code> and a non-negative integer <code>k</code>. Each minute, you may take either the <strong>leftmost</strong> character of <code>s</code>, or the <strong>rightmost</strong> character of <code>s</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of minutes needed for you to take <strong>at least</strong> </em><code>k</code><em> of each character, or return </em><code>-1</code><em> if it is not possible to take </em><code>k</code><em> of each character.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabaaaacaabc", k = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
|
||||
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
|
||||
A total of 3 + 5 = 8 minutes is needed.
|
||||
It can be proven that 8 is the minimum number of minutes needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "a", k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is not possible to take one 'b' or 'c' so return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of only the letters <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li>
|
||||
<li><code>0 <= k <= s.length</code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user