From 003b34a639521f63f30c0e800a29cae73bbec55b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 25 Jun 2024 12:46:16 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.2732+ --- .../README.md | 2 +- .../README_EN.md | 2 +- .../README.md | 6 ++- .../README_EN.md | 6 ++- .../2736.Maximum Sum Queries/README.md | 47 ------------------- .../2736.Maximum Sum Queries/README_EN.md | 47 ------------------- .../2736.Maximum Sum Queries/Solution2.java | 42 ----------------- .../2747.Count Zero Request Servers/README.md | 2 +- .../README_EN.md | 8 +++- .../README.md | 4 +- .../README_EN.md | 14 +++++- .../README_EN.md | 6 ++- 12 files changed, 40 insertions(+), 146 deletions(-) delete mode 100644 solution/2700-2799/2736.Maximum Sum Queries/Solution2.java diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md index 7f77570713e9a..112b4a8f3d078 100644 --- a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md @@ -94,7 +94,7 @@ tags: - 如果 $k = 4$,每一列的和最大为 $2$,此时一定是 $k = 2$ 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 $2$。我们在 $4$ 行中任意选取 $2$ 行,一共有 $C_4^2 = 6$ 种选法,那么存在至少 $6$ 个 $2$ 的列。由于列数 $n \le 5$,所以一定存在至少一列的和大于 $2$,所以 $k = 4$ 也不满足条件。 - 对于 $k \gt 4$ 且 $k$ 为偶数的情况,我们可以得出同样的结论,即 $k$ 一定不满足条件。 -综上所述,我们只需要考虑 $k = 1$ 和 $k = 2$ 的情况即可。即判断是否有一行全为 $0$,或者是否存在两行按位或之后的结果为 $0$。 +综上所述,我们只需要考虑 $k = 1$ 和 $k = 2$ 的情况即可。即判断是否有一行全为 $0$,或者是否存在两行按位与之后的结果为 $0$。 时间复杂度 $O(m \times n + 4^n)$,空间复杂度 $O(2^n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md index 4325a3a5e19d0..ea281d6093215 100644 --- a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md @@ -92,7 +92,7 @@ We can consider the number of rows $k$ chosen for the answer from smallest to la - 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. - For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition. -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$. +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$. 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. diff --git a/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README.md b/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README.md index 5bcd4fd9133e9..7fb8b44e6d9ce 100644 --- a/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README.md +++ b/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README.md @@ -74,7 +74,11 @@ tags: -### 方法一 +### 方法一:贪心 + +我们可以从左到右遍历字符串 $s$,找到第一个不是 'a' 的字符所在的位置 $i$,然后找到从 $i$ 开始的第一个 'a' 字符所在的位置 $j$,将 $s[i:j]$ 中的字符都减一,最后返回处理后的字符串即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 diff --git a/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README_EN.md b/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README_EN.md index 3ce3197c8f9a1..282189a85ee60 100644 --- a/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README_EN.md +++ b/solution/2700-2799/2734.Lexicographically Smallest String After Substring Operation/README_EN.md @@ -90,7 +90,11 @@ tags: -### Solution 1 +### Solution 1: Greedy Algorithm + +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. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$. diff --git a/solution/2700-2799/2736.Maximum Sum Queries/README.md b/solution/2700-2799/2736.Maximum Sum Queries/README.md index 314658c5846c5..6de4140384583 100644 --- a/solution/2700-2799/2736.Maximum Sum Queries/README.md +++ b/solution/2700-2799/2736.Maximum Sum Queries/README.md @@ -210,53 +210,6 @@ class Solution { } ``` -#### Java - -```java -class Solution { - public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { - int n = nums1.length, m = q.length; - int[][] a = new int[n][2]; - for (int i = 0; i < n; i++) { - a[i][0] = nums1[i]; - a[i][1] = nums2[i]; - } - int[][] b = new int[m][3]; - for (int i = 0; i < m; i++) { - b[i][0] = q[i][0]; - b[i][1] = q[i][1]; - b[i][2] = i; - } - Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); - Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); - TreeMap map = new TreeMap<>(); - int[] res = new int[m]; - int max = -1; - for (int i = m - 1, j = n - 1; i >= 0; i--) { - int x = b[i][0], y = b[i][1], idx = b[i][2]; - while (j >= 0 && a[j][0] >= x) { - if (max < a[j][1]) { - max = a[j][1]; - Integer key = map.floorKey(a[j][1]); - while (key != null && map.get(key) <= a[j][0] + a[j][1]) { - map.remove(key); - key = map.floorKey(key); - } - map.put(max, a[j][0] + a[j][1]); - } - j--; - } - Integer key = map.ceilingKey(y); - if (key == null) - res[idx] = -1; - else - res[idx] = map.get(key); - } - return res; - } -} -``` - #### C++ ```cpp diff --git a/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md b/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md index c0296eba71310..d06ae4bc604ba 100644 --- a/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md +++ b/solution/2700-2799/2736.Maximum Sum Queries/README_EN.md @@ -214,53 +214,6 @@ class Solution { } ``` -#### Java - -```java -class Solution { - public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { - int n = nums1.length, m = q.length; - int[][] a = new int[n][2]; - for (int i = 0; i < n; i++) { - a[i][0] = nums1[i]; - a[i][1] = nums2[i]; - } - int[][] b = new int[m][3]; - for (int i = 0; i < m; i++) { - b[i][0] = q[i][0]; - b[i][1] = q[i][1]; - b[i][2] = i; - } - Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); - Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); - TreeMap map = new TreeMap<>(); - int[] res = new int[m]; - int max = -1; - for (int i = m - 1, j = n - 1; i >= 0; i--) { - int x = b[i][0], y = b[i][1], idx = b[i][2]; - while (j >= 0 && a[j][0] >= x) { - if (max < a[j][1]) { - max = a[j][1]; - Integer key = map.floorKey(a[j][1]); - while (key != null && map.get(key) <= a[j][0] + a[j][1]) { - map.remove(key); - key = map.floorKey(key); - } - map.put(max, a[j][0] + a[j][1]); - } - j--; - } - Integer key = map.ceilingKey(y); - if (key == null) - res[idx] = -1; - else - res[idx] = map.get(key); - } - return res; - } -} -``` - #### C++ ```cpp diff --git a/solution/2700-2799/2736.Maximum Sum Queries/Solution2.java b/solution/2700-2799/2736.Maximum Sum Queries/Solution2.java deleted file mode 100644 index c95f2d9b4c925..0000000000000 --- a/solution/2700-2799/2736.Maximum Sum Queries/Solution2.java +++ /dev/null @@ -1,42 +0,0 @@ -class Solution { - public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) { - int n = nums1.length, m = q.length; - int[][] a = new int[n][2]; - for (int i = 0; i < n; i++) { - a[i][0] = nums1[i]; - a[i][1] = nums2[i]; - } - int[][] b = new int[m][3]; - for (int i = 0; i < m; i++) { - b[i][0] = q[i][0]; - b[i][1] = q[i][1]; - b[i][2] = i; - } - Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]); - Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]); - TreeMap map = new TreeMap<>(); - int[] res = new int[m]; - int max = -1; - for (int i = m - 1, j = n - 1; i >= 0; i--) { - int x = b[i][0], y = b[i][1], idx = b[i][2]; - while (j >= 0 && a[j][0] >= x) { - if (max < a[j][1]) { - max = a[j][1]; - Integer key = map.floorKey(a[j][1]); - while (key != null && map.get(key) <= a[j][0] + a[j][1]) { - map.remove(key); - key = map.floorKey(key); - } - map.put(max, a[j][0] + a[j][1]); - } - j--; - } - Integer key = map.ceilingKey(y); - if (key == null) - res[idx] = -1; - else - res[idx] = map.get(key); - } - return res; - } -} \ No newline at end of file diff --git a/solution/2700-2799/2747.Count Zero Request Servers/README.md b/solution/2700-2799/2747.Count Zero Request Servers/README.md index 5a9276db7cb46..4620843d987cf 100644 --- a/solution/2700-2799/2747.Count Zero Request Servers/README.md +++ b/solution/2700-2799/2747.Count Zero Request Servers/README.md @@ -78,7 +78,7 @@ tags: 对于每个查询 $q = (r, i)$,其窗口左边界为 $l = r - x$,我们需要统计在窗口 $[l, r]$ 内有多少个服务器收到了请求。我们用双指针 $j$ 和 $k$ 分别维护窗口的左右边界,初始时 $j = k = 0$。每一次,如果 $k$ 指向的日志的时间小于等于 $r$,我们就将其加入到窗口中,然后将 $k$ 向右移动一位。如果 $j$ 指向的日志的时间小于 $l$,我们就将其从窗口中移除,然后将 $j$ 向右移动一位。在移动的过程中,我们需要统计窗口中有多少个不同的服务器,这可以使用哈希表来实现。移动结束后,当前时间区间中没有收到请求的服务器数目就是 $n$ 减去哈希表中不同的服务器数目。 -时间复杂度 $O(l \times \log l + m \times \log m + n)$,空间复杂度 $O(l + m)$。其中 $l$ 和 $n$ 分别是数组 $logs$ 的长度和服务器的数量,而 $m$ 是数组 $queries$ 的长度。 +时间复杂度 $O(l \times \log l + m \times \log m + n)$,空间复杂度 $O(l + m)$。其中 $l$ 和 $n$ 分别是数组 $\text{logs}$ 的长度和服务器的数量,而 $m$ 是数组 $\text{queries}$ 的长度。 diff --git a/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md b/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md index 821a87a3fce9c..1c6a234423f98 100644 --- a/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md +++ b/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md @@ -72,7 +72,13 @@ For queries[1]: Only server with id 3 gets no request in the duration [2,4]. -### Solution 1 +### Solution 1: Offline Queries + Sorting + Two Pointers + +We can sort all the queries by time from smallest to largest, and then process each query in chronological order. + +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. + +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}$. diff --git a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md index 3085eac02d571..991a8d0fe2faa 100644 --- a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md +++ b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md @@ -66,9 +66,9 @@ tags: ### 方法一:枚举 -如果我们操作了 $k$ 次,那么问题实际上就变成了:判断 $num1 - k \times num2$ 能否拆分成 $k$ 个 $2^i$ 之和。 +如果我们操作了 $k$ 次,那么问题实际上就变成了:判断 $\text{num1} - k \times \text{num2}$ 能否拆分成 $k$ 个 $2^i$ 之和。 -我们不妨假设 $x = num1 - k \times num2$,接下来分类讨论: +我们不妨假设 $x = \text{num1} - k \times \text{num2}$,接下来分类讨论: - 如果 $x \lt 0$,那么 $x$ 无法拆分成 $k$ 个 $2^i$ 之和,因为 $2^i \gt 0$,显然无解; - 如果 $x$ 的二进制表示中 $1$ 的个数大于 $k$,此时也是无解; diff --git a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md index 9c856a5f39992..a130bb5ac3137 100644 --- a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md +++ b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md @@ -62,7 +62,19 @@ It can be proven, that 3 is the minimum number of operations that we need to per -### Solution 1 +### Solution 1: Enumeration + +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. + +Let's assume $x = \text{num1} - k \times \text{num2}$. Next, we discuss in categories: + +- 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; +- If the number of $1$s in the binary representation of $x$ is greater than $k$, there is also no solution in this case; +- Otherwise, for the current $k$, there must exist a splitting scheme. + +Therefore, we start enumerating $k$ from $1$. Once we find a $k$ that meets the condition, we can directly return the answer. + +The time complexity is $O(\log x)$, and the space complexity is $O(1)$. diff --git a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md index 4b05464c56e17..97cbb1c4236ff 100644 --- a/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md +++ b/solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md @@ -62,7 +62,11 @@ tags: -### Solution 1 +### Solution 1: Multiplication Principle + +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$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.