From 9c2d2e69ee4fa4236083282c4b5caf2aa230473b Mon Sep 17 00:00:00 2001 From: rain84 <rainy_sky@mail.ru> Date: Tue, 18 Mar 2025 16:20:51 +0300 Subject: [PATCH] feat: update solutions to lc problem: No.2206 --- .../README.md | 42 +++++++++++------ .../README_EN.md | 46 ++++++++++++------- .../Solution.js | 12 +++-- .../Solution.ts | 12 +++-- 4 files changed, 74 insertions(+), 38 deletions(-) diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md index 69f019780fa1d..d24acdbd92927 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md @@ -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 @@ -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 @@ -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; }; ``` diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md index 7e4e94cfccf86..fc1ded8217a77 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md @@ -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> @@ -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> @@ -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 @@ -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 @@ -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; }; ``` diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js index 6ec8af3eaa8a8..987dc8c17d6fd 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js @@ -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; }; diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts index a6604adfe2ae3..7f8606168a301 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts @@ -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; }