mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
2817184d94
commit
3a14465651
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
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,47 @@
|
||||
<p>给你一个整数 <code>k</code> 和一个整数 <code>x</code> 。</p>
|
||||
|
||||
<p>令 <code>s</code> 为整数 <code>num</code> 的下标从 <strong>1</strong> 开始的二进制表示。我们说一个整数 <code>num</code> 的 <strong>价值</strong> 是满足 <code>i % x == 0</code> 且 <code><font face="monospace">s[i]</font></code> 是 <strong>设置位</strong> 的 <code>i</code> 的数目。</p>
|
||||
|
||||
<p>请你返回<strong> 最大</strong> 整数<em> </em><code>num</code> ,满足从 <code>1</code> 到 <code>num</code> 的所有整数的 <strong>价值</strong> 和小于等于 <code>k</code> 。</p>
|
||||
|
||||
<p><b>注意:</b></p>
|
||||
|
||||
<ul>
|
||||
<li>一个整数二进制表示下 <strong>设置位</strong> 是值为 <code>1</code> 的数位。</li>
|
||||
<li>一个整数的二进制表示下标从右到左编号,比方说如果 <code>s == 11100</code> ,那么 <code>s[4] == 1</code> 且 <code>s[2] == 0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>k = 9, x = 1
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>数字 1 ,2 ,3 ,4 ,5 和 6 二进制表示分别为 "1" ,"10" ,"11" ,"100" ,"101" 和 "110" 。
|
||||
由于 x 等于 1 ,每个数字的价值分别为所有设置位的数目。
|
||||
这些数字的所有设置位数目总数是 9 ,所以前 6 个数字的价值和为 9 。
|
||||
所以答案为 6 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>k = 7, x = 2
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>由于 x 等于 2 ,我们检查每个数字的偶数位。
|
||||
2 和 3 在二进制表示下的第二个数位为设置位,所以它们的价值和为 2 。
|
||||
6 和 7 在二进制表示下的第二个数位为设置位,所以它们的价值和为 2 。
|
||||
8 和 9 在二进制表示下的第四个数位为设置位但第二个数位不是设置位,所以它们的价值和为 2 。
|
||||
数字 1 ,4 和 5 在二进制下偶数位都不是设置位,所以它们的价值和为 0 。
|
||||
10 在二进制表示下的第二个数位和第四个数位都是设置位,所以它的价值为 2 。
|
||||
前 9 个数字的价值和为 6 。
|
||||
前 10 个数字的价值和为 8,超过了 k = 7 ,所以答案为 9 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>15</sup></code></li>
|
||||
<li><code>1 <= x <= 8</code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> 、字符串 <code>a</code> 、字符串 <code>b</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果下标 <code>i</code> 满足以下条件,则认为它是一个 <strong>美丽下标</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= s.length - a.length</code></li>
|
||||
<li><code>s[i..(i + a.length - 1)] == a</code></li>
|
||||
<li>存在下标 <code>j</code> 使得:
|
||||
<ul>
|
||||
<li><code>0 <= j <= s.length - b.length</code></li>
|
||||
<li><code>s[j..(j + b.length - 1)] == b</code></li>
|
||||
<li><code>|j - i| <= k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>以数组形式按<strong> 从小到大排序 </strong>返回美丽下标。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
|
||||
<strong>输出:</strong>[16,33]
|
||||
<strong>解释:</strong>存在 2 个美丽下标:[16,33]。
|
||||
- 下标 16 是美丽下标,因为 s[16..17] == "my" ,且存在下标 4 ,满足 s[4..11] == "squirrel" 且 |16 - 4| <= 15 。
|
||||
- 下标 33 是美丽下标,因为 s[33..34] == "my" ,且存在下标 18 ,满足 s[18..25] == "squirrel" 且 |33 - 18| <= 15 。
|
||||
因此返回 [16,33] 作为结果。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcd", a = "a", b = "a", k = 4
|
||||
<strong>输出:</strong>[0]
|
||||
<strong>解释:</strong>存在 1 个美丽下标:[0]。
|
||||
- 下标 0 是美丽下标,因为 s[0..0] == "a" ,且存在下标 0 ,满足 s[0..0] == "a" 且 |0 - 0| <= 4 。
|
||||
因此返回 [0] 作为结果。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= a.length, b.length <= 10</code></li>
|
||||
<li><code>s</code>、<code>a</code>、和 <code>b</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> 、字符串 <code>a</code> 、字符串 <code>b</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果下标 <code>i</code> 满足以下条件,则认为它是一个 <strong>美丽下标</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= s.length - a.length</code></li>
|
||||
<li><code>s[i..(i + a.length - 1)] == a</code></li>
|
||||
<li>存在下标 <code>j</code> 使得:
|
||||
<ul>
|
||||
<li><code>0 <= j <= s.length - b.length</code></li>
|
||||
<li><code>s[j..(j + b.length - 1)] == b</code></li>
|
||||
<li><code>|j - i| <= k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>以数组形式按<strong> 从小到大排序 </strong>返回美丽下标。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
|
||||
<strong>输出:</strong>[16,33]
|
||||
<strong>解释:</strong>存在 2 个美丽下标:[16,33]。
|
||||
- 下标 16 是美丽下标,因为 s[16..17] == "my" ,且存在下标 4 ,满足 s[4..11] == "squirrel" 且 |16 - 4| <= 15 。
|
||||
- 下标 33 是美丽下标,因为 s[33..34] == "my" ,且存在下标 18 ,满足 s[18..25] == "squirrel" 且 |33 - 18| <= 15 。
|
||||
因此返回 [16,33] 作为结果。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcd", a = "a", b = "a", k = 4
|
||||
<b>输出:</b>[0]
|
||||
<strong>解释:</strong>存在 1 个美丽下标:[0]。
|
||||
- 下标 0 是美丽下标,因为 s[0..0] == "a" ,且存在下标 0 ,满足 s[0..0] == "a" 且 |0 - 0| <= 4 。
|
||||
因此返回 [0] 作为结果。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code>、<code>a</code>、和 <code>b</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个由 <strong>正整数 </strong>组成的数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>返回数组 <code>nums</code> 中所有具有 <strong>最大 </strong>频率的元素的 <strong>总频率 </strong>。</p>
|
||||
|
||||
<p>元素的 <strong>频率 </strong>是指该元素在数组中出现的次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,2,3,1,4]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>元素 1 和 2 的频率为 2 ,是数组中的最大频率。
|
||||
因此具有最大频率的元素在数组中的数量是 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4,5]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>数组中的所有元素的频率都为 1 ,是最大频率。
|
||||
因此具有最大频率的元素在数组中的数量是 5 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>You are given an integer <code>k</code> and an integer <code>x</code>.</p>
|
||||
|
||||
<p>Consider <code>s</code> is the <strong>1-indexed </strong>binary representation of an integer <code>num</code>. The <strong>price</strong> of a number <code>num</code> is the number of <code>i</code>'s such that <code>i % x == 0</code> and <code><font face="monospace">s[i]</font></code> is a <strong>set bit</strong>.</p>
|
||||
|
||||
<p>Return <em>the <b>greatest</b> integer </em><code>num</code><em> such that the sum of <strong>prices</strong> of all numbers from </em><code>1</code><em> to </em><code>num</code><em> is less than or equal to </em><code>k</code><em>.</em></p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>In the binary representation of a number <strong>set bit</strong> is a bit of value <code>1</code>.</li>
|
||||
<li>The binary representation of a number will be indexed from right to left. For example, if <code>s == 11100</code>, <code>s[4] == 1</code> and <code>s[2] == 0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 9, x = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
|
||||
Since x is equal to 1, the price of each number is the number of its set bits.
|
||||
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
|
||||
So the answer is 6.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 7, x = 2
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Since x is equal to 2, we should just check even<sup>th</sup> bits.
|
||||
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
|
||||
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
|
||||
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
|
||||
Numbers 1, 4, and 5 don't have set bits in their even<sup>th</sup> bits in their binary representation. So the sum of their prices is 0.
|
||||
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
|
||||
The sum of the prices of the first 9 numbers is 6.
|
||||
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>15</sup></code></li>
|
||||
<li><code>1 <= x <= 8</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= s.length - a.length</code></li>
|
||||
<li><code>s[i..(i + a.length - 1)] == a</code></li>
|
||||
<li>There exists an index <code>j</code> such that:
|
||||
<ul>
|
||||
<li><code>0 <= j <= s.length - b.length</code></li>
|
||||
<li><code>s[j..(j + b.length - 1)] == b</code></li>
|
||||
<li><code>|j - i| <= k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
|
||||
<strong>Output:</strong> [16,33]
|
||||
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
|
||||
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
|
||||
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
|
||||
Thus we return [16,33] as the result.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> There is 1 beautiful index: [0].
|
||||
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
|
||||
Thus we return [0] as the result.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= a.length, b.length <= 10</code></li>
|
||||
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= s.length - a.length</code></li>
|
||||
<li><code>s[i..(i + a.length - 1)] == a</code></li>
|
||||
<li>There exists an index <code>j</code> such that:
|
||||
<ul>
|
||||
<li><code>0 <= j <= s.length - b.length</code></li>
|
||||
<li><code>s[j..(j + b.length - 1)] == b</code></li>
|
||||
<li><code>|j - i| <= k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
|
||||
<strong>Output:</strong> [16,33]
|
||||
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
|
||||
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
|
||||
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
|
||||
Thus we return [16,33] as the result.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> There is 1 beautiful index: [0].
|
||||
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
|
||||
Thus we return [0] as the result.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>Return <em>the <strong>total frequencies</strong> of elements in</em><em> </em><code>nums</code> <em>such that those elements all have the <strong>maximum</strong> frequency</em>.</p>
|
||||
|
||||
<p>The <strong>frequency</strong> of an element is the number of occurrences of that element in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,3,1,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
|
||||
So the number of elements in the array with maximum frequency is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> All elements of the array have a frequency of 1 which is the maximum.
|
||||
So the number of elements in the array with maximum frequency is 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
180
leetcode/originData/count-elements-with-maximum-frequency.json
Normal file
180
leetcode/originData/count-elements-with-maximum-frequency.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
32
leetcode/problem/count-elements-with-maximum-frequency.html
Normal file
32
leetcode/problem/count-elements-with-maximum-frequency.html
Normal file
@ -0,0 +1,32 @@
|
||||
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>Return <em>the <strong>total frequencies</strong> of elements in</em><em> </em><code>nums</code> <em>such that those elements all have the <strong>maximum</strong> frequency</em>.</p>
|
||||
|
||||
<p>The <strong>frequency</strong> of an element is the number of occurrences of that element in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,3,1,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
|
||||
So the number of elements in the array with maximum frequency is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> All elements of the array have a frequency of 1 which is the maximum.
|
||||
So the number of elements in the array with maximum frequency is 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= s.length - a.length</code></li>
|
||||
<li><code>s[i..(i + a.length - 1)] == a</code></li>
|
||||
<li>There exists an index <code>j</code> such that:
|
||||
<ul>
|
||||
<li><code>0 <= j <= s.length - b.length</code></li>
|
||||
<li><code>s[j..(j + b.length - 1)] == b</code></li>
|
||||
<li><code>|j - i| <= k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
|
||||
<strong>Output:</strong> [16,33]
|
||||
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
|
||||
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
|
||||
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
|
||||
Thus we return [16,33] as the result.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> There is 1 beautiful index: [0].
|
||||
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
|
||||
Thus we return [0] as the result.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= a.length, b.length <= 10</code></li>
|
||||
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i <= s.length - a.length</code></li>
|
||||
<li><code>s[i..(i + a.length - 1)] == a</code></li>
|
||||
<li>There exists an index <code>j</code> such that:
|
||||
<ul>
|
||||
<li><code>0 <= j <= s.length - b.length</code></li>
|
||||
<li><code>s[j..(j + b.length - 1)] == b</code></li>
|
||||
<li><code>|j - i| <= k</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
|
||||
<strong>Output:</strong> [16,33]
|
||||
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
|
||||
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
|
||||
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
|
||||
Thus we return [16,33] as the result.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> There is 1 beautiful index: [0].
|
||||
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
|
||||
Thus we return [0] as the result.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,45 @@
|
||||
<p>You are given an integer <code>k</code> and an integer <code>x</code>.</p>
|
||||
|
||||
<p>Consider <code>s</code> is the <strong>1-indexed </strong>binary representation of an integer <code>num</code>. The <strong>price</strong> of a number <code>num</code> is the number of <code>i</code>'s such that <code>i % x == 0</code> and <code><font face="monospace">s[i]</font></code> is a <strong>set bit</strong>.</p>
|
||||
|
||||
<p>Return <em>the <b>greatest</b> integer </em><code>num</code><em> such that the sum of <strong>prices</strong> of all numbers from </em><code>1</code><em> to </em><code>num</code><em> is less than or equal to </em><code>k</code><em>.</em></p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>In the binary representation of a number <strong>set bit</strong> is a bit of value <code>1</code>.</li>
|
||||
<li>The binary representation of a number will be indexed from right to left. For example, if <code>s == 11100</code>, <code>s[4] == 1</code> and <code>s[2] == 0</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 9, x = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
|
||||
Since x is equal to 1, the price of each number is the number of its set bits.
|
||||
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
|
||||
So the answer is 6.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 7, x = 2
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Since x is equal to 2, we should just check even<sup>th</sup> bits.
|
||||
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
|
||||
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
|
||||
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
|
||||
Numbers 1, 4, and 5 don't have set bits in their even<sup>th</sup> bits in their binary representation. So the sum of their prices is 0.
|
||||
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
|
||||
The sum of the prices of the first 9 numbers is 6.
|
||||
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>15</sup></code></li>
|
||||
<li><code>1 <= x <= 8</code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user