Skip to content

[pull] main from doocs:main #425

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
Mar 19, 2025
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
42 changes: 27 additions & 15 deletions solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,6 @@ func divideArray(nums []int) bool {
}
```

#### TypeScript

```ts
function divideArray(nums: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
return Object.values(cnt).every(x => x % 2 === 0);
}
```

#### Rust

```rust
Expand All @@ -172,6 +160,24 @@ impl Solution {
}
```

#### TypeScript

```ts
function divideArray(nums: number[]): boolean {
const cnt = Array(501).fill(0);

for (const x of nums) {
cnt[x]++;
}

for (const x of cnt) {
if (x & 1) return false;
}

return true;
}
```

#### JavaScript

```js
Expand All @@ -180,11 +186,17 @@ impl Solution {
* @return {boolean}
*/
var divideArray = function (nums) {
const cnt = {};
const cnt = Array(501).fill(0);

for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
cnt[x]++;
}
return Object.values(cnt).every(x => x % 2 === 0);

for (const x of cnt) {
if (x & 1) return false;
}

return true;
};
```

Expand Down
46 changes: 29 additions & 17 deletions solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [3,2,3,2,2,2]
<strong>Output:</strong> true
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
</pre>
Expand All @@ -48,7 +48,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
</pre>

Expand Down Expand Up @@ -142,18 +142,6 @@ func divideArray(nums []int) bool {
}
```

#### TypeScript

```ts
function divideArray(nums: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
return Object.values(cnt).every(x => x % 2 === 0);
}
```

#### Rust

```rust
Expand All @@ -170,6 +158,24 @@ impl Solution {
}
```

#### TypeScript

```ts
function divideArray(nums: number[]): boolean {
const cnt = Array(501).fill(0);

for (const x of nums) {
cnt[x]++;
}

for (const x of cnt) {
if (x & 1) return false;
}

return true;
}
```

#### JavaScript

```js
Expand All @@ -178,11 +184,17 @@ impl Solution {
* @return {boolean}
*/
var divideArray = function (nums) {
const cnt = {};
const cnt = Array(501).fill(0);

for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
cnt[x]++;
}
return Object.values(cnt).every(x => x % 2 === 0);

for (const x of cnt) {
if (x & 1) return false;
}

return true;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
* @return {boolean}
*/
var divideArray = function (nums) {
const cnt = {};
const cnt = Array(501).fill(0);

for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
cnt[x]++;
}
return Object.values(cnt).every(x => x % 2 === 0);

for (const x of cnt) {
if (x & 1) return false;
}

return true;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function divideArray(nums: number[]): boolean {
const cnt: Record<number, number> = {};
const cnt = Array(501).fill(0);

for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
cnt[x]++;
}
return Object.values(cnt).every(x => x % 2 === 0);

for (const x of cnt) {
if (x & 1) return false;
}

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ nums 中的所有元素都有用到,并且每一行都由不同的整数组成

### 方法一:数组或哈希表

我们先用数组或哈希表 $cnt$ 统计数组 $nums$ 中每个元素出现的次数。
我们先用一个数组或者哈希表 $\textit{cnt}$ 统计数组 $\textit{nums}$ 中每个元素出现的次数。

然后遍历 $cnt$,对于每个元素 $x$,我们将其添加到答案列表中的第 $0$ 行,第 $1$ 行,第 $2$ 行,...,第 $cnt[x]-1$ 行。
然后遍历 $\textit{cnt}$,对于每个元素 $x$,我们将其添加到答案列表中的第 $0$ 行,第 $1$ 行,第 $2$ 行,...,第 $\textit{cnt}[x]-1$ 行。

最后返回答案列表即可。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -171,7 +171,7 @@ func findMatrix(nums []int) (ans [][]int) {
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = new Array(n + 1).fill(0);
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
Expand All @@ -187,6 +187,33 @@ function findMatrix(nums: number[]): number[][] {
}
```

#### Rust

```rust
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();

for &x in &nums {
cnt[x as usize] += 1;
}

for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ It can be shown that we cannot have less than 3 rows in a valid array.</pre>

### Solution 1: Array or Hash Table

We use an array or hash table $cnt$ to count the number of occurrences of each element in the array $nums$.
We first use an array or hash table $\textit{cnt}$ to count the frequency of each element in the array $\textit{nums}$.

Then we traverse the $cnt$ array, add $x$ to the $0$th row, the $1$st row, the $2$nd row, ..., the ($cnt[x]-1$)th row of the answer list.
Then we iterate through $\textit{cnt}$. For each element $x$, we add it to the 0th row, 1st row, 2nd row, ..., and $(cnt[x]-1)$th row of the answer list.

Finally, return the answer list.
Finally, we return the answer list.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -171,7 +171,7 @@ func findMatrix(nums []int) (ans [][]int) {
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = new Array(n + 1).fill(0);
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
Expand All @@ -187,6 +187,33 @@ function findMatrix(nums: number[]): number[][] {
}
```

#### Rust

```rust
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();

for &x in &nums {
cnt[x as usize] += 1;
}

for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();

for &x in &nums {
cnt[x as usize] += 1;
}

for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = new Array(n + 1).fill(0);
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<strong>输入:</strong>nums = [1,10,3,4,19]
<strong>输出:</strong>133
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
可以证明不存在值大于 133 的有序下标三元组。
可以证明不存在值大于 133 的有序下标三元组。
</pre>

<p><strong class="example">示例 3:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<strong>Input:</strong> nums = [12,6,1,2,7]
<strong>Output:</strong> 77
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
</pre>

<p><strong class="example">Example 2:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<strong>输入:</strong>nums = [1,10,3,4,19]
<strong>输出:</strong>133
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
可以证明不存在值大于 133 的有序下标三元组。
可以证明不存在值大于 133 的有序下标三元组。
</pre>

<p><strong class="example">示例 3:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<strong>Input:</strong> nums = [12,6,1,2,7]
<strong>Output:</strong> 77
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
</pre>

<p><strong class="example">Example 2:</strong></p>
Expand Down
Loading