Skip to content

feat: update solutions to lc problems: No.2164,2732 #3162

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 2 commits 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
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
mkdocs build -f mkdocs-en.yml

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ It can be shown that it is not possible to obtain a difference smaller than 1.

<!-- solution:start -->

### Solution 1
### Solution 1: Priority Queue (Max and Min Heap) + Prefix and Suffix Sum + Enumeration of Split Points

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.

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

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.

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

<!-- tabs:start -->

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

<!-- solution:start -->

### 方法一
### 方法一:排序

我们可以将奇数下标和偶数下标分别取出来,然后对奇数下标的数组进行非递增排序,对偶数下标的数组进行非递减排序,最后再将两个数组合并即可。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -133,16 +137,21 @@ public:
vector<int> a;
vector<int> b;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
if (i % 2 == 0) {
a.push_back(nums[i]);
else
} else {
b.push_back(nums[i]);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end(), greater<int>());
sort(b.rbegin(), b.rend());
vector<int> ans(n);
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
ans[i] = a[j];
}
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
ans[i] = b[j];
}
return ans;
}
};
Expand Down Expand Up @@ -177,6 +186,33 @@ func sortEvenOdd(nums []int) []int {
}
```

#### TypeScript

```ts
function sortEvenOdd(nums: number[]): number[] {
const n = nums.length;
const a: number[] = [];
const b: number[] = [];
for (let i = 0; i < n; ++i) {
if (i % 2 === 0) {
a.push(nums[i]);
} else {
b.push(nums[i]);
}
}
a.sort((x, y) => x - y);
b.sort((x, y) => y - x);
const ans: number[] = [];
for (let i = 0, j = 0; j < a.length; i += 2, ++j) {
ans[i] = a[j];
}
for (let i = 1, j = 0; j < b.length; i += 2, ++j) {
ans[i] = b[j];
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ The resultant array formed is [2,1], which is the same as the initial array.

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

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.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -131,16 +135,21 @@ public:
vector<int> a;
vector<int> b;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
if (i % 2 == 0) {
a.push_back(nums[i]);
else
} else {
b.push_back(nums[i]);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end(), greater<int>());
sort(b.rbegin(), b.rend());
vector<int> ans(n);
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
ans[i] = a[j];
}
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
ans[i] = b[j];
}
return ans;
}
};
Expand Down Expand Up @@ -175,6 +184,33 @@ func sortEvenOdd(nums []int) []int {
}
```

#### TypeScript

```ts
function sortEvenOdd(nums: number[]): number[] {
const n = nums.length;
const a: number[] = [];
const b: number[] = [];
for (let i = 0; i < n; ++i) {
if (i % 2 === 0) {
a.push(nums[i]);
} else {
b.push(nums[i]);
}
}
a.sort((x, y) => x - y);
b.sort((x, y) => y - x);
const ans: number[] = [];
for (let i = 0, j = 0; j < a.length; i += 2, ++j) {
ans[i] = a[j];
}
for (let i = 1, j = 0; j < b.length; i += 2, ++j) {
ans[i] = b[j];
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ class Solution {
vector<int> a;
vector<int> b;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
if (i % 2 == 0) {
a.push_back(nums[i]);
else
} else {
b.push_back(nums[i]);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end(), greater<int>());
sort(b.rbegin(), b.rend());
vector<int> ans(n);
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
ans[i] = a[j];
}
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
ans[i] = b[j];
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function sortEvenOdd(nums: number[]): number[] {
const n = nums.length;
const a: number[] = [];
const b: number[] = [];
for (let i = 0; i < n; ++i) {
if (i % 2 === 0) {
a.push(nums[i]);
} else {
b.push(nums[i]);
}
}
a.sort((x, y) => x - y);
b.sort((x, y) => y - x);
const ans: number[] = [];
for (let i = 0, j = 0; j < a.length; i += 2, ++j) {
ans[i] = a[j];
}
for (let i = 1, j = 0; j < b.length; i += 2, ++j) {
ans[i] = b[j];
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:分情况讨论

我们可以从小到大考虑答案选择的行数 $k$。

- 如果 $k = 1$,每一列的和最大为 $0$,那么必须满足有一行的所有元素都是 $0$,否则无法满足条件。
- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位或之后的结果是 $0$,否则无法满足条件。
- 如果 $k = 3$,每一列的和最大也是 $1$。如果 $k = 2$ 不满足条件,那么 $k = 3$ 也一定不满足条件,所以我们不需要考虑所有 $k \gt 2$ 且 $k$ 为奇数的情况。
- 如果 $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$。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,19 @@ The length of the chosen subset is 1.

<!-- solution:start -->

### Solution 1
### Solution 1: Case Analysis

We can consider the number of rows $k$ chosen for the answer from smallest to largest.

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

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.

<!-- tabs:start -->

Expand Down
Loading