Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c1ace34

Browse files
authoredJun 25, 2024··
feat: update solutions to lc problems: No.2732+ (#3163)
1 parent ecd44c1 commit c1ace34

File tree

12 files changed

+40
-146
lines changed

12 files changed

+40
-146
lines changed
 

‎solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ tags:
9494
- 如果 $k = 4$,每一列的和最大为 $2$,此时一定是 $k = 2$ 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 $2$。我们在 $4$ 行中任意选取 $2$ 行,一共有 $C_4^2 = 6$ 种选法,那么存在至少 $6$ 个 $2$ 的列。由于列数 $n \le 5$,所以一定存在至少一列的和大于 $2$,所以 $k = 4$ 也不满足条件。
9595
- 对于 $k \gt 4$ 且 $k$ 为偶数的情况,我们可以得出同样的结论,即 $k$ 一定不满足条件。
9696

97-
综上所述,我们只需要考虑 $k = 1$ 和 $k = 2$ 的情况即可。即判断是否有一行全为 $0$,或者是否存在两行按位或之后的结果为 $0$。
97+
综上所述,我们只需要考虑 $k = 1$ 和 $k = 2$ 的情况即可。即判断是否有一行全为 $0$,或者是否存在两行按位与之后的结果为 $0$。
9898

9999
时间复杂度 $O(m \times n + 4^n)$,空间复杂度 $O(2^n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
100100

‎solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ We can consider the number of rows $k$ chosen for the answer from smallest to la
9292
- If $k = 4$, the maximum sum of each column is $2$. This situation definitely occurs when the condition for $k = 2$ is not met, meaning that for any two selected rows, there exists at least one column with a sum of $2$. When choosing any 2 rows out of 4, there are a total of $C_4^2 = 6$ ways to choose, so there are at least $6$ columns with a sum of $2$. Since the number of columns $n \le 5$, there must be at least one column with a sum greater than $2$, so the condition for $k = 4$ is also not met.
9393
- For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition.
9494

95-
In summary, we only need to consider the cases of $k = 1$ and $k = 2$. That is, to check whether there is a row entirely composed of $0$s, or whether there exist two rows whose bitwise OR result is $0$.
95+
In summary, we only need to consider the cases of $k = 1$ and $k = 2$. That is, to check whether there is a row entirely composed of $0$s, or whether there exist two rows whose bitwise AND result is $0$.
9696

9797
The time complexity is $O(m \times n + 4^n)$, and the space complexity is $O(2^n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.
9898

‎solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一
77+
### 方法一:贪心
78+
79+
我们可以从左到右遍历字符串 $s$,找到第一个不是 'a' 的字符所在的位置 $i$,然后找到从 $i$ 开始的第一个 'a' 字符所在的位置 $j$,将 $s[i:j]$ 中的字符都减一,最后返回处理后的字符串即可。
80+
81+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
7882

7983
<!-- tabs:start -->
8084

‎solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ tags:
9090

9191
<!-- solution:start -->
9292

93-
### Solution 1
93+
### Solution 1: Greedy Algorithm
94+
95+
We can traverse the string $s$ from left to right, find the position $i$ of the first character that is not 'a', and then find the position $j$ of the first 'a' character starting from $i$. We decrement each character in $s[i:j]$, and finally return the processed string.
96+
97+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
9498

9599
<!-- tabs:start -->
96100

‎solution/2700-2799/2736.Maximum Sum Queries/README.md

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -210,53 +210,6 @@ class Solution {
210210
}
211211
```
212212

213-
#### Java
214-
215-
```java
216-
class Solution {
217-
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
218-
int n = nums1.length, m = q.length;
219-
int[][] a = new int[n][2];
220-
for (int i = 0; i < n; i++) {
221-
a[i][0] = nums1[i];
222-
a[i][1] = nums2[i];
223-
}
224-
int[][] b = new int[m][3];
225-
for (int i = 0; i < m; i++) {
226-
b[i][0] = q[i][0];
227-
b[i][1] = q[i][1];
228-
b[i][2] = i;
229-
}
230-
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
231-
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
232-
TreeMap<Integer, Integer> map = new TreeMap<>();
233-
int[] res = new int[m];
234-
int max = -1;
235-
for (int i = m - 1, j = n - 1; i >= 0; i--) {
236-
int x = b[i][0], y = b[i][1], idx = b[i][2];
237-
while (j >= 0 && a[j][0] >= x) {
238-
if (max < a[j][1]) {
239-
max = a[j][1];
240-
Integer key = map.floorKey(a[j][1]);
241-
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
242-
map.remove(key);
243-
key = map.floorKey(key);
244-
}
245-
map.put(max, a[j][0] + a[j][1]);
246-
}
247-
j--;
248-
}
249-
Integer key = map.ceilingKey(y);
250-
if (key == null)
251-
res[idx] = -1;
252-
else
253-
res[idx] = map.get(key);
254-
}
255-
return res;
256-
}
257-
}
258-
```
259-
260213
#### C++
261214

262215
```cpp

‎solution/2700-2799/2736.Maximum Sum Queries/README_EN.md

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -214,53 +214,6 @@ class Solution {
214214
}
215215
```
216216

217-
#### Java
218-
219-
```java
220-
class Solution {
221-
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
222-
int n = nums1.length, m = q.length;
223-
int[][] a = new int[n][2];
224-
for (int i = 0; i < n; i++) {
225-
a[i][0] = nums1[i];
226-
a[i][1] = nums2[i];
227-
}
228-
int[][] b = new int[m][3];
229-
for (int i = 0; i < m; i++) {
230-
b[i][0] = q[i][0];
231-
b[i][1] = q[i][1];
232-
b[i][2] = i;
233-
}
234-
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
235-
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
236-
TreeMap<Integer, Integer> map = new TreeMap<>();
237-
int[] res = new int[m];
238-
int max = -1;
239-
for (int i = m - 1, j = n - 1; i >= 0; i--) {
240-
int x = b[i][0], y = b[i][1], idx = b[i][2];
241-
while (j >= 0 && a[j][0] >= x) {
242-
if (max < a[j][1]) {
243-
max = a[j][1];
244-
Integer key = map.floorKey(a[j][1]);
245-
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
246-
map.remove(key);
247-
key = map.floorKey(key);
248-
}
249-
map.put(max, a[j][0] + a[j][1]);
250-
}
251-
j--;
252-
}
253-
Integer key = map.ceilingKey(y);
254-
if (key == null)
255-
res[idx] = -1;
256-
else
257-
res[idx] = map.get(key);
258-
}
259-
return res;
260-
}
261-
}
262-
```
263-
264217
#### C++
265218

266219
```cpp

