Skip to content

Commit fccb469

Browse files
authored
feat: add solutions to lc problems: No.2542+ (doocs#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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 0 deletions
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**

0 commit comments

Comments
 (0)