Skip to content

feat: add solutions to lc problem: No.0888 #4115

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
Feb 27, 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
62 changes: 45 additions & 17 deletions solution/0800-0899/0888.Fair Candy Swap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表

我们可以先计算出爱丽丝和鲍勃的糖果总数之差,除以二得到需要交换的糖果数之差 $\textit{diff}$,用一个哈希表 $\textit{s}$ 存储鲍勃的糖果盒中的糖果数,然后遍历爱丽丝的糖果盒,对于每个糖果数 $\textit{a}$,我们判断 $\textit{a} - \textit{diff}$ 是否在哈希表 $\textit{s}$ 中,如果存在,说明找到了一组答案,返回即可。

时间复杂度 $O(m + n)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是爱丽丝和鲍勃的糖果盒的数量。

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -127,9 +130,9 @@ public:
unordered_set<int> s(bobSizes.begin(), bobSizes.end());
vector<int> ans;
for (int& a : aliceSizes) {
int target = a - diff;
if (s.count(target)) {
ans = vector<int>{a, target};
int b = a - diff;
if (s.count(b)) {
ans = vector<int>{a, b};
break;
}
}
Expand All @@ -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 [];
}
```

Expand Down
62 changes: 45 additions & 17 deletions solution/0800-0899/0888.Fair Candy Swap/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ tags:

<!-- solution:start -->

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

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -118,9 +121,9 @@ public:
unordered_set<int> s(bobSizes.begin(), bobSizes.end());
vector<int> ans;
for (int& a : aliceSizes) {
int target = a - diff;
if (s.count(target)) {
ans = vector<int>{a, target};
int b = a - diff;
if (s.count(b)) {
ans = vector<int>{a, b};
break;
}
}
Expand All @@ -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 [];
}
```

Expand Down
8 changes: 4 additions & 4 deletions solution/0800-0899/0888.Fair Candy Swap/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class Solution {
unordered_set<int> s(bobSizes.begin(), bobSizes.end());
vector<int> ans;
for (int& a : aliceSizes) {
int target = a - diff;
if (s.count(target)) {
ans = vector<int>{a, target};
int b = a - diff;
if (s.count(b)) {
ans = vector<int>{a, b};
break;
}
}
return ans;
}
};
};
18 changes: 18 additions & 0 deletions solution/0800-0899/0888.Fair Candy Swap/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions solution/0800-0899/0888.Fair Candy Swap/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
5 changes: 2 additions & 3 deletions solution/0800-0899/0888.Fair Candy Swap/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
16 changes: 9 additions & 7 deletions solution/0800-0899/0888.Fair Candy Swap/Solution.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}