Skip to content

feat: update solutions to lc problems: No.2732+ #3163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 分别是矩阵的行数和列数。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心

我们可以从左到右遍历字符串 $s$,找到第一个不是 'a' 的字符所在的位置 $i$,然后找到从 $i$ 开始的第一个 'a' 字符所在的位置 $j$,将 $s[i:j]$ 中的字符都减一,最后返回处理后的字符串即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ tags:

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

Expand Down
47 changes: 0 additions & 47 deletions solution/2700-2799/2736.Maximum Sum Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> 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
Expand Down
47 changes: 0 additions & 47 deletions solution/2700-2799/2736.Maximum Sum Queries/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> 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
Expand Down
42 changes: 0 additions & 42 deletions solution/2700-2799/2736.Maximum Sum Queries/Solution2.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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}$ 的长度。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ For queries[1]: Only server with id 3 gets no request in the duration [2,4].

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$,此时也是无解;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ It can be proven, that 3 is the minimum number of operations that we need to per

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ tags:

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand Down
Loading