‎solution/2700-2799/2736.Maximum Sum Queries/Solution2.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎solution/2700-2799/2747.Count Zero Request Servers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ tags:
7878

7979
对于每个查询 $q = (r, i)$,其窗口左边界为 $l = r - x$,我们需要统计在窗口 $[l, r]$ 内有多少个服务器收到了请求。我们用双指针 $j$ 和 $k$ 分别维护窗口的左右边界,初始时 $j = k = 0$。每一次,如果 $k$ 指向的日志的时间小于等于 $r$,我们就将其加入到窗口中,然后将 $k$ 向右移动一位。如果 $j$ 指向的日志的时间小于 $l$,我们就将其从窗口中移除,然后将 $j$ 向右移动一位。在移动的过程中,我们需要统计窗口中有多少个不同的服务器,这可以使用哈希表来实现。移动结束后,当前时间区间中没有收到请求的服务器数目就是 $n$ 减去哈希表中不同的服务器数目。
8080

81-
时间复杂度 $O(l \times \log l + m \times \log m + n)$,空间复杂度 $O(l + m)$。其中 $l$ 和 $n$ 分别是数组 $logs$ 的长度和服务器的数量,而 $m$ 是数组 $queries$ 的长度。
81+
时间复杂度 $O(l \times \log l + m \times \log m + n)$,空间复杂度 $O(l + m)$。其中 $l$ 和 $n$ 分别是数组 $\text{logs}$ 的长度和服务器的数量,而 $m$ 是数组 $\text{queries}$ 的长度。
8282

