Skip to content

feat: add solutions to lc problem: No.2206 #3979

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
Jan 21, 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
72 changes: 63 additions & 9 deletions solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ nums 可以划分成 (2, 2) ,(3, 3) 和 (2, 2) ,满足所有要求。

<!-- solution:start -->

### 方法一
### 方法一:计数

根据题目描述,只要数组中每个元素出现的次数都是偶数次,就可以将数组划分成 $n$ 个数对。

因此,我们可以用一个哈希表或者数组 $\textit{cnt}$ 记录每个元素出现的次数,然后遍历 $\textit{cnt}$,如果有任何一个元素出现的次数是奇数次,就返回 $\textit{false}$,否则返回 $\textit{true}$。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -107,11 +113,15 @@ class Solution {
class Solution {
public:
bool divideArray(vector<int>& nums) {
vector<int> cnt(510);
for (int& v : nums) ++cnt[v];
for (int& v : cnt)
if (v % 2)
int cnt[510]{};
for (int x : nums) {
++cnt[x];
}
for (int i = 1; i <= 500; ++i) {
if (cnt[i] % 2) {
return false;
}
}
return true;
}
};
Expand All @@ -121,19 +131,63 @@ public:

```go
func divideArray(nums []int) bool {
cnt := make([]int, 510)
for _, v := range nums {
cnt[v]++
cnt := [510]int{}
for _, x := range nums {
cnt[x]++
}
for _, v := range cnt {
if v%2 == 1 {
if v%2 != 0 {
return false
}
}
return true
}
```

#### 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
use std::collections::HashMap;

impl Solution {
pub fn divide_array(nums: Vec<i32>) -> bool {
let mut cnt = HashMap::new();
for x in nums {
*cnt.entry(x).or_insert(0) += 1;
}
cnt.values().all(|&v| v % 2 == 0)
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {boolean}
*/
var divideArray = function (nums) {
const cnt = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
return Object.values(cnt).every(x => x % 2 === 0);
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
76 changes: 65 additions & 11 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 All @@ -67,7 +67,13 @@ There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

According to the problem description, as long as each element in the array appears an even number of times, the array can be divided into $n$ pairs.

Therefore, we can use a hash table or an array $\textit{cnt}$ to record the number of occurrences of each element, then traverse $\textit{cnt}$. If any element appears an odd number of times, return $\textit{false}$; otherwise, return $\textit{true}$.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -105,11 +111,15 @@ class Solution {
class Solution {
public:
bool divideArray(vector<int>& nums) {
vector<int> cnt(510);
for (int& v : nums) ++cnt[v];
for (int& v : cnt)
if (v % 2)
int cnt[510]{};
for (int x : nums) {
++cnt[x];
}
for (int i = 1; i <= 500; ++i) {
if (cnt[i] % 2) {
return false;
}
}
return true;
}
};
Expand All @@ -119,19 +129,63 @@ public:

```go
func divideArray(nums []int) bool {
cnt := make([]int, 510)
for _, v := range nums {
cnt[v]++
cnt := [510]int{}
for _, x := range nums {
cnt[x]++
}
for _, v := range cnt {
if v%2 == 1 {
if v%2 != 0 {
return false
}
}
return true
}
```

#### 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
use std::collections::HashMap;

impl Solution {
pub fn divide_array(nums: Vec<i32>) -> bool {
let mut cnt = HashMap::new();
for x in nums {
*cnt.entry(x).or_insert(0) += 1;
}
cnt.values().all(|&v| v % 2 == 0)
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {boolean}
*/
var divideArray = function (nums) {
const cnt = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
return Object.values(cnt).every(x => x % 2 === 0);
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class Solution {
public:
bool divideArray(vector<int>& nums) {
vector<int> cnt(510);
for (int& v : nums) ++cnt[v];
for (int& v : cnt)
if (v % 2)
int cnt[510]{};
for (int x : nums) {
++cnt[x];
}
for (int i = 1; i <= 500; ++i) {
if (cnt[i] % 2) {
return false;
}
}
return true;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
func divideArray(nums []int) bool {
cnt := make([]int, 510)
for _, v := range nums {
cnt[v]++
cnt := [510]int{}
for _, x := range nums {
cnt[x]++
}
for _, v := range cnt {
if v%2 == 1 {
if v%2 != 0 {
return false
}
}
return true
}
}
11 changes: 11 additions & 0 deletions solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var divideArray = function (nums) {
const cnt = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
return Object.values(cnt).every(x => x % 2 === 0);
};
11 changes: 11 additions & 0 deletions solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::collections::HashMap;

impl Solution {
pub fn divide_array(nums: Vec<i32>) -> bool {
let mut cnt = HashMap::new();
for x in nums {
*cnt.entry(x).or_insert(0) += 1;
}
cnt.values().all(|&v| v % 2 == 0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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);
}
Loading