Skip to content

Commit ce3e3b0

Browse files
committed
feat: add solutions to lc problems: No.2540~2547
* No.2540.Minimum Common Value * No.2541.Minimum Operations to Make Array Equal II * No.2542.Maximum Subsequence Score * No.2543.Check if Point Is Reachable * No.2544.Alternating Digit Sum * No.2545.Sort the Students by Their Kth Score * No.2546.Apply Bitwise Operations to Make Strings Equal * No.2547.Minimum Cost to Split an Array
1 parent fa90acc commit ce3e3b0

File tree

8 files changed

+66
-1
lines changed
  • solution/2500-2599

8 files changed

+66
-1
lines changed

solution/2500-2599/2540.Minimum Common Value/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:双指针**
44+
45+
遍历两个数组,如果两个指针指向的元素相等,则返回该元素;如果两个指针指向的元素不相等,则将指向较小元素的指针右移一位,直到找到相等的元素或者遍历完数组。
46+
47+
时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是两个数组的长度。
48+
4349
<!-- tabs:start -->
4450

4551
### **Python3**

solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:一次遍历**
53+
54+
我们用变量 $x$ 记录加减次数的差值,用变量 $ans$ 记录操作次数。
55+
56+
遍历数组,对于每个位置 $i$,如果存在 $k=0$ 并且 $a_i \neq b_i$,则无法使两个数组相等,返回 $-1$。否则,如果 $k \neq 0$,则 $a_i - b_i$ 必须是 $k$ 的倍数,否则无法使两个数组相等,返回 $-1$。接下来,我们更新 $x$ 和 $ans$。
57+
58+
最后,如果 $x \neq 0$,则无法使两个数组相等,返回 $-1$。否则,返回 $\frac{ans}{2}$。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
61+
5262
<!-- tabs:start -->
5363

5464
### **Python3**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:排序 + 优先队列(小根堆)**
61+
62+
`nums2``nums1` 按照 `nums2` 降序排序,然后从前往后遍历,维护一个小根堆,堆中存储 `nums1` 中的元素,堆中元素个数不超过 $k$ 个,同时维护一个变量 $s$,表示堆中元素的和,遍历过程中不断更新答案。
63+
64+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums1` 的长度。
65+
6066
<!-- tabs:start -->
6167

6268
### **Python3**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:数学**
51+
52+
我们注意到,前两种移动方式不会改变横、纵坐标的最大公约数,而后两种移动方式可以使得横、纵坐标的最大公约数乘上 $2$ 的幂次。也就是说,最后的横、纵坐标的最大公约数必须是 $2$ 的幂次。最大公约数不是 $2$ 的幂次,那么就无法到达。
53+
54+
接下来,我们证明,任意满足 $gcd(x, y)=2^k$ 的 $(x, y)$ 均可达。
55+
56+
我们将移动方式反转一下,即从终点往回走,那么 $(x, y)$ 可以移动到 $(x, x+y)$, $(x+y, y)$, $(\frac{x}{2}, y)$ 和 $(x, \frac{y}{2})$。
57+
58+
只要 $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$,说明可以到达。
59+
60+
时间复杂度 $O(\log(min(targetX, targetY)))$,空间复杂度 $O(1)$。
61+
5062
<!-- tabs:start -->
5163

5264
### **Python3**
@@ -70,7 +82,7 @@ class Solution {
7082
int x = gcd(targetX, targetY);
7183
return (x & (x - 1)) == 0;
7284
}
73-
85+
7486
private int gcd(int a, int b) {
7587
return b == 0 ? a : gcd(b, a % b);
7688
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:模拟**
58+
59+
从最高有效位开始,每次取出一位数字,根据其相邻数字的符号,决定当前数字的符号,然后将当前数字加入答案。
60+
61+
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为给定数字。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:排序**
60+
61+
`score` 按照第 $k$ 列的分数从大到小排序,然后返回即可。
62+
63+
时间复杂度 $O(m \times \log m)$,空间复杂度 $O(1)$。其中 $m$ 为 `score` 的行数。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:脑筋急转弯**
54+
55+
注意到 $1$ 其实是数字转换的“工具”,因此只要两个字符串中都有 $1$ 或者都没有 $1$,那么就可以通过操作使得两个字符串相等。
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串的长度。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@
7676

7777
<!-- 这里可写通用的实现逻辑 -->
7878

79+
**方法一:记忆化搜索**
80+
81+
我们设计一个函数 $dfs(i)$,表示从下标 $i$ 开始拆分的最小代价。那么答案就是 $dfs(0)$。
82+
83+
函数 $dfs(i)$ 的计算过程如下:
84+
85+
- 如果 $i \ge n$,说明已经拆分到了数组末尾,此时返回 $0$。
86+
- 否则,我们枚举子数组的末尾 $j$,过程中用一个数组或哈希表 `cnt` 统计子数组中每个数字出现的次数,用一个变量 `one` 统计子数组中出现次数为 $1$ 的数字的个数。那么子数组的重要性就是 $k + j - i + 1 - one$,拆分的代价就是 $k + j - i + 1 - one + dfs(j + 1)$。我们枚举所有的 $j$,取其中的最小值作为 $dfs(i)$ 的返回值。
87+
88+
过程中,我们可以使用记忆化搜索,即使用一个数组 $f$ 记忆化函数 $dfs(i)$ 的返回值,避免重复计算。
89+
90+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
91+
7992
<!-- tabs:start -->
8093

8194
### **Python3**

0 commit comments

Comments
 (0)