8383
<!-- tabs:start -->
8484

‎solution/2700-2799/2747.Count Zero Request Servers/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ For queries[1]: Only server with id 3 gets no request in the duration [2,4].
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Offline Queries + Sorting + Two Pointers
76+
77+
We can sort all the queries by time from smallest to largest, and then process each query in chronological order.
78+
79+
For each query $q = (r, i)$, its window left boundary is $l = r - x$, and we need to count how many servers received requests within the window $[l, r]$. We use two pointers $j$ and $k$ to maintain the left and right boundaries of the window, initially $j = k = 0$. Each time, if the log time pointed by $k$ is less than or equal to $r$, we add it to the window, and then move $k$ to the right by one. If the log time pointed by $j$ is less than $l$, we remove it from the window, and then move $j$ to the right by one. During the movement, we need to count how many different servers are in the window, which can be implemented using a hash table. After the movement, the number of servers that did not receive requests in the current time interval is $n$ minus the number of different servers in the hash table.
80+
81+
The time complexity is $O(l \times \log l + m \times \log m + n)$, and the space complexity is $O(l + m)$. Here, $l$ and $n$ are the lengths of the arrays $\text{logs}$ and the number of servers, respectively, while $m$ is the length of the array $\text{queries}$.
7682

7783
<!-- tabs:start -->
7884

‎solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ tags:
6666

6767
### 方法一:枚举
6868

69-
如果我们操作了 $k$ 次,那么问题实际上就变成了:判断 $num1 - k \times num2$ 能否拆分成 $k$ 个 $2^i$ 之和。
69+
如果我们操作了 $k$ 次,那么问题实际上就变成了:判断 $\text{num1} - k \times \text{num2}$ 能否拆分成 $k$ 个 $2^i$ 之和。
7070

71-
我们不妨假设 $x = num1 - k \times num2$,接下来分类讨论:
71+
我们不妨假设 $x = \text{num1} - k \times \text{num2}$,接下来分类讨论:
7272

7373
- 如果 $x \lt 0$,那么 $x$ 无法拆分成 $k$ 个 $2^i$ 之和,因为 $2^i \gt 0$,显然无解;
7474
- 如果 $x$ 的二进制表示中 $1$ 的个数大于 $k$,此时也是无解;

‎solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ It can be proven, that 3 is the minimum number of operations that we need to per
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Enumeration
66+
67+
If we operate $k$ times, then the problem essentially becomes: determining whether $\text{num1} - k \times \text{num2}$ can be split into the sum of $k$ $2^i$s.
68+
69+
Let's assume $x = \text{num1} - k \times \text{num2}$. Next, we discuss in categories:
70+
71+
- If $x < 0$, then $x$ cannot be split into the sum of $k$ $2^i$s, because $2^i > 0$, which obviously has no solution;
72+
- If the number of $1$s in the binary representation of $x$ is greater than $k$, there is also no solution in this case;
73+
- Otherwise, for the current $k$, there must exist a splitting scheme.
74+
75+
Therefore, we start enumerating $k$ from $1$. Once we find a $k$ that meets the condition, we can directly return the answer.
76+
77+
The time complexity is $O(\log x)$, and the space complexity is $O(1)$.
6678

6779
<!-- tabs:start -->
6880

‎solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Multiplication Principle
66+
67+
Based on the problem description, we can draw a dividing line between two $1$s. Assuming the indices of the two $1$s are $j$ and $i$ respectively, then the number of different dividing lines that can be drawn is $i - j$. We find all the pairs of $j$ and $i$ that meet the condition, and then multiply all the $i - j$ together. If no dividing line can be found between two $1$s, it means there are no $1$s in the array, and the answer is $0$.
68+
69+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
6670

6771
<!-- tabs:start -->
6872

0 commit comments

Comments
 (0)
Please sign in to comment.