From 4e0feba04dcb8d63678c55690756d786e01bbbb1 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 27 Feb 2025 14:47:19 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0888 No.0888.Fair Candy Swap --- .../0800-0899/0888.Fair Candy Swap/README.md | 62 ++++++++++++++----- .../0888.Fair Candy Swap/README_EN.md | 62 ++++++++++++++----- .../0888.Fair Candy Swap/Solution.cpp | 8 +-- .../0888.Fair Candy Swap/Solution.go | 18 ++++++ .../0888.Fair Candy Swap/Solution.java | 8 +-- .../0888.Fair Candy Swap/Solution.py | 5 +- .../0888.Fair Candy Swap/Solution.ts | 16 ++--- 7 files changed, 127 insertions(+), 52 deletions(-) create mode 100644 solution/0800-0899/0888.Fair Candy Swap/Solution.go diff --git a/solution/0800-0899/0888.Fair Candy Swap/README.md b/solution/0800-0899/0888.Fair Candy Swap/README.md index fa6edaf737b7b..2ebda14111a8c 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/README.md +++ b/solution/0800-0899/0888.Fair Candy Swap/README.md @@ -72,7 +72,11 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们可以先计算出爱丽丝和鲍勃的糖果总数之差,除以二得到需要交换的糖果数之差 $\textit{diff}$,用一个哈希表 $\textit{s}$ 存储鲍勃的糖果盒中的糖果数,然后遍历爱丽丝的糖果盒,对于每个糖果数 $\textit{a}$,我们判断 $\textit{a} - \textit{diff}$ 是否在哈希表 $\textit{s}$ 中,如果存在,说明找到了一组答案,返回即可。 + +时间复杂度 $O(m + n)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是爱丽丝和鲍勃的糖果盒的数量。 @@ -84,9 +88,8 @@ class Solution: diff = (sum(aliceSizes) - sum(bobSizes)) >> 1 s = set(bobSizes) for a in aliceSizes: - target = a - diff - if target in s: - return [a, target] + if (b := (a - diff)) in s: + return [a, b] ``` #### Java @@ -105,9 +108,9 @@ class Solution { } int diff = (s1 - s2) >> 1; for (int a : aliceSizes) { - int target = a - diff; - if (s.contains(target)) { - return new int[] {a, target}; + int b = a - diff; + if (s.contains(b)) { + return new int[] {a, b}; } } return null; @@ -127,9 +130,9 @@ public: unordered_set s(bobSizes.begin(), bobSizes.end()); vector ans; for (int& a : aliceSizes) { - int target = a - diff; - if (s.count(target)) { - ans = vector{a, target}; + int b = a - diff; + if (s.count(b)) { + ans = vector{a, b}; break; } } @@ -138,19 +141,44 @@ public: }; ``` +#### Go + +```go +func fairCandySwap(aliceSizes []int, bobSizes []int) []int { + s1, s2 := 0, 0 + s := map[int]bool{} + for _, a := range aliceSizes { + s1 += a + } + for _, b := range bobSizes { + s2 += b + s[b] = true + } + diff := (s1 - s2) / 2 + for _, a := range aliceSizes { + if b := a - diff; s[b] { + return []int{a, b} + } + } + return nil +} +``` + #### TypeScript ```ts function fairCandySwap(aliceSizes: number[], bobSizes: number[]): number[] { - let s1 = aliceSizes.reduce((a, c) => a + c, 0); - let s2 = bobSizes.reduce((a, c) => a + c, 0); - let diff = (s1 - s2) >> 1; - for (let num of aliceSizes) { - let target = num - diff; - if (bobSizes.includes(target)) { - return [num, target]; + const s1 = aliceSizes.reduce((acc, cur) => acc + cur, 0); + const s2 = bobSizes.reduce((acc, cur) => acc + cur, 0); + const diff = (s1 - s2) >> 1; + const s = new Set(bobSizes); + for (const a of aliceSizes) { + const b = a - diff; + if (s.has(b)) { + return [a, b]; } } + return []; } ``` diff --git a/solution/0800-0899/0888.Fair Candy Swap/README_EN.md b/solution/0800-0899/0888.Fair Candy Swap/README_EN.md index b0b81f5669670..5f67869a9877f 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/README_EN.md +++ b/solution/0800-0899/0888.Fair Candy Swap/README_EN.md @@ -63,7 +63,11 @@ tags: -### Solution 1 +### Solution 1: Hash Table + +We can first calculate the difference in the total number of candies between Alice and Bob, divide it by two to get the difference in the number of candies to be exchanged $\textit{diff}$, and use a hash table $\textit{s}$ to store the number of candies in Bob's candy boxes. Then, we traverse Alice's candy boxes, and for each candy count $\textit{a}$, we check if $\textit{a} - \textit{diff}$ is in the hash table $\textit{s}$. If it exists, it means we have found a valid answer, and we return it. + +The time complexity is $O(m + n)$, and the space complexity is $O(n)$. Where $m$ and $n$ are the number of candy boxes Alice and Bob have, respectively. @@ -75,9 +79,8 @@ class Solution: diff = (sum(aliceSizes) - sum(bobSizes)) >> 1 s = set(bobSizes) for a in aliceSizes: - target = a - diff - if target in s: - return [a, target] + if (b := (a - diff)) in s: + return [a, b] ``` #### Java @@ -96,9 +99,9 @@ class Solution { } int diff = (s1 - s2) >> 1; for (int a : aliceSizes) { - int target = a - diff; - if (s.contains(target)) { - return new int[] {a, target}; + int b = a - diff; + if (s.contains(b)) { + return new int[] {a, b}; } } return null; @@ -118,9 +121,9 @@ public: unordered_set s(bobSizes.begin(), bobSizes.end()); vector ans; for (int& a : aliceSizes) { - int target = a - diff; - if (s.count(target)) { - ans = vector{a, target}; + int b = a - diff; + if (s.count(b)) { + ans = vector{a, b}; break; } } @@ -129,19 +132,44 @@ public: }; ``` +#### Go + +```go +func fairCandySwap(aliceSizes []int, bobSizes []int) []int { + s1, s2 := 0, 0 + s := map[int]bool{} + for _, a := range aliceSizes { + s1 += a + } + for _, b := range bobSizes { + s2 += b + s[b] = true + } + diff := (s1 - s2) / 2 + for _, a := range aliceSizes { + if b := a - diff; s[b] { + return []int{a, b} + } + } + return nil +} +``` + #### TypeScript ```ts function fairCandySwap(aliceSizes: number[], bobSizes: number[]): number[] { - let s1 = aliceSizes.reduce((a, c) => a + c, 0); - let s2 = bobSizes.reduce((a, c) => a + c, 0); - let diff = (s1 - s2) >> 1; - for (let num of aliceSizes) { - let target = num - diff; - if (bobSizes.includes(target)) { - return [num, target]; + const s1 = aliceSizes.reduce((acc, cur) => acc + cur, 0); + const s2 = bobSizes.reduce((acc, cur) => acc + cur, 0); + const diff = (s1 - s2) >> 1; + const s = new Set(bobSizes); + for (const a of aliceSizes) { + const b = a - diff; + if (s.has(b)) { + return [a, b]; } } + return []; } ``` diff --git a/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp b/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp index 082af60dfc6cb..c24719f97de1a 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp +++ b/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp @@ -7,12 +7,12 @@ class Solution { unordered_set s(bobSizes.begin(), bobSizes.end()); vector ans; for (int& a : aliceSizes) { - int target = a - diff; - if (s.count(target)) { - ans = vector{a, target}; + int b = a - diff; + if (s.count(b)) { + ans = vector{a, b}; break; } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0800-0899/0888.Fair Candy Swap/Solution.go b/solution/0800-0899/0888.Fair Candy Swap/Solution.go new file mode 100644 index 0000000000000..b13ff54df489b --- /dev/null +++ b/solution/0800-0899/0888.Fair Candy Swap/Solution.go @@ -0,0 +1,18 @@ +func fairCandySwap(aliceSizes []int, bobSizes []int) []int { + s1, s2 := 0, 0 + s := map[int]bool{} + for _, a := range aliceSizes { + s1 += a + } + for _, b := range bobSizes { + s2 += b + s[b] = true + } + diff := (s1 - s2) / 2 + for _, a := range aliceSizes { + if b := a - diff; s[b] { + return []int{a, b} + } + } + return nil +} diff --git a/solution/0800-0899/0888.Fair Candy Swap/Solution.java b/solution/0800-0899/0888.Fair Candy Swap/Solution.java index 006227ab6a108..ae2e0a30d0c83 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/Solution.java +++ b/solution/0800-0899/0888.Fair Candy Swap/Solution.java @@ -11,11 +11,11 @@ public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) { } int diff = (s1 - s2) >> 1; for (int a : aliceSizes) { - int target = a - diff; - if (s.contains(target)) { - return new int[] {a, target}; + int b = a - diff; + if (s.contains(b)) { + return new int[] {a, b}; } } return null; } -} \ No newline at end of file +} diff --git a/solution/0800-0899/0888.Fair Candy Swap/Solution.py b/solution/0800-0899/0888.Fair Candy Swap/Solution.py index 761603c9eb791..d132dab51e0b5 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/Solution.py +++ b/solution/0800-0899/0888.Fair Candy Swap/Solution.py @@ -3,6 +3,5 @@ def fairCandySwap(self, aliceSizes: List[int], bobSizes: List[int]) -> List[int] diff = (sum(aliceSizes) - sum(bobSizes)) >> 1 s = set(bobSizes) for a in aliceSizes: - target = a - diff - if target in s: - return [a, target] + if (b := (a - diff)) in s: + return [a, b] diff --git a/solution/0800-0899/0888.Fair Candy Swap/Solution.ts b/solution/0800-0899/0888.Fair Candy Swap/Solution.ts index 6c542efa59430..bbcd15f3c6991 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/Solution.ts +++ b/solution/0800-0899/0888.Fair Candy Swap/Solution.ts @@ -1,11 +1,13 @@ function fairCandySwap(aliceSizes: number[], bobSizes: number[]): number[] { - let s1 = aliceSizes.reduce((a, c) => a + c, 0); - let s2 = bobSizes.reduce((a, c) => a + c, 0); - let diff = (s1 - s2) >> 1; - for (let num of aliceSizes) { - let target = num - diff; - if (bobSizes.includes(target)) { - return [num, target]; + const s1 = aliceSizes.reduce((acc, cur) => acc + cur, 0); + const s2 = bobSizes.reduce((acc, cur) => acc + cur, 0); + const diff = (s1 - s2) >> 1; + const s = new Set(bobSizes); + for (const a of aliceSizes) { + const b = a - diff; + if (s.has(b)) { + return [a, b]; } } + return []; }