Skip to content

Commit fccb469

Browse files
authored
feat: add solutions to lc problems: No.2542+ (#2153)
1 parent 0fe2033 commit fccb469

File tree

46 files changed

+404
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+404
-21
lines changed

solution/2500-2599/2542.Maximum Subsequence Score/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum po
5353

5454
## Solutions
5555

56+
**Solution 1: Sorting + Priority Queue (Min Heap)**
57+
58+
Sort nums2 and nums1 in descending order according to nums2, then traverse from front to back, maintaining a min heap. The heap stores elements from nums1, and the number of elements in the heap does not exceed $k$. At the same time, maintain a variable $s$ representing the sum of the elements in the heap, and continuously update the answer during the traversal process.
59+
60+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array nums1.
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**

solution/2500-2599/2543.Check if Point Is Reachable/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
只要 $x$ 或 $y$ 是偶数,我们就将其除以 $2$,直到 $x$ 和 $y$ 均为奇数。此时,若 $x \neq y$,不妨设 $x \gt y$,那么 $\frac{x+y}{2} \lt x$。由于 $x+y$ 是偶数,我们可以通过操作从 $(x, y)$ 移动到 $(x+y, y)$,再移动到 $(\frac{x+y}{2}, y)$。也就是说,我们总能让 $x$ 和 $y$ 不断变小。循环结束时,如果 $x=y=1$,说明可以到达。
5959

60-
时间复杂度 $O(\log(min(targetX, targetY)))$,空间复杂度 $O(1)$。
60+
时间复杂度 $O(\log(\min(targetX, targetY)))$,空间复杂度 $O(1)$。
6161

6262
<!-- tabs:start -->
6363

solution/2500-2599/2543.Check if Point Is Reachable/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Mathematics**
47+
48+
We notice that the first two types of moves do not change the greatest common divisor (gcd) of the horizontal and vertical coordinates, while the last two types of moves can multiply the gcd of the horizontal and vertical coordinates by a power of $2$. In other words, the final gcd of the horizontal and vertical coordinates must be a power of $2$. If the gcd is not a power of $2$, then it is impossible to reach.
49+
50+
Next, we prove that any $(x, y)$ that satisfies $gcd(x, y)=2^k$ can be reached.
51+
52+
We reverse the direction of movement, that is, move from the end point back. Then $(x, y)$ can move to $(x, x+y)$, $(x+y, y)$, $(\frac{x}{2}, y)$, and $(x, \frac{y}{2})$.
53+
54+
As long as $x$ or $y$ is even, we divide it by $2$ until both $x$ and $y$ are odd. At this point, if $x \neq y$, without loss of generality, let $x \gt y$, then $\frac{x+y}{2} \lt x$. Since $x+y$ is even, we can move from $(x, y)$ to $(x+y, y)$, and then to $(\frac{x+y}{2}, y)$ through operations. That is to say, we can always make $x$ and $y$ continuously decrease. When the loop ends, if $x=y=1$, it means it can be reached.
55+
56+
The time complexity is $O(\log(\min(targetX, targetY)))$, and the space complexity is $O(1)$.
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**

solution/2500-2599/2544.Alternating Digit Sum/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656

5757
## Solutions
5858

59+
**Solution 1: Simulation**
60+
61+
We can directly simulate the process as described in the problem.
62+
63+
We define an initial symbol $sign=1$. Starting from the most significant digit, we take out one digit $x$ each time, multiply it by $sign$, add the result to the answer, then negate $sign$, and continue to process the next digit until all digits are processed.
64+
65+
The time complexity is $O(\log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the given number.
66+
5967
<!-- tabs:start -->
6068

6169
### **Python3**

solution/2500-2599/2545.Sort the Students by Their Kth Score/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
**方法一:排序**
6060

61-
`score` 按照第 $k$ 列的分数从大到小排序,然后返回即可。
61+
我们将 `score` 按照第 $k$ 列的分数从大到小排序,然后返回即可。
6262

6363
时间复杂度 $O(m \times \log m)$,空间复杂度 $O(1)$。其中 $m$ 为 `score` 的行数。
6464

solution/2500-2599/2545.Sort the Students by Their Kth Score/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Sorting**
50+
51+
We sort score in descending order based on the scores in the $k^{th}$ column, and then return it.
52+
53+
The time complexity is $O(m \times \log m)$, and the space complexity is $O(1)$. Here, $m$ is the number of rows in score.
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**

solution/2500-2599/2546.Apply Bitwise Operations to Make Strings Equal/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252

5353
**方法一:脑筋急转弯**
5454

55-
注意到 $1$ 其实是数字转换的“工具”,因此只要两个字符串中都有 $1$ 或者都没有 $1$,那么就可以通过操作使得两个字符串相等。
55+
我们注意到 $1$ 其实是数字转换的“工具”,因此只要两个字符串中都有 $1$ 或者都没有 $1$,那么就可以通过操作使得两个字符串相等。
5656

57-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串的长度
57+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。
5858

5959
<!-- tabs:start -->
6060

solution/2500-2599/2546.Apply Bitwise Operations to Make Strings Equal/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ Since we can make s equal to target, we return true.
4646

4747
## Solutions
4848

49+
**Solution 1: Lateral Thinking**
50+
51+
We notice that $1$ is actually a "tool" for number conversion. Therefore, as long as both strings either have $1$ or neither have $1$, we can make the two strings equal through operations.
52+
53+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**

solution/2500-2599/2547.Minimum Cost to Split an Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787

8888
过程中,我们可以使用记忆化搜索,即使用一个数组 $f$ 记忆化函数 $dfs(i)$ 的返回值,避免重复计算。
8989

90-
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
90+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
9191

9292
<!-- tabs:start -->
9393

solution/2500-2599/2547.Minimum Cost to Split an Array/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ The cost of the split is 10. It can be shown that this is the minimum possible c
7777

7878
## Solutions
7979

80+
**Solution 1: Memoization Search**
81+
82+
We design a function $dfs(i)$, which represents the minimum cost of splitting from index $i$. So the answer is $dfs(0)$.
83+
84+
The calculation process of the function $dfs(i)$ is as follows:
85+
86+
If $i \ge n$, it means that the splitting has reached the end of the array, and $0$ is returned at this time.
87+
Otherwise, we enumerate the end $j$ of the subarray. During the process, we use an array or hash table cnt to count the number of times each number appears in the subarray, and use a variable one to count the number of numbers in the subarray that appear once. So the importance of the subarray is $k + j - i + 1 - one$, and the cost of splitting is $k + j - i + 1 - one + dfs(j + 1)$. We enumerate all $j$ and take the minimum value as the return value of $dfs(i)$.
88+
During the process, we can use memoization search, that is, use an array $f$ to memorize the return value of the function $dfs(i)$ to avoid repeated calculations.
89+
90+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
91+
8092
<!-- tabs:start -->
8193

8294
### **Python3**

solution/2500-2599/2548.Maximum Price to Fill a Bag/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
**方法一:贪心 + 排序**
6161

62-
将物品按照单位价格从大到小排序,然后依次取出物品,直到背包装满。
62+
我们将物品按照单位价格从大到小排序,然后依次取出物品,直到背包装满。
6363

6464
若最后背包未装满,则返回 $-1$,否则返回总价格。
6565

solution/2500-2599/2548.Maximum Price to Fill a Bag/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ It can be proved that 55.0 is the maximum total price that we can achieve.
5151

5252
## Solutions
5353

54+
**Solution 1: Greedy + Sorting**
55+
56+
We sort the items in descending order by unit price, and then take out the items one by one until the backpack is full.
57+
58+
If the backpack is not full in the end, return $-1$, otherwise return the total price.
59+
60+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of items.
61+
5462
<!-- tabs:start -->
5563

5664
### **Python3**

solution/2500-2599/2549.Count Distinct Numbers on Board/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ After a billion days, the only two distinct numbers on the board are 2 and 3.
5151

5252
## Solutions
5353

54+
**Solution 1: Lateral Thinking**
55+
56+
Since every operation on the number $n$ on the desktop will also cause the number $n-1$ to appear on the desktop, the final numbers on the desktop are $[2,...n]$, that is, $n-1$ numbers.
57+
58+
Note that $n$ could be $1$, so it needs to be specially judged.
59+
60+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
61+
5462
<!-- tabs:start -->
5563

5664
### **Python3**

solution/2500-2599/2550.Count Collisions of Monkeys on a Polygon/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858

5959
**方法一:数学(快速幂)**
6060

61-
每一只猴子都有两种移动方式,即顺时针或逆时针。因此,一共有 $2^n$ 种移动方式。不碰撞的移动方式只有两种,即所有猴子都顺时针移动或所有猴子都逆时针移动。因此,碰撞的移动方式有 $2^n - 2$ 种。
61+
根据题目描述,每一只猴子都有两种移动方式,即顺时针或逆时针。因此,一共有 $2^n$ 种移动方式。不碰撞的移动方式只有两种,即所有猴子都顺时针移动或所有猴子都逆时针移动。因此,碰撞的移动方式有 $2^n - 2$ 种。
6262

6363
我们可以用快速幂求出 $2^n$ 的值,然后用 $2^n - 2$ 求出碰撞的移动方式数,最后对 $10^9 + 7$ 取余即可。
6464

65-
时间复杂度为 $O(\log n)$,空间复杂度为 $O(1)$。其中 $n$ 为猴子的数量。
65+
时间复杂度 $O(\log n)$,其中 $n$ 为猴子的数量。空间复杂度 $O(1)$
6666

6767
<!-- tabs:start -->
6868

solution/2500-2599/2550.Count Collisions of Monkeys on a Polygon/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ It can be shown 6 total movements result in a collision.
4949

5050
## Solutions
5151

52+
**Solution 1: Mathematics (Fast Power)**
53+
54+
According to the problem description, each monkey has two ways of moving, either clockwise or counterclockwise. Therefore, there are a total of $2^n$ ways to move. The non-collision ways of moving are only two, that is, all monkeys move clockwise or all monkeys move counterclockwise. Therefore, the number of collision ways of moving is $2^n - 2$.
55+
56+
We can use fast power to calculate the value of $2^n$, then use $2^n - 2$ to calculate the number of collision ways of moving, and finally take the remainder of $10^9 + 7$.
57+
58+
The time complexity is $O(\log n)$, where $n$ is the number of monkeys. The space complexity is $O(1)$.
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**

solution/2500-2599/2551.Put Marbles in Bags/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ Since both the maximal and minimal score are the same, we return 0.
4949

5050
## Solutions
5151

52+
**Solution 1: Problem Transformation + Sorting**
53+
54+
We can transform the problem into: dividing the array `weights` into $k$ consecutive subarrays, that is, we need to find $k-1$ splitting points, each splitting point's cost is the sum of the elements on the left and right of the splitting point. The difference between the sum of the costs of the largest $k-1$ splitting points and the smallest $k-1$ splitting points is the answer.
55+
56+
Therefore, we can process the array `weights` and transform it into an array `arr` of length $n-1$, where `arr[i] = weights[i] + weights[i+1]`. Then we sort the array `arr`, and finally calculate the difference between the sum of the costs of the largest $k-1$ splitting points and the smallest $k-1$ splitting points.
57+
58+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array `weights`.
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**

solution/2500-2599/2552.Count Increasing Quadruplets/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ There are no other quadruplets, so we return 2.
4444

4545
## Solutions
4646

47+
**Solution 1: Enumeration + Preprocessing**
48+
49+
We can enumerate $j$ and $k$ in the quadruplet, then the problem is transformed into, for the current $j$ and $k$:
50+
51+
- Count how many $l$ satisfy $l > k$ and $nums[l] > nums[j]$;
52+
- Count how many $i$ satisfy $i < j$ and $nums[i] < nums[k]$.
53+
54+
We can use two two-dimensional arrays $f$ and $g$ to record these two pieces of information. Where $f[j][k]$ represents how many $l$ satisfy $l > k$ and $nums[l] > nums[j]$, and $g[j][k]$ represents how many $i$ satisfy $i < j$ and $nums[i] < nums[k]$.
55+
56+
Therefore, the answer is the sum of all $f[j][k] \times g[j][k]$.
57+
58+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the array.
59+
4760
<!-- tabs:start -->
4861

4962
### **Python3**

solution/2500-2599/2553.Separate the Digits in an Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ answer = [7,1,3,9] 。
5353

5454
将数组中的每个数字进行数位分割,然后将分割后的数字依次放入答案数组中。
5555

56-
时间复杂度 $O(n \times \log_{10} M)$,空间复杂度 $O(n \times \log_{10} M)$,其中 $n$ 为数组 `nums` 的长度,而 $M$ 为数组 `nums` 中的最大值。
56+
时间复杂度 $O(n \times \log_{10} M)$,空间复杂度 $O(n \times \log_{10} M)$,其中 $n$ 为数组 $nums$ 的长度,而 $M$ 为数组 $nums$ 中的最大值。
5757

5858
<!-- tabs:start -->
5959

solution/2500-2599/2553.Separate the Digits in an Array/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ answer = [7,1,3,9].
4545

4646
## Solutions
4747

48+
**Solution 1: Simulation**
49+
50+
Split each number in the array into digits, then put the split numbers into the answer array in order.
51+
52+
The time complexity is $O(n \times \log_{10} M)$, and the space complexity is $O(n \times \log_{10} M)$. Where $n$ is the length of the array $nums$, and $M$ is the maximum value in the array $nums$.
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**

solution/2500-2599/2554.Maximum Number of Integers to Choose From a Range I/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@
7676

7777
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `banned` 的长度。
7878

79-
相似题目:[2557. 从一个范围内选择最多整数 II](/solution/2500-2599/2557.Maximum%20Number%20of%20Integers%20to%20Choose%20From%20a%20Range%20II/README.md)
79+
相似题目:
80+
81+
- [2557. 从一个范围内选择最多整数 II](/solution/2500-2599/2557.Maximum%20Number%20of%20Integers%20to%20Choose%20From%20a%20Range%20II/README.md)
8082

8183
<!-- tabs:start -->
8284

solution/2500-2599/2554.Maximum Number of Integers to Choose From a Range I/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ They are from the range [1, 7], all did not appear in banned, and their sum is 2
5353

5454
## Solutions
5555

56+
**Solution 1: Greedy + Enumeration**
57+
58+
We use the variable $s$ to represent the sum of the currently selected integers, and the variable $ans$ to represent the number of currently selected integers. We convert the array `banned` into a hash table for easy determination of whether a certain integer is not selectable.
59+
60+
Next, we start enumerating the integer $i$ from $1$. If $s + i \leq maxSum$ and $i$ is not in `banned`, then we can select the integer $i$, and add $i$ and $1$ to $s$ and $ans$ respectively.
61+
62+
Finally, we return $ans$.
63+
64+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the given integer.
65+
66+
**Solution 2: Greedy + Binary Search**
67+
68+
If $n$ is very large, the enumeration in Method One will time out.
69+
70+
We can add $0$ and $n + 1$ to the array `banned`, deduplicate the array `banned`, remove elements greater than $n+1$, and then sort it.
71+
72+
Next, we enumerate every two adjacent elements $i$ and $j$ in the array `banned`. The range of selectable integers is $[i + 1, j - 1]$. We use binary search to enumerate the number of elements we can select in this range, find the maximum number of selectable elements, and then add it to $ans$. At the same time, we subtract the sum of these elements from `maxSum`. If `maxSum` is less than $0$, we break the loop. Return the answer.
73+
74+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array `banned`.
75+
76+
Similar problems:
77+
78+
- [2557. Maximum Number of Integers to Choose From a Range II](/solution/2500-2599/2557.Maximum%20Number%20of%20Integers%20to%20Choose%20From%20a%20Range%20II/README.md)
79+
5680
<!-- tabs:start -->
5781

5882
### **Python3**

solution/2500-2599/2555.Maximize Win From Two Segments/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252

5353
## Solutions
5454

55+
**Solution 1: Dynamic Programming + Binary Search**
56+
57+
We define $f[i]$ as the maximum number of prizes that can be obtained by selecting a segment of length $k$ from the first $i$ prizes. Initially, $f[0] = 0$. We define the answer variable as $ans = 0$.
58+
59+
Next, we enumerate the position $x$ of each prize, and use binary search to find the leftmost prize index $j$ such that $prizePositions[j] \geq x - k$. At this point, we update the answer $ans = \max(ans, f[j] + i - j)$, and update $f[i] = \max(f[i - 1], i - j)$.
60+
61+
Finally, we return $ans$.
62+
63+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of prizes.
64+
5565
<!-- tabs:start -->
5666

5767
### **Python3**

solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Two DFS Traversals**
47+
48+
First, we perform a DFS traversal to determine whether there is a path from $(0, 0)$ to $(m - 1, n - 1)$, and we denote the result as $a$. During the DFS process, we set the value of the visited cells to $0$ to prevent revisiting.
49+
50+
Next, we set the values of $(0, 0)$ and $(m - 1, n - 1)$ to $1$, and perform another DFS traversal to determine whether there is a path from $(0, 0)$ to $(m - 1, n - 1)$, and we denote the result as $b$. During the DFS process, we set the value of the visited cells to $0$ to avoid revisiting.
51+
52+
Finally, if both $a$ and $b$ are `true`, we return `false`, otherwise, we return `true`.
53+
54+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix, respectively.
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**

solution/2500-2599/2557.Maximum Number of Integers to Choose From a Range II/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ All these integers are in the range [1, 7], all do not appear in banned, and the
4545

4646
## Solutions
4747

48+
**Solution 1: Deduplication + Sorting + Binary Search**
49+
50+
We can add $0$ and $n + 1$ to the array `banned`, then deduplicate and sort the array `banned`.
51+
52+
Next, we enumerate every two adjacent elements $i$ and $j$ in the array `banned`. The range of selectable integers is $[i + 1, j - 1]$. We use binary search to enumerate the number of elements we can select in this range, find the maximum number of selectable elements, and then add it to $ans$. At the same time, we subtract the sum of these elements from `maxSum`. If `maxSum` is less than $0$, we break the loop. Return the answer.
53+
54+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array `banned`.
55+
4856
<!-- tabs:start -->
4957

5058
### **Python3**

solution/2500-2599/2559.Count Vowel Strings in Ranges/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ We return [2,3,0].
4646

4747
## Solutions
4848

49+
**Solution 1: Preprocessing + Binary Search**
50+
51+
We can preprocess all the indices of the strings that start and end with a vowel, and record them in order in the array $nums$.
52+
53+
Next, we iterate through each query $(l, r)$, and use binary search to find the first index $i$ in $nums$ that is greater than or equal to $l$, and the first index $j$ that is greater than $r$. Therefore, the answer to the current query is $j - i$.
54+
55+
The time complexity is $O(n + m \times \log n)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of the arrays $words$ and $queries$, respectively.
56+
57+
**Solution 2: Prefix Sum**
58+
59+
We can create a prefix sum array $s$ of length $n+1$, where $s[i]$ represents the number of strings that start and end with a vowel in the first $i$ strings of the array $words$. Initially, $s[0] = 0$.
60+
61+
Next, we iterate through the array $words$. If the current string starts and ends with a vowel, then $s[i+1] = s[i] + 1$, otherwise $s[i+1] = s[i]$.
62+
63+
Finally, we iterate through each query $(l, r)$. Therefore, the answer to the current query is $s[r+1] - s[l]$.
64+
65+
The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of the arrays $words$ and $queries$, respectively.
66+
4967
<!-- tabs:start -->
5068

5169
### **Python3**

solution/2500-2599/2560.House Robber IV/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
题目求的是窃贼的最小窃取能力,我们可以二分枚举窃贼的窃取能力,对于枚举的能力 $x$,我们可以通过贪心的方式判断窃贼是否能够窃取至少 $k$ 间房屋,具体地,我们从左到右遍历数组,对于当前遍历到的房屋 $i$,如果 $nums[i] \leq x$ 且 $i$ 与上一个窃取的房屋的下标之差大于 $1$,则窃贼可以窃取房屋 $i$,否则窃贼不能窃取房屋 $i$。累计窃取的房屋数,如果窃取的房屋数大于等于 $k$,则说明窃贼可以窃取至少 $k$ 间房屋,此时窃贼的窃取能力 $x$ 可能是最小的,否则窃贼的窃取能力 $x$ 不是最小的。
6161

62-
时间复杂度 $O(n \times \log m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。
62+
时间复杂度 $O(n \times \log m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别是数组 $nums$ 的长度和数组 $nums$ 中的最大值。
6363

6464
<!-- tabs:start -->
6565

0 commit comments

Comments
 (0)