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
f3b23d1fc3
commit
f5bf94d67e
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
178
leetcode-cn/originData/count-integers-in-intervals.json
Normal file
178
leetcode-cn/originData/count-integers-in-intervals.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
177
leetcode-cn/originData/find-the-k-beauty-of-a-number.json
Normal file
177
leetcode-cn/originData/find-the-k-beauty-of-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
File diff suppressed because one or more lines are too long
158
leetcode-cn/originData/number-of-ways-to-split-array.json
Normal file
158
leetcode-cn/originData/number-of-ways-to-split-array.json
Normal file
File diff suppressed because one or more lines are too long
172
leetcode-cn/originData/substring-with-largest-variance.json
Normal file
172
leetcode-cn/originData/substring-with-largest-variance.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,37 @@
|
||||
<p>Alice 管理着一家公司,并租用大楼的部分楼层作为办公空间。Alice 决定将一些楼层作为 <strong>特殊楼层</strong> ,仅用于放松。</p>
|
||||
|
||||
<p>给你两个整数 <code>bottom</code> 和 <code>top</code> ,表示 Alice 租用了从 <code>bottom</code> 到 <code>top</code>(含 <code>bottom</code> 和 <code>top</code> 在内)的所有楼层。另给你一个整数数组 <code>special</code> ,其中 <code>special[i]</code> 表示 Alice 指定用于放松的特殊楼层。</p>
|
||||
|
||||
<p>返回不含特殊楼层的 <strong>最大</strong> 连续楼层数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>bottom = 2, top = 9, special = [4,6]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>下面列出的是不含特殊楼层的连续楼层范围:
|
||||
- (2, 3) ,楼层数为 2 。
|
||||
- (5, 5) ,楼层数为 1 。
|
||||
- (7, 9) ,楼层数为 3 。
|
||||
因此,返回最大连续楼层数 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>bottom = 6, top = 8, special = [7,6,8]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>每层楼都被规划为特殊楼层,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= special.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= bottom <= special[i] <= top <= 10<sup>9</sup></code></li>
|
||||
<li><code>special</code> 中的所有值 <strong>互不相同</strong></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的整数数组 <code>nums</code> 。<br />
|
||||
<span style="">如果以下描述为真,那么</span><span style=""> </span><code>nums</code> 在下标 <code>i</code> 处有一个 <strong>合法的分割</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>前 <code>i + 1</code> 个元素的和 <strong>大于等于</strong> 剩下的 <code>n - i - 1</code> 个元素的和。</li>
|
||||
<li>下标 <code>i</code> 的右边 <strong>至少有一个</strong> 元素,也就是说下标 <code>i</code> 满足 <code>0 <= i < n - 1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中的 <strong>合法分割</strong> 方案数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [10,4,-8,7]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>
|
||||
总共有 3 种不同的方案可以将 nums 分割成两个非空的部分:
|
||||
- 在下标 0 处分割 nums 。那么第一部分为 [10] ,和为 10 。第二部分为 [4,-8,7] ,和为 3 。因为 10 >= 3 ,所以 i = 0 是一个合法的分割。
|
||||
- 在下标 1 处分割 nums 。那么第一部分为 [10,4] ,和为 14 。第二部分为 [-8,7] ,和为 -1 。因为 14 >= -1 ,所以 i = 1 是一个合法的分割。
|
||||
- 在下标 2 处分割 nums 。那么第一部分为 [10,4,-8] ,和为 6 。第二部分为 [7] ,和为 7 。因为 6 < 7 ,所以 i = 2 不是一个合法的分割。
|
||||
所以 nums 中总共合法分割方案受为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,3,1,0]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>
|
||||
总共有 2 种 nums 的合法分割:
|
||||
- 在下标 1 处分割 nums 。那么第一部分为 [2,3] ,和为 5 。第二部分为 [1,0] ,和为 1 。因为 5 >= 1 ,所以 i = 1 是一个合法的分割。
|
||||
- 在下标 2 处分割 nums 。那么第一部分为 [2,3,1] ,和为 6 。第二部分为 [0] ,和为 0 。因为 6 >= 0 ,所以 i = 2 是一个合法的分割。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,53 @@
|
||||
<p>一个整数 <code>num</code> 的 <strong>k </strong>美丽值定义为 <code>num</code> 中符合以下条件的 <strong>子字符串</strong> 数目:</p>
|
||||
|
||||
<ul>
|
||||
<li>子字符串长度为 <code>k</code> 。</li>
|
||||
<li>子字符串能整除 <code>num</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你整数 <code>num</code> 和 <code>k</code> ,请你返回<em> </em><code>num</code> 的 k 美丽值。</p>
|
||||
|
||||
<p>注意:</p>
|
||||
|
||||
<ul>
|
||||
<li>允许有 <strong>前缀</strong> <strong>0</strong> 。</li>
|
||||
<li><code>0</code> 不能整除任何值。</li>
|
||||
</ul>
|
||||
|
||||
<p>一个 <strong>子字符串</strong> 是一个字符串里的连续一段字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>num = 240, k = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>以下是 num 里长度为 k 的子字符串:
|
||||
- "<em><strong>24</strong></em>0" 中的 "24" :24 能整除 240 。
|
||||
- "2<em><strong>40</strong></em>" 中的 "40" :40 能整除 240 。
|
||||
所以,k 美丽值为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>num = 430043, k = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>以下是 num 里长度为 k 的子字符串:
|
||||
- "<em><strong>43</strong></em>0043" 中的 "43" :43 能整除 430043 。
|
||||
- "4<em><strong>30</strong></em>043" 中的 "30" :30 不能整除 430043 。
|
||||
- "43<em><strong>00</strong></em>43" 中的 "00" :0 不能整除 430043 。
|
||||
- "430<em><strong>04</strong></em>3" 中的 "04" :4 不能整除 430043 。
|
||||
- "4300<em><strong>43</strong></em>" 中的 "43" :43 能整除 430043 。
|
||||
所以,k 美丽值为 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= num.length</code> (将 <code>num</code> 视为字符串)</li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>对数组 <code>nums</code> 执行 <strong>按位与</strong> 相当于对数组 <code>nums</code> 中的所有整数执行 <strong>按位与</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,对 <code>nums = [1, 5, 3]</code> 来说,按位与等于 <code>1 & 5 & 3 = 1</code> 。</li>
|
||||
<li>同样,对 <code>nums = [7]</code> 而言,按位与等于 <code>7</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个正整数数组 <code>candidates</code> 。计算 <code>candidates</code> 中的数字每种组合下 <strong>按位与</strong> 的结果。 <code>candidates</code> 中的每个数字在每种组合中只能使用 <strong>一次</strong> 。</p>
|
||||
|
||||
<p>返回按位与结果大于 <code>0</code> 的 <strong>最长</strong> 组合的长度<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candidates = [16,17,71,62,12,24,14]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>组合 [16,17,62,24] 的按位与结果是 16 & 17 & 62 & 24 = 16 > 0 。
|
||||
组合长度是 4 。
|
||||
可以证明不存在按位与结果大于 0 且长度大于 4 的组合。
|
||||
注意,符合长度最大的组合可能不止一种。
|
||||
例如,组合 [62,12,24,14] 的按位与结果是 62 & 12 & 24 & 14 = 8 > 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candidates = [8,8]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>最长组合是 [8,8] ,按位与结果 8 & 8 = 8 > 0 。
|
||||
组合长度是 2 ,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candidates.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candidates[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>字符串的 <strong>波动</strong> 定义为子字符串中出现次数 <strong>最多</strong> 的字符次数与出现次数 <strong>最少</strong> 的字符次数之差。</p>
|
||||
|
||||
<p>给你一个字符串 <code>s</code> ,它只包含小写英文字母。请你返回 <code>s</code> 里所有 <strong>子字符串的</strong> <strong>最大波动</strong> 值。</p>
|
||||
|
||||
<p><strong>子字符串</strong> 是一个字符串的一段连续字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "aababbb"
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>
|
||||
所有可能的波动值和它们对应的子字符串如以下所示:
|
||||
- 波动值为 0 的子字符串:"a" ,"aa" ,"ab" ,"abab" ,"aababb" ,"ba" ,"b" ,"bb" 和 "bbb" 。
|
||||
- 波动值为 1 的子字符串:"aab" ,"aba" ,"abb" ,"aabab" ,"ababb" ,"aababbb" 和 "bab" 。
|
||||
- 波动值为 2 的子字符串:"aaba" ,"ababbb" ,"abbb" 和 "babb" 。
|
||||
- 波动值为 3 的子字符串 "babbb" 。
|
||||
所以,最大可能波动值为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcde"
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
s 中没有字母出现超过 1 次,所以 s 中每个子字符串的波动值都是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>给你一个二维整数数组 <code>tiles</code> ,其中 <code>tiles[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> ,表示所有在 <code>l<sub>i</sub> <= j <= r<sub>i</sub></code> 之间的每个瓷砖位置 <code>j</code> 都被涂成了白色。</p>
|
||||
|
||||
<p>同时给你一个整数 <code>carpetLen</code> ,表示可以放在 <strong>任何位置</strong> 的一块毯子。</p>
|
||||
|
||||
<p>请你返回使用这块毯子,<strong>最多</strong> 可以盖住多少块瓷砖。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png" style="width: 644px; height: 158px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>将毯子从瓷砖 10 开始放置。
|
||||
总共覆盖 9 块瓷砖,所以返回 9 。
|
||||
注意可能有其他方案也可以覆盖 9 块瓷砖。
|
||||
可以看出,瓷砖无法覆盖超过 9 块瓷砖。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png" style="width: 231px; height: 168px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tiles = [[10,11],[1,1]], carpetLen = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>将毯子从瓷砖 10 开始放置。
|
||||
总共覆盖 2 块瓷砖,所以我们返回 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tiles.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>tiles[i].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub> <= r<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= carpetLen <= 10<sup>9</sup></code></li>
|
||||
<li><code>tiles</code> 互相 <strong>不会重叠</strong> 。</li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>words</code> ,其中 <code>words[i]</code> 由小写英文字符组成。</p>
|
||||
|
||||
<p>在一步操作中,需要选出任一下标 <code>i</code> ,从 <code>words</code> 中 <strong>删除</strong> <code>words[i]</code> 。其中下标 <code>i</code> 需要同时满足下述两个条件:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>0 < i < words.length</code></li>
|
||||
<li><code>words[i - 1]</code> 和 <code>words[i]</code> 是 <strong>字母异位词</strong> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>只要可以选出满足条件的下标,就一直执行这个操作。</p>
|
||||
|
||||
<p>在执行所有操作后,返回 <code>words</code> 。可以证明,按任意顺序为每步操作选择下标都会得到相同的结果。</p>
|
||||
|
||||
<p><strong>字母异位词</strong> 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。例如,<code>"dacb"</code> 是 <code>"abdc"</code> 的一个字母异位词。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["abba","baba","bbaa","cd","cd"]
|
||||
<strong>输出:</strong>["abba","cd"]
|
||||
<strong>解释:</strong>
|
||||
获取结果数组的方法之一是执行下述步骤:
|
||||
- 由于 words[2] = "bbaa" 和 words[1] = "baba" 是字母异位词,选择下标 2 并删除 words[2] 。
|
||||
现在 words = ["abba","baba","cd","cd"] 。
|
||||
- 由于 words[1] = "baba" 和 words[0] = "abba" 是字母异位词,选择下标 1 并删除 words[1] 。
|
||||
现在 words = ["abba","cd","cd"] 。
|
||||
- 由于 words[2] = "cd" 和 words[1] = "cd" 是字母异位词,选择下标 2 并删除 words[2] 。
|
||||
现在 words = ["abba","cd"] 。
|
||||
无法再执行任何操作,所以 ["abba","cd"] 是最终答案。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["a","b","c","d","e"]
|
||||
<strong>输出:</strong>["a","b","c","d","e"]
|
||||
<strong>解释:</strong>
|
||||
words 中不存在互为字母异位词的两个相邻字符串,所以无需执行任何操作。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>给你区间的 <strong>空</strong> 集,请你设计并实现满足要求的数据结构:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>新增:</strong>添加一个区间到这个区间集合中。</li>
|
||||
<li><strong>统计:</strong>计算出现在 <strong>至少一个</strong> 区间中的整数个数。</li>
|
||||
</ul>
|
||||
|
||||
<p>实现 <code>CountIntervals</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>CountIntervals()</code> 使用区间的空集初始化对象</li>
|
||||
<li><code>void add(int left, int right)</code> 添加区间 <code>[left, right]</code> 到区间集合之中。</li>
|
||||
<li><code>int count()</code> 返回出现在 <strong>至少一个</strong> 区间中的整数个数。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意:</strong>区间 <code>[left, right]</code> 表示满足 <code>left <= x <= right</code> 的所有整数 <code>x</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
["CountIntervals", "add", "add", "count", "add", "count"]
|
||||
[[], [2, 3], [7, 10], [], [5, 8], []]
|
||||
<strong>输出</strong>
|
||||
[null, null, null, 6, null, 8]
|
||||
|
||||
<strong>解释</strong>
|
||||
CountIntervals countIntervals = new CountIntervals(); // 用一个区间空集初始化对象
|
||||
countIntervals.add(2, 3); // 将 [2, 3] 添加到区间集合中
|
||||
countIntervals.add(7, 10); // 将 [7, 10] 添加到区间集合中
|
||||
countIntervals.count(); // 返回 6
|
||||
// 整数 2 和 3 出现在区间 [2, 3] 中
|
||||
// 整数 7、8、9、10 出现在区间 [7, 10] 中
|
||||
countIntervals.add(5, 8); // 将 [5, 8] 添加到区间集合中
|
||||
countIntervals.count(); // 返回 8
|
||||
// 整数 2 和 3 出现在区间 [2, 3] 中
|
||||
// 整数 5 和 6 出现在区间 [5, 8] 中
|
||||
// 整数 7 和 8 出现在区间 [5, 8] 和区间 [7, 10] 中
|
||||
// 整数 9 和 10 出现在区间 [7, 10] 中</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>9</sup></code></li>
|
||||
<li>最多调用 <code>add</code> 和 <code>count</code> 方法 <strong>总计</strong> <code>10<sup>5</sup></code> 次</li>
|
||||
<li>调用 <code>count</code> 方法至少一次</li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be <strong>special floors</strong>, used for relaxation only.</p>
|
||||
|
||||
<p>You are given two integers <code>bottom</code> and <code>top</code>, which denote that Alice has rented all the floors from <code>bottom</code> to <code>top</code> (<strong>inclusive</strong>). You are also given the integer array <code>special</code>, where <code>special[i]</code> denotes a special floor that Alice has designated for relaxation.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of consecutive floors without a special floor</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> bottom = 2, top = 9, special = [4,6]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following are the ranges (inclusive) of consecutive floors without a special floor:
|
||||
- (2, 3) with a total amount of 2 floors.
|
||||
- (5, 5) with a total amount of 1 floor.
|
||||
- (7, 9) with a total amount of 3 floors.
|
||||
Therefore, we return the maximum number which is 3 floors.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> bottom = 6, top = 8, special = [7,6,8]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Every floor rented is a special floor, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= special.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= bottom <= special[i] <= top <= 10<sup>9</sup></code></li>
|
||||
<li>All the values of <code>special</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p><code>nums</code> contains a <strong>valid split</strong> at index <code>i</code> if the following are true:</p>
|
||||
|
||||
<ul>
|
||||
<li>The sum of the first <code>i + 1</code> elements is <strong>greater than or equal to</strong> the sum of the last <code>n - i - 1</code> elements.</li>
|
||||
<li>There is <strong>at least one</strong> element to the right of <code>i</code>. That is, <code>0 <= i < n - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>valid splits</strong> in</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,4,-8,7]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
There are three ways of splitting nums into two non-empty parts:
|
||||
- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split.
|
||||
- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split.
|
||||
- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split.
|
||||
Thus, the number of valid splits in nums is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,1,0]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
There are two valid splits in nums:
|
||||
- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split.
|
||||
- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>The <strong>k-beauty</strong> of an integer <code>num</code> is defined as the number of <strong>substrings</strong> of <code>num</code> when it is read as a string that meet the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>It has a length of <code>k</code>.</li>
|
||||
<li>It is a divisor of <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given integers <code>num</code> and <code>k</code>, return <em>the k-beauty of </em><code>num</code>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Leading zeros</strong> are allowed.</li>
|
||||
<li><code>0</code> is not a divisor of any value.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 240, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The following are the substrings of num of length k:
|
||||
- "24" from "<strong><u>24</u></strong>0": 24 is a divisor of 240.
|
||||
- "40" from "2<u><strong>40</strong></u>": 40 is a divisor of 240.
|
||||
Therefore, the k-beauty is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 430043, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The following are the substrings of num of length k:
|
||||
- "43" from "<u><strong>43</strong></u>0043": 43 is a divisor of 430043.
|
||||
- "30" from "4<u><strong>30</strong></u>043": 30 is not a divisor of 430043.
|
||||
- "00" from "43<u><strong>00</strong></u>43": 0 is not a divisor of 430043.
|
||||
- "04" from "430<u><strong>04</strong></u>3": 4 is not a divisor of 430043.
|
||||
- "43" from "4300<u><strong>43</strong></u>": 43 is a divisor of 430043.
|
||||
Therefore, the k-beauty is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= num.length</code> (taking <code>num</code> as a string)</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>The <strong>bitwise AND</strong> of an array <code>nums</code> is the bitwise AND of all integers in <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>nums = [1, 5, 3]</code>, the bitwise AND is equal to <code>1 & 5 & 3 = 1</code>.</li>
|
||||
<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given an array of positive integers <code>candidates</code>. Evaluate the <strong>bitwise AND</strong> of every <strong>combination</strong> of numbers of <code>candidates</code>. Each number in <code>candidates</code> may only be used <strong>once</strong> in each combination.</p>
|
||||
|
||||
<p>Return <em>the size of the <strong>largest</strong> combination of </em><code>candidates</code><em> with a bitwise AND <strong>greater</strong> than </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candidates = [16,17,71,62,12,24,14]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
|
||||
The size of the combination is 4.
|
||||
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
|
||||
Note that more than one combination may have the largest size.
|
||||
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candidates = [8,8]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
|
||||
The size of the combination is 2, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candidates.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candidates[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
|
||||
|
||||
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aababbb"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
All possible variances along with their respective substrings are listed below:
|
||||
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
|
||||
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
|
||||
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
|
||||
- Variance 3 for substring "babbb".
|
||||
Since the largest possible variance is 3, we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcde"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No letter occurs more than once in s, so the variance of every substring is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>You are given a 2D integer array <code>tiles</code> where <code>tiles[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents that every tile <code>j</code> in the range <code>l<sub>i</sub> <= j <= r<sub>i</sub></code> is colored white.</p>
|
||||
|
||||
<p>You are also given an integer <code>carpetLen</code>, the length of a single carpet that can be placed <strong>anywhere</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of white tiles that can be covered by the carpet</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png" style="width: 644px; height: 158px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Place the carpet starting on tile 10.
|
||||
It covers 9 white tiles, so we return 9.
|
||||
Note that there may be other places where the carpet covers 9 white tiles.
|
||||
It can be shown that the carpet cannot cover more than 9 white tiles.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png" style="width: 231px; height: 168px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> tiles = [[10,11],[1,1]], carpetLen = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Place the carpet starting on tile 10.
|
||||
It covers 2 white tiles, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tiles.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>tiles[i].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub> <= r<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= carpetLen <= 10<sup>9</sup></code></li>
|
||||
<li>The <code>tiles</code> are <strong>non-overlapping</strong>.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>, where <code>words[i]</code> consists of lowercase English letters.</p>
|
||||
|
||||
<p>In one operation, select any index <code>i</code> such that <code>0 < i < words.length</code> and <code>words[i - 1]</code> and <code>words[i]</code> are <strong>anagrams</strong>, and <strong>delete</strong> <code>words[i]</code> from <code>words</code>. Keep performing this operation as long as you can select an index that satisfies the conditions.</p>
|
||||
|
||||
<p>Return <code>words</code> <em>after performing all operations</em>. It can be shown that selecting the indices for each operation in <strong>any</strong> arbitrary order will lead to the same result.</p>
|
||||
|
||||
<p>An <strong>Anagram</strong> is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example, <code>"dacb"</code> is an anagram of <code>"abdc"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abba","baba","bbaa","cd","cd"]
|
||||
<strong>Output:</strong> ["abba","cd"]
|
||||
<strong>Explanation:</strong>
|
||||
One of the ways we can obtain the resultant array is by using the following operations:
|
||||
- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
|
||||
Now words = ["abba","baba","cd","cd"].
|
||||
- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
|
||||
Now words = ["abba","cd","cd"].
|
||||
- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
|
||||
Now words = ["abba","cd"].
|
||||
We can no longer perform any operations, so ["abba","cd"] is the final answer.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","b","c","d","e"]
|
||||
<strong>Output:</strong> ["a","b","c","d","e"]
|
||||
<strong>Explanation:</strong>
|
||||
No two adjacent strings in words are anagrams of each other, so no operations are performed.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>Given an <strong>empty</strong> set of intervals, implement a data structure that can:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Add</strong> an interval to the set of intervals.</li>
|
||||
<li><strong>Count</strong> the number of integers that are present in <strong>at least one</strong> interval.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>CountIntervals</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>CountIntervals()</code> Initializes the object with an empty set of intervals.</li>
|
||||
<li><code>void add(int left, int right)</code> Adds the interval <code>[left, right]</code> to the set of intervals.</li>
|
||||
<li><code>int count()</code> Returns the number of integers that are present in <strong>at least one</strong> interval.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that an interval <code>[left, right]</code> denotes all the integers <code>x</code> where <code>left <= x <= right</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["CountIntervals", "add", "add", "count", "add", "count"]
|
||||
[[], [2, 3], [7, 10], [], [5, 8], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, 6, null, 8]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
CountIntervals countIntervals = new CountIntervals(); // initialize the object with an empty set of intervals.
|
||||
countIntervals.add(2, 3); // add [2, 3] to the set of intervals.
|
||||
countIntervals.add(7, 10); // add [7, 10] to the set of intervals.
|
||||
countIntervals.count(); // return 6
|
||||
// the integers 2 and 3 are present in the interval [2, 3].
|
||||
// the integers 7, 8, 9, and 10 are present in the interval [7, 10].
|
||||
countIntervals.add(5, 8); // add [5, 8] to the set of intervals.
|
||||
countIntervals.count(); // return 8
|
||||
// the integers 2 and 3 are present in the interval [2, 3].
|
||||
// the integers 5 and 6 are present in the interval [5, 8].
|
||||
// the integers 7 and 8 are present in the intervals [5, 8] and [7, 10].
|
||||
// the integers 9 and 10 are present in the interval [7, 10].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>add</code> and <code>count</code>.</li>
|
||||
<li>At least <strong>one</strong> call will be made to <code>count</code>.</li>
|
||||
</ul>
|
@ -48,7 +48,9 @@ def save_problem(title,content):
|
||||
f.write(content)
|
||||
|
||||
def get_proble_content(problemUrl,title):
|
||||
response = requests.get(problemUrl)
|
||||
response = requests.get(problemUrl, headers = {
|
||||
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
|
||||
})
|
||||
setCookie = response.headers["Set-Cookie"]
|
||||
'''
|
||||
print(setCookie)
|
||||
|
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
175
leetcode/originData/count-integers-in-intervals.json
Normal file
175
leetcode/originData/count-integers-in-intervals.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
174
leetcode/originData/find-the-k-beauty-of-a-number.json
Normal file
174
leetcode/originData/find-the-k-beauty-of-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
186
leetcode/originData/maximum-white-tiles-covered-by-a-carpet.json
Normal file
186
leetcode/originData/maximum-white-tiles-covered-by-a-carpet.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode/originData/number-of-ways-to-split-array.json
Normal file
168
leetcode/originData/number-of-ways-to-split-array.json
Normal file
File diff suppressed because one or more lines are too long
169
leetcode/originData/substring-with-largest-variance.json
Normal file
169
leetcode/originData/substring-with-largest-variance.json
Normal file
File diff suppressed because one or more lines are too long
50
leetcode/problem/count-integers-in-intervals.html
Normal file
50
leetcode/problem/count-integers-in-intervals.html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>Given an <strong>empty</strong> set of intervals, implement a data structure that can:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Add</strong> an interval to the set of intervals.</li>
|
||||
<li><strong>Count</strong> the number of integers that are present in <strong>at least one</strong> interval.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>CountIntervals</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>CountIntervals()</code> Initializes the object with an empty set of intervals.</li>
|
||||
<li><code>void add(int left, int right)</code> Adds the interval <code>[left, right]</code> to the set of intervals.</li>
|
||||
<li><code>int count()</code> Returns the number of integers that are present in <strong>at least one</strong> interval.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that an interval <code>[left, right]</code> denotes all the integers <code>x</code> where <code>left <= x <= right</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["CountIntervals", "add", "add", "count", "add", "count"]
|
||||
[[], [2, 3], [7, 10], [], [5, 8], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, 6, null, 8]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
CountIntervals countIntervals = new CountIntervals(); // initialize the object with an empty set of intervals.
|
||||
countIntervals.add(2, 3); // add [2, 3] to the set of intervals.
|
||||
countIntervals.add(7, 10); // add [7, 10] to the set of intervals.
|
||||
countIntervals.count(); // return 6
|
||||
// the integers 2 and 3 are present in the interval [2, 3].
|
||||
// the integers 7, 8, 9, and 10 are present in the interval [7, 10].
|
||||
countIntervals.add(5, 8); // add [5, 8] to the set of intervals.
|
||||
countIntervals.count(); // return 8
|
||||
// the integers 2 and 3 are present in the interval [2, 3].
|
||||
// the integers 5 and 6 are present in the interval [5, 8].
|
||||
// the integers 7 and 8 are present in the intervals [5, 8] and [7, 10].
|
||||
// the integers 9 and 10 are present in the interval [7, 10].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>add</code> and <code>count</code>.</li>
|
||||
<li>At least <strong>one</strong> call will be made to <code>count</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>, where <code>words[i]</code> consists of lowercase English letters.</p>
|
||||
|
||||
<p>In one operation, select any index <code>i</code> such that <code>0 < i < words.length</code> and <code>words[i - 1]</code> and <code>words[i]</code> are <strong>anagrams</strong>, and <strong>delete</strong> <code>words[i]</code> from <code>words</code>. Keep performing this operation as long as you can select an index that satisfies the conditions.</p>
|
||||
|
||||
<p>Return <code>words</code> <em>after performing all operations</em>. It can be shown that selecting the indices for each operation in <strong>any</strong> arbitrary order will lead to the same result.</p>
|
||||
|
||||
<p>An <strong>Anagram</strong> is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example, <code>"dacb"</code> is an anagram of <code>"abdc"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abba","baba","bbaa","cd","cd"]
|
||||
<strong>Output:</strong> ["abba","cd"]
|
||||
<strong>Explanation:</strong>
|
||||
One of the ways we can obtain the resultant array is by using the following operations:
|
||||
- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
|
||||
Now words = ["abba","baba","cd","cd"].
|
||||
- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
|
||||
Now words = ["abba","cd","cd"].
|
||||
- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
|
||||
Now words = ["abba","cd"].
|
||||
We can no longer perform any operations, so ["abba","cd"] is the final answer.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","b","c","d","e"]
|
||||
<strong>Output:</strong> ["a","b","c","d","e"]
|
||||
<strong>Explanation:</strong>
|
||||
No two adjacent strings in words are anagrams of each other, so no operations are performed.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
51
leetcode/problem/find-the-k-beauty-of-a-number.html
Normal file
51
leetcode/problem/find-the-k-beauty-of-a-number.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>The <strong>k-beauty</strong> of an integer <code>num</code> is defined as the number of <strong>substrings</strong> of <code>num</code> when it is read as a string that meet the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>It has a length of <code>k</code>.</li>
|
||||
<li>It is a divisor of <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given integers <code>num</code> and <code>k</code>, return <em>the k-beauty of </em><code>num</code>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Leading zeros</strong> are allowed.</li>
|
||||
<li><code>0</code> is not a divisor of any value.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 240, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The following are the substrings of num of length k:
|
||||
- "24" from "<strong><u>24</u></strong>0": 24 is a divisor of 240.
|
||||
- "40" from "2<u><strong>40</strong></u>": 40 is a divisor of 240.
|
||||
Therefore, the k-beauty is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 430043, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The following are the substrings of num of length k:
|
||||
- "43" from "<u><strong>43</strong></u>0043": 43 is a divisor of 430043.
|
||||
- "30" from "4<u><strong>30</strong></u>043": 30 is not a divisor of 430043.
|
||||
- "00" from "43<u><strong>00</strong></u>43": 0 is not a divisor of 430043.
|
||||
- "04" from "430<u><strong>04</strong></u>3": 4 is not a divisor of 430043.
|
||||
- "43" from "4300<u><strong>43</strong></u>": 43 is a divisor of 430043.
|
||||
Therefore, the k-beauty is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= num.length</code> (taking <code>num</code> as a string)</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>The <strong>bitwise AND</strong> of an array <code>nums</code> is the bitwise AND of all integers in <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>nums = [1, 5, 3]</code>, the bitwise AND is equal to <code>1 & 5 & 3 = 1</code>.</li>
|
||||
<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given an array of positive integers <code>candidates</code>. Evaluate the <strong>bitwise AND</strong> of every <strong>combination</strong> of numbers of <code>candidates</code>. Each number in <code>candidates</code> may only be used <strong>once</strong> in each combination.</p>
|
||||
|
||||
<p>Return <em>the size of the <strong>largest</strong> combination of </em><code>candidates</code><em> with a bitwise AND <strong>greater</strong> than </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candidates = [16,17,71,62,12,24,14]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
|
||||
The size of the combination is 4.
|
||||
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
|
||||
Note that more than one combination may have the largest size.
|
||||
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candidates = [8,8]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
|
||||
The size of the combination is 2, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candidates.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candidates[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be <strong>special floors</strong>, used for relaxation only.</p>
|
||||
|
||||
<p>You are given two integers <code>bottom</code> and <code>top</code>, which denote that Alice has rented all the floors from <code>bottom</code> to <code>top</code> (<strong>inclusive</strong>). You are also given the integer array <code>special</code>, where <code>special[i]</code> denotes a special floor that Alice has designated for relaxation.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of consecutive floors without a special floor</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> bottom = 2, top = 9, special = [4,6]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following are the ranges (inclusive) of consecutive floors without a special floor:
|
||||
- (2, 3) with a total amount of 2 floors.
|
||||
- (5, 5) with a total amount of 1 floor.
|
||||
- (7, 9) with a total amount of 3 floors.
|
||||
Therefore, we return the maximum number which is 3 floors.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> bottom = 6, top = 8, special = [7,6,8]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Every floor rented is a special floor, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= special.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= bottom <= special[i] <= top <= 10<sup>9</sup></code></li>
|
||||
<li>All the values of <code>special</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>You are given a 2D integer array <code>tiles</code> where <code>tiles[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents that every tile <code>j</code> in the range <code>l<sub>i</sub> <= j <= r<sub>i</sub></code> is colored white.</p>
|
||||
|
||||
<p>You are also given an integer <code>carpetLen</code>, the length of a single carpet that can be placed <strong>anywhere</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of white tiles that can be covered by the carpet</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/example1drawio3.png" style="width: 644px; height: 158px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Place the carpet starting on tile 10.
|
||||
It covers 9 white tiles, so we return 9.
|
||||
Note that there may be other places where the carpet covers 9 white tiles.
|
||||
It can be shown that the carpet cannot cover more than 9 white tiles.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/24/example2drawio.png" style="width: 231px; height: 168px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> tiles = [[10,11],[1,1]], carpetLen = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Place the carpet starting on tile 10.
|
||||
It covers 2 white tiles, so we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tiles.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>tiles[i].length == 2</code></li>
|
||||
<li><code>1 <= l<sub>i</sub> <= r<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= carpetLen <= 10<sup>9</sup></code></li>
|
||||
<li>The <code>tiles</code> are <strong>non-overlapping</strong>.</li>
|
||||
</ul>
|
43
leetcode/problem/number-of-ways-to-split-array.html
Normal file
43
leetcode/problem/number-of-ways-to-split-array.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p><code>nums</code> contains a <strong>valid split</strong> at index <code>i</code> if the following are true:</p>
|
||||
|
||||
<ul>
|
||||
<li>The sum of the first <code>i + 1</code> elements is <strong>greater than or equal to</strong> the sum of the last <code>n - i - 1</code> elements.</li>
|
||||
<li>There is <strong>at least one</strong> element to the right of <code>i</code>. That is, <code>0 <= i < n - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>valid splits</strong> in</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,4,-8,7]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
There are three ways of splitting nums into two non-empty parts:
|
||||
- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split.
|
||||
- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split.
|
||||
- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split.
|
||||
Thus, the number of valid splits in nums is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,1,0]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
There are two valid splits in nums:
|
||||
- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split.
|
||||
- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
37
leetcode/problem/substring-with-largest-variance.html
Normal file
37
leetcode/problem/substring-with-largest-variance.html
Normal file
@ -0,0 +1,37 @@
|
||||
<p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
|
||||
|
||||
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aababbb"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
All possible variances along with their respective substrings are listed below:
|
||||
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
|
||||
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
|
||||
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
|
||||
- Variance 3 for substring "babbb".
|
||||
Since the largest possible variance is 3, we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcde"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No letter occurs more than once in s, so the variance of every substring is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user