Skip to content

Commit ecd44c1

Browse files
authored
feat: update solutions to lc problems: No.2164,2732 (#3162)
* No.2164.Sort Even and Odd Indices Independently * No.2732.Find a Good Subset of the Matrix
1 parent f6c2bf4 commit ecd44c1

File tree

8 files changed

+152
-21
lines changed

8 files changed

+152
-21
lines changed

.github/workflows/deploy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
mkdocs build -f mkdocs-en.yml
7070
7171
- name: Deploy
72-
uses: peaceiris/actions-gh-pages@v3
72+
uses: peaceiris/actions-gh-pages@v4
7373
with:
7474
github_token: ${{ secrets.GITHUB_TOKEN }}
7575
publish_dir: ./site

solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ It can be shown that it is not possible to obtain a difference smaller than 1.
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Priority Queue (Max and Min Heap) + Prefix and Suffix Sum + Enumeration of Split Points
82+
83+
The problem is essentially equivalent to finding a split point in $nums$, dividing the array into two parts. In the first part, select the smallest $n$ elements, and in the second part, select the largest $n$ elements, so that the difference between the sums of the two parts is minimized.
84+
85+
We can use a max heap to maintain the smallest $n$ elements in the prefix, and a min heap to maintain the largest $n$ elements in the suffix. We define $pre[i]$ as the sum of the smallest $n$ elements among the first $i$ elements of the array $nums$, and $suf[i]$ as the sum of the largest $n$ elements from the $i$-th element to the last element of the array. During the process of maintaining the max and min heaps, update the values of $pre[i]$ and $suf[i]$.
86+
87+
Finally, we enumerate the split points in the range of $i \in [n, 2n]$, calculate the value of $pre[i] - suf[i + 1]$, and take the minimum value.
88+
89+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
8290

8391
<!-- tabs:start -->
8492

solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md

+42-6
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### 方法一
81+
### 方法一:排序
82+
83+
我们可以将奇数下标和偶数下标分别取出来,然后对奇数下标的数组进行非递增排序,对偶数下标的数组进行非递减排序,最后再将两个数组合并即可。
84+
85+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。
8286

8387
<!-- tabs:start -->
8488

@@ -133,16 +137,21 @@ public:
133137
vector<int> a;
134138
vector<int> b;
135139
for (int i = 0; i < n; ++i) {
136-
if (i % 2 == 0)
140+
if (i % 2 == 0) {
137141
a.push_back(nums[i]);
138-
else
142+
} else {
139143
b.push_back(nums[i]);
144+
}
140145
}
141146
sort(a.begin(), a.end());
142-
sort(b.begin(), b.end(), greater<int>());
147+
sort(b.rbegin(), b.rend());
143148
vector<int> ans(n);
144-
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
145-
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
149+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
150+
ans[i] = a[j];
151+
}
152+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
153+
ans[i] = b[j];
154+
}
146155
return ans;
147156
}
148157
};
@@ -177,6 +186,33 @@ func sortEvenOdd(nums []int) []int {
177186
}
178187
```
179188

189+
#### TypeScript
190+
191+
```ts
192+
function sortEvenOdd(nums: number[]): number[] {
193+
const n = nums.length;
194+
const a: number[] = [];
195+
const b: number[] = [];
196+
for (let i = 0; i < n; ++i) {
197+
if (i % 2 === 0) {
198+
a.push(nums[i]);
199+
} else {
200+
b.push(nums[i]);
201+
}
202+
}
203+
a.sort((x, y) => x - y);
204+
b.sort((x, y) => y - x);
205+
const ans: number[] = [];
206+
for (let i = 0, j = 0; j < a.length; i += 2, ++j) {
207+
ans[i] = a[j];
208+
}
209+
for (let i = 1, j = 0; j < b.length; i += 2, ++j) {
210+
ans[i] = b[j];
211+
}
212+
return ans;
213+
}
214+
```
215+
180216
<!-- tabs:end -->
181217

182218
<!-- solution:end -->

solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md

+42-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ The resultant array formed is [2,1], which is the same as the initial array.
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Sorting
80+
81+
We can extract the elements at odd and even indices separately, then sort the array of odd indices in non-increasing order and the array of even indices in non-decreasing order. Finally, merge the two arrays back together.
82+
83+
The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{nums}$.
8084

8185
<!-- tabs:start -->
8286

@@ -131,16 +135,21 @@ public:
131135
vector<int> a;
132136
vector<int> b;
133137
for (int i = 0; i < n; ++i) {
134-
if (i % 2 == 0)
138+
if (i % 2 == 0) {
135139
a.push_back(nums[i]);
136-
else
140+
} else {
137141
b.push_back(nums[i]);
142+
}
138143
}
139144
sort(a.begin(), a.end());
140-
sort(b.begin(), b.end(), greater<int>());
145+
sort(b.rbegin(), b.rend());
141146
vector<int> ans(n);
142-
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
143-
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
147+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
148+
ans[i] = a[j];
149+
}
150+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
151+
ans[i] = b[j];
152+
}
144153
return ans;
145154
}
146155
};
@@ -175,6 +184,33 @@ func sortEvenOdd(nums []int) []int {
175184
}
176185
```
177186

187+
#### TypeScript
188+
189+
```ts
190+
function sortEvenOdd(nums: number[]): number[] {
191+
const n = nums.length;
192+
const a: number[] = [];
193+
const b: number[] = [];
194+
for (let i = 0; i < n; ++i) {
195+
if (i % 2 === 0) {
196+
a.push(nums[i]);
197+
} else {
198+
b.push(nums[i]);
199+
}
200+
}
201+
a.sort((x, y) => x - y);
202+
b.sort((x, y) => y - x);
203+
const ans: number[] = [];
204+
for (let i = 0, j = 0; j < a.length; i += 2, ++j) {
205+
ans[i] = a[j];
206+
}
207+
for (let i = 1, j = 0; j < b.length; i += 2, ++j) {
208+
ans[i] = b[j];
209+
}
210+
return ans;
211+
}
212+
```
213+
178214
<!-- tabs:end -->
179215

180216
<!-- solution:end -->

solution/2100-2199/2164.Sort Even and Odd Indices Independently/Solution.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ class Solution {
55
vector<int> a;
66
vector<int> b;
77
for (int i = 0; i < n; ++i) {
8-
if (i % 2 == 0)
8+
if (i % 2 == 0) {
99
a.push_back(nums[i]);
10-
else
10+
} else {
1111
b.push_back(nums[i]);
12+
}
1213
}
1314
sort(a.begin(), a.end());
14-
sort(b.begin(), b.end(), greater<int>());
15+
sort(b.rbegin(), b.rend());
1516
vector<int> ans(n);
16-
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
17-
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
17+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
18+
ans[i] = a[j];
19+
}
20+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
21+
ans[i] = b[j];
22+
}
1823
return ans;
1924
}
2025
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function sortEvenOdd(nums: number[]): number[] {
2+
const n = nums.length;
3+
const a: number[] = [];
4+
const b: number[] = [];
5+
for (let i = 0; i < n; ++i) {
6+
if (i % 2 === 0) {
7+
a.push(nums[i]);
8+
} else {
9+
b.push(nums[i]);
10+
}
11+
}
12+
a.sort((x, y) => x - y);
13+
b.sort((x, y) => y - x);
14+
const ans: number[] = [];
15+
for (let i = 0, j = 0; j < a.length; i += 2, ++j) {
16+
ans[i] = a[j];
17+
}
18+
for (let i = 1, j = 0; j < b.length; i += 2, ++j) {
19+
ans[i] = b[j];
20+
}
21+
return ans;
22+
}

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,19 @@ tags:
8484

8585
<!-- solution:start -->
8686

87-
### 方法一
87+
### 方法一:分情况讨论
88+
89+
我们可以从小到大考虑答案选择的行数 $k$。
90+
91+
- 如果 $k = 1$,每一列的和最大为 $0$,那么必须满足有一行的所有元素都是 $0$,否则无法满足条件。
92+
- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位或之后的结果是 $0$,否则无法满足条件。
93+
- 如果 $k = 3$,每一列的和最大也是 $1$。如果 $k = 2$ 不满足条件,那么 $k = 3$ 也一定不满足条件,所以我们不需要考虑所有 $k \gt 2$ 且 $k$ 为奇数的情况。
94+
- 如果 $k = 4$,每一列的和最大为 $2$,此时一定是 $k = 2$ 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 $2$。我们在 $4$ 行中任意选取 $2$ 行,一共有 $C_4^2 = 6$ 种选法,那么存在至少 $6$ 个 $2$ 的列。由于列数 $n \le 5$,所以一定存在至少一列的和大于 $2$,所以 $k = 4$ 也不满足条件。
95+
- 对于 $k \gt 4$ 且 $k$ 为偶数的情况,我们可以得出同样的结论,即 $k$ 一定不满足条件。
96+
97+
综上所述,我们只需要考虑 $k = 1$ 和 $k = 2$ 的情况即可。即判断是否有一行全为 $0$,或者是否存在两行按位或之后的结果为 $0$。
98+
99+
时间复杂度 $O(m \times n + 4^n)$,空间复杂度 $O(2^n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
88100

89101
<!-- tabs:start -->
90102

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@ The length of the chosen subset is 1.
8282

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

85-
### Solution 1
85+
### Solution 1: Case Analysis
86+
87+
We can consider the number of rows $k$ chosen for the answer from smallest to largest.
88+
89+
- If $k = 1$, the maximum sum of each column is $0$. Therefore, there must be a row where all elements are $0$, otherwise, the condition cannot be met.
90+
- If $k = 2$, the maximum sum of each column is $1$. There must exist two rows, and the bitwise OR result of these two rows' elements is $0$, otherwise, the condition cannot be met.
91+
- If $k = 3$, the maximum sum of each column is also $1$. If the condition for $k = 2$ is not met, then the condition for $k = 3$ will definitely not be met either. Therefore, we do not need to consider any case where $k > 2$ and $k$ is odd.
92+
- 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.
93+
- For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition.
94+
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$.
96+
97+
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.
8698

8799
<!-- tabs:start -->
88100

0 commit comments

Comments
 (0)