mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<p>You are given two positive integers <code>x</code> and <code>y</code>.</p>
|
||||
|
||||
<p>In one operation, you can do one of the four following operations:</p>
|
||||
|
||||
<ol>
|
||||
<li>Divide <code>x</code> by <code>11</code> if <code>x</code> is a multiple of <code>11</code>.</li>
|
||||
<li>Divide <code>x</code> by <code>5</code> if <code>x</code> is a multiple of <code>5</code>.</li>
|
||||
<li>Decrement <code>x</code> by <code>1</code>.</li>
|
||||
<li>Increment <code>x</code> by <code>1</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make </em> <code>x</code> <i>and</i> <code>y</code> equal.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 26, y = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can make 26 equal to 1 by applying the following operations:
|
||||
1. Decrement x by 1
|
||||
2. Divide x by 5
|
||||
3. Divide x by 5
|
||||
It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 54, y = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can make 54 equal to 2 by applying the following operations:
|
||||
1. Increment x by 1
|
||||
2. Divide x by 11
|
||||
3. Divide x by 5
|
||||
4. Increment x by 1
|
||||
It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 25, y = 30
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We can make 25 equal to 30 by applying the following operations:
|
||||
1. Increment x by 1
|
||||
2. Increment x by 1
|
||||
3. Increment x by 1
|
||||
4. Increment x by 1
|
||||
5. Increment x by 1
|
||||
It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>You can apply the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,4], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4].
|
||||
- Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4].
|
||||
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
|
||||
It can be shown that we cannot make the XOR equal to k in less than 2 operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,0,2,0], k = 0
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,78 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code> having an <strong>even</strong> length <code>n</code>.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> 2D integer array, <code>queries</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, d<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>For each query <code>i</code>, you are allowed to perform the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Rearrange the characters within the <strong>substring</strong> <code>s[a<sub>i</sub>:b<sub>i</sub>]</code>, where <code>0 <= a<sub>i</sub> <= b<sub>i</sub> < n / 2</code>.</li>
|
||||
<li>Rearrange the characters within the <strong>substring</strong> <code>s[c<sub>i</sub>:d<sub>i</sub>]</code>, where <code>n / 2 <= c<sub>i</sub> <= d<sub>i</sub> < n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For each query, your task is to determine whether it is possible to make <code>s</code> a <strong>palindrome</strong> by performing the operations.</p>
|
||||
|
||||
<p>Each query is answered <strong>independently</strong> of the others.</p>
|
||||
|
||||
<p>Return <em>a <strong>0-indexed</strong> array </em><code>answer</code><em>, where </em><code>answer[i] == true</code><em> if it is possible to make </em><code>s</code><em> a palindrome by performing operations specified by the </em><code>i<sup>th</sup></code><em> query, and </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<ul>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
<li><code>s[x:y]</code> represents the substring consisting of characters from the index <code>x</code> to index <code>y</code> in <code>s</code>, <strong>both inclusive</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
|
||||
<strong>Output:</strong> [true,true]
|
||||
<strong>Explanation:</strong> In this example, there are two queries:
|
||||
In the first query:
|
||||
- a<sub>0</sub> = 1, b<sub>0</sub> = 1, c<sub>0</sub> = 3, d<sub>0</sub> = 5.
|
||||
- So, you are allowed to rearrange s[1:1] => a<u>b</u>cabc and s[3:5] => abc<u>abc</u>.
|
||||
- To make s a palindrome, s[3:5] can be rearranged to become => abc<u>cba</u>.
|
||||
- Now, s is a palindrome. So, answer[0] = true.
|
||||
In the second query:
|
||||
- a<sub>1</sub> = 0, b<sub>1</sub> = 2, c<sub>1</sub> = 5, d<sub>1</sub> = 5.
|
||||
- So, you are allowed to rearrange s[0:2] => <u>abc</u>abc and s[5:5] => abcab<u>c</u>.
|
||||
- To make s a palindrome, s[0:2] can be rearranged to become => <u>cba</u>abc.
|
||||
- Now, s is a palindrome. So, answer[1] = true.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abbcdecbba", queries = [[0,2,7,9]]
|
||||
<strong>Output:</strong> [false]
|
||||
<strong>Explanation:</strong> In this example, there is only one query.
|
||||
a<sub>0</sub> = 0, b<sub>0</sub> = 2, c<sub>0</sub> = 7, d<sub>0</sub> = 9.
|
||||
So, you are allowed to rearrange s[0:2] => <u>abb</u>cdecbba and s[7:9] => abbcdec<u>bba</u>.
|
||||
It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome.
|
||||
So, answer[0] = false.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "acbcab", queries = [[1,2,4,5]]
|
||||
<strong>Output:</strong> [true]
|
||||
<strong>Explanation: </strong>In this example, there is only one query.
|
||||
a<sub>0</sub> = 1, b<sub>0</sub> = 2, c<sub>0</sub> = 4, d<sub>0</sub> = 5.
|
||||
So, you are allowed to rearrange s[1:2] => a<u>cb</u>cab and s[4:5] => acbc<u>ab</u>.
|
||||
To make s a palindrome s[1:2] can be rearranged to become a<u>bc</u>cab.
|
||||
Then, s[4:5] can be rearranged to become abcc<u>ba</u>.
|
||||
Now, s is a palindrome. So, answer[0] = true.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 4</code></li>
|
||||
<li><code>a<sub>i</sub> == queries[i][0], b<sub>i</sub> == queries[i][1]</code></li>
|
||||
<li><code>c<sub>i</sub> == queries[i][2], d<sub>i</sub> == queries[i][3]</code></li>
|
||||
<li><code>0 <= a<sub>i</sub> <= b<sub>i</sub> < n / 2</code></li>
|
||||
<li><code>n / 2 <= c<sub>i</sub> <= d<sub>i</sub> < n </code></li>
|
||||
<li><code>n</code> is even.</li>
|
||||
<li><code>s</code> consists of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>.</p>
|
||||
|
||||
<p>A prefix <code>nums[0..i]</code> is <strong>sequential</strong> if, for all <code>1 <= j <= i</code>, <code>nums[j] = nums[j - 1] + 1</code>. In particular, the prefix consisting only of <code>nums[0]</code> is <strong>sequential</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>smallest</strong> integer</em> <code>x</code> <em>missing from</em> <code>nums</code> <em>such that</em> <code>x</code> <em>is greater than or equal to the sum of the <strong>longest</strong> sequential prefix.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,2,5]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,5,1,12,14,13]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given a 2D <strong>0-indexed </strong>integer array <code>dimensions</code>.</p>
|
||||
|
||||
<p>For all indices <code>i</code>, <code>0 <= i < dimensions.length</code>, <code>dimensions[i][0]</code> represents the length and <code>dimensions[i][1]</code> represents the width of the rectangle<span style="font-size: 13.3333px;"> <code>i</code></span>.</p>
|
||||
|
||||
<p>Return <em>the <strong>area</strong> of the rectangle having the <strong>longest</strong> diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the <strong>maximum</strong> area.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dimensions = [[9,3],[8,6]]
|
||||
<strong>Output:</strong> 48
|
||||
<strong>Explanation:</strong>
|
||||
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈<!-- notionvc: 882cf44c-3b17-428e-9c65-9940810216f1 --> 9.487.
|
||||
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
|
||||
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> dimensions = [[3,4],[4,3]]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> Length of diagonal is the same for both which is 5, so maximum area = 12.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= dimensions.length <= 100</code></li>
|
||||
<li><code><font face="monospace">dimensions[i].length == 2</font></code></li>
|
||||
<li><code><font face="monospace">1 <= dimensions[i][0], dimensions[i][1] <= 100</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,71 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are to perform the following partitioning operations until <code>s</code> is <strong>empty</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the <strong>longest</strong> <strong>prefix</strong> of <code>s</code> containing at most <code>k</code> <strong>distinct</strong> characters.</li>
|
||||
<li><strong>Delete</strong> the prefix from <code>s</code> and increase the number of partitions by one. The remaining characters (if any) in <code>s</code> maintain their initial order.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Before</strong> the operations, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> number of resulting partitions after the operations by optimally choosing at most one index to change.</em></p>
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "accca", k = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'.
|
||||
s becomes "acbca".
|
||||
The operations can now be performed as follows until s becomes empty:
|
||||
- Choose the longest prefix containing at most 2 distinct characters, "<u>ac</u>bca".
|
||||
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
|
||||
- Choose the longest prefix containing at most 2 distinct characters, "<u>bc</u>a".
|
||||
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
|
||||
- Choose the longest prefix containing at most 2 distinct characters, "<u>a</u>".
|
||||
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
|
||||
Hence, the answer is 3.
|
||||
It can be shown that it is not possible to obtain more than 3 partitions.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabaab", k = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation: </strong>In this example, to maximize the number of resulting partitions we can leave s as it is.
|
||||
The operations can now be performed as follows until s becomes empty:
|
||||
- Choose the longest prefix containing at most 3 distinct characters, "<u>aabaab</u>".
|
||||
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
|
||||
Hence, the answer is 1.
|
||||
It can be shown that it is not possible to obtain more than 1 partition.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "xxyz", k = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'.
|
||||
s becomes "xayz".
|
||||
The operations can now be performed as follows until s becomes empty:
|
||||
- Choose the longest prefix containing at most 1 distinct character, "<u>x</u>ayz".
|
||||
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
|
||||
- Choose the longest prefix containing at most 1 distinct character, "<u>a</u>yz".
|
||||
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
|
||||
- Choose the longest prefix containing at most 1 distinct character, "<u>y</u>z".
|
||||
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
|
||||
- Choose the longest prefix containing at most 1 distinct character, "<u>z</u>".
|
||||
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
|
||||
Hence, the answer is 4.
|
||||
It can be shown that it is not possible to obtain more than 4 partitions.
|
||||
</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 only of lowercase English letters.</li>
|
||||
<li><code>1 <= k <= 26</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a string <code>s</code> that consists of lowercase English letters.</p>
|
||||
|
||||
<p>A string is called <strong>special</strong> if it is made up of only a single character. For example, the string <code>"abc"</code> is not special, whereas the strings <code>"ddd"</code>, <code>"zz"</code>, and <code>"f"</code> are special.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest special substring</strong> of </em><code>s</code> <em>which occurs <strong>at least thrice</strong></em>, <em>or </em><code>-1</code><em> if no special substring occurs at least thrice</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The longest special substring which occurs thrice is "aa": substrings "<u><strong>aa</strong></u>aa", "a<u><strong>aa</strong></u>a", and "aa<u><strong>aa</strong></u>".
|
||||
It can be shown that the maximum length achievable is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdef"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There exists no special substring which occurs at least thrice. Hence return -1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcaba"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The longest special substring which occurs thrice is "a": substrings "<u><strong>a</strong></u>bcaba", "abc<u><strong>a</strong></u>ba", and "abcab<u><strong>a</strong></u>".
|
||||
It can be shown that the maximum length achievable is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 50</code></li>
|
||||
<li><code>s</code> consists of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a string <code>s</code> that consists of lowercase English letters.</p>
|
||||
|
||||
<p>A string is called <strong>special</strong> if it is made up of only a single character. For example, the string <code>"abc"</code> is not special, whereas the strings <code>"ddd"</code>, <code>"zz"</code>, and <code>"f"</code> are special.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest special substring</strong> of </em><code>s</code> <em>which occurs <strong>at least thrice</strong></em>, <em>or </em><code>-1</code><em> if no special substring occurs at least thrice</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The longest special substring which occurs thrice is "aa": substrings "<u><strong>aa</strong></u>aa", "a<u><strong>aa</strong></u>a", and "aa<u><strong>aa</strong></u>".
|
||||
It can be shown that the maximum length achievable is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdef"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There exists no special substring which occurs at least thrice. Hence return -1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcaba"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The longest special substring which occurs thrice is "a": substrings "<u><strong>a</strong></u>bcaba", "abc<u><strong>a</strong></u>ba", and "abcab<u><strong>a</strong></u>".
|
||||
It can be shown that the maximum length achievable is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>There is a <strong>1-indexed</strong> <code>8 x 8</code> chessboard containing <code>3</code> pieces.</p>
|
||||
|
||||
<p>You are given <code>6</code> integers <code>a</code>, <code>b</code>, <code>c</code>, <code>d</code>, <code>e</code>, and <code>f</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>(a, b)</code> denotes the position of the white rook.</li>
|
||||
<li><code>(c, d)</code> denotes the position of the white bishop.</li>
|
||||
<li><code>(e, f)</code> denotes the position of the black queen.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given that you can only move the white pieces, return <em>the <strong>minimum</strong> number of moves required to capture the black queen</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.</li>
|
||||
<li>Bishops can move any number of squares diagonally, but cannot jump over other pieces.</li>
|
||||
<li>A rook or a bishop can capture the queen if it is located in a square that they can move to.</li>
|
||||
<li>The queen does not move.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/ex1.png" style="width: 600px; height: 600px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
|
||||
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/ex2.png" style="width: 600px; height: 600px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can capture the black queen in a single move by doing one of the following:
|
||||
- Move the white rook to (5, 2).
|
||||
- Move the white bishop to (5, 2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b, c, d, e, f <= 8</code></li>
|
||||
<li>No two pieces are on the same square.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
|
||||
|
||||
<p>You have to check if it is possible to select <strong>two or more</strong> elements in the array such that the bitwise <code>OR</code> of the selected elements has <strong>at least </strong>one trailing zero in its binary representation.</p>
|
||||
|
||||
<p>For example, the binary representation of <code>5</code>, which is <code>"101"</code>, does not have any trailing zeros, whereas the binary representation of <code>4</code>, which is <code>"100"</code>, has two trailing zeros.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to select two or more elements whose bitwise</em> <code>OR</code> <em>has trailing zeros, return</em> <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,8,16]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
|
||||
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,7,9]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p>
|
||||
|
||||
<p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elements of <code>nums1</code> and <code>nums2</code> into a set <code>s</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible size of the set</em> <code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,1,2], nums2 = [1,1,1,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
|
||||
It can be shown that 2 is the maximum possible size of the set s after the removals.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
|
||||
It can be shown that 5 is the maximum possible size of the set s after the removals.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
|
||||
It can be shown that 6 is the maximum possible size of the set s after the removals.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n</code> is even.</li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given three integers <code>start</code>, <code>finish</code>, and <code>limit</code>. You are also given a <strong>0-indexed</strong> string <code>s</code> representing a <strong>positive</strong> integer.</p>
|
||||
|
||||
<p>A <strong>positive</strong> integer <code>x</code> is called <strong>powerful</strong> if it ends with <code>s</code> (in other words, <code>s</code> is a <strong>suffix</strong> of <code>x</code>) and each digit in <code>x</code> is at most <code>limit</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total</strong> number of powerful integers in the range</em> <code>[start..finish]</code>.</p>
|
||||
|
||||
<p>A string <code>x</code> is a suffix of a string <code>y</code> if and only if <code>x</code> is a substring of <code>y</code> that starts from some index (<strong>including </strong><code>0</code>) in <code>y</code> and extends to the index <code>y.length - 1</code>. For example, <code>25</code> is a suffix of <code>5125</code> whereas <code>512</code> is not.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 1, finish = 6000, limit = 4, s = "124"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
|
||||
It can be shown that there are only 5 powerful integers in this range.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 15, finish = 215, limit = 6, s = "10"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
|
||||
It can be shown that there are only 2 powerful integers in this range.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 1000, finish = 2000, limit = 4, s = "3000"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= start <= finish <= 10<sup>15</sup></code></li>
|
||||
<li><code>1 <= limit <= 9</code></li>
|
||||
<li><code>1 <= s.length <= floor(log<sub>10</sub>(finish)) + 1</code></li>
|
||||
<li><code>s</code> only consists of numeric digits which are at most <code>limit</code>.</li>
|
||||
<li><code>s</code> does not have leading zeros.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user