mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 16:01:41 +08:00
update
This commit is contained in:
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>
|
Reference in New Issue
Block a user