Skip to content

Commit 8ac4a26

Browse files
authored
feat: add solutions to lc problem: No.0888 (#4115)
No.0888.Fair Candy Swap
1 parent 65cbd25 commit 8ac4a26

File tree

7 files changed

+127
-52
lines changed

7 files changed

+127
-52
lines changed

solution/0800-0899/0888.Fair Candy Swap/README.md

+45-17
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ tags:
7272

7373
<!-- solution:start -->
7474

75-
### 方法一
75+
### 方法一:哈希表
76+
77+
我们可以先计算出爱丽丝和鲍勃的糖果总数之差,除以二得到需要交换的糖果数之差 $\textit{diff}$,用一个哈希表 $\textit{s}$ 存储鲍勃的糖果盒中的糖果数,然后遍历爱丽丝的糖果盒,对于每个糖果数 $\textit{a}$,我们判断 $\textit{a} - \textit{diff}$ 是否在哈希表 $\textit{s}$ 中,如果存在,说明找到了一组答案,返回即可。
78+
79+
时间复杂度 $O(m + n)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是爱丽丝和鲍勃的糖果盒的数量。
7680

7781
<!-- tabs:start -->
7882

@@ -84,9 +88,8 @@ class Solution:
8488
diff = (sum(aliceSizes) - sum(bobSizes)) >> 1
8589
s = set(bobSizes)
8690
for a in aliceSizes:
87-
target = a - diff
88-
if target in s:
89-
return [a, target]
91+
if (b := (a - diff)) in s:
92+
return [a, b]
9093
```
9194

9295
#### Java
@@ -105,9 +108,9 @@ class Solution {
105108
}
106109
int diff = (s1 - s2) >> 1;
107110
for (int a : aliceSizes) {
108-
int target = a - diff;
109-
if (s.contains(target)) {
110-
return new int[] {a, target};
111+
int b = a - diff;
112+
if (s.contains(b)) {
113+
return new int[] {a, b};
111114
}
112115
}
113116
return null;
@@ -127,9 +130,9 @@ public:
127130
unordered_set<int> s(bobSizes.begin(), bobSizes.end());
128131
vector<int> ans;
129132
for (int& a : aliceSizes) {
130-
int target = a - diff;
131-
if (s.count(target)) {
132-
ans = vector<int>{a, target};
133+
int b = a - diff;
134+
if (s.count(b)) {
135+
ans = vector<int>{a, b};
133136
break;
134137
}
135138
}
@@ -138,19 +141,44 @@ public:
138141
};
139142
```
140143
144+
#### Go
145+
146+
```go
147+
func fairCandySwap(aliceSizes []int, bobSizes []int) []int {
148+
s1, s2 := 0, 0
149+
s := map[int]bool{}
150+
for _, a := range aliceSizes {
151+
s1 += a
152+
}
153+
for _, b := range bobSizes {
154+
s2 += b
155+
s[b] = true
156+
}
157+
diff := (s1 - s2) / 2
158+
for _, a := range aliceSizes {
159+
if b := a - diff; s[b] {
160+
return []int{a, b}
161+
}
162+
}
163+
return nil
164+
}
165+
```
166+
141167
#### TypeScript
142168

143169
```ts
144170
function fairCandySwap(aliceSizes: number[], bobSizes: number[]): number[] {
145-
let s1 = aliceSizes.reduce((a, c) => a + c, 0);
146-
let s2 = bobSizes.reduce((a, c) => a + c, 0);
147-
let diff = (s1 - s2) >> 1;
148-
for (let num of aliceSizes) {
149-
let target = num - diff;
150-
if (bobSizes.includes(target)) {
151-
return [num, target];
171+
const s1 = aliceSizes.reduce((acc, cur) => acc + cur, 0);
172+
const s2 = bobSizes.reduce((acc, cur) => acc + cur, 0);
173+
const diff = (s1 - s2) >> 1;
174+
const s = new Set(bobSizes);
175+
for (const a of aliceSizes) {
176+
const b = a - diff;
177+
if (s.has(b)) {
178+
return [a, b];
152179
}
153180
}
181+
return [];
154182
}
155183
```
156184

solution/0800-0899/0888.Fair Candy Swap/README_EN.md

+45-17
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Hash Table
67+
68+
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.
69+
70+
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.
6771

6872
<!-- tabs:start -->
6973

@@ -75,9 +79,8 @@ class Solution:
7579
diff = (sum(aliceSizes) - sum(bobSizes)) >> 1
7680
s = set(bobSizes)
7781
for a in aliceSizes:
78-
target = a - diff
79-
if target in s:
80-
return [a, target]
82+
if (b := (a - diff)) in s:
83+
return [a, b]
8184
```
8285

8386
#### Java
@@ -96,9 +99,9 @@ class Solution {
9699
}
97100
int diff = (s1 - s2) >> 1;
98101
for (int a : aliceSizes) {
99-
int target = a - diff;
100-
if (s.contains(target)) {
101-
return new int[] {a, target};
102+
int b = a - diff;
103+
if (s.contains(b)) {
104+
return new int[] {a, b};
102105
}
103106
}
104107
return null;
@@ -118,9 +121,9 @@ public:
118121
unordered_set<int> s(bobSizes.begin(), bobSizes.end());
119122
vector<int> ans;
120123
for (int& a : aliceSizes) {
121-
int target = a - diff;
122-
if (s.count(target)) {
123-
ans = vector<int>{a, target};
124+
int b = a - diff;
125+
if (s.count(b)) {
126+
ans = vector<int>{a, b};
124127
break;
125128
}
126129
}
@@ -129,19 +132,44 @@ public:
129132
};
130133
```
131134
135+
#### Go
136+
137+
```go
138+
func fairCandySwap(aliceSizes []int, bobSizes []int) []int {
139+
s1, s2 := 0, 0
140+
s := map[int]bool{}
141+
for _, a := range aliceSizes {
142+
s1 += a
143+
}
144+
for _, b := range bobSizes {
145+
s2 += b
146+
s[b] = true
147+
}
148+
diff := (s1 - s2) / 2
149+
for _, a := range aliceSizes {
150+
if b := a - diff; s[b] {
151+
return []int{a, b}
152+
}
153+
}
154+
return nil
155+
}
156+
```
157+
132158
#### TypeScript
133159

134160
```ts
135161
function fairCandySwap(aliceSizes: number[], bobSizes: number[]): number[] {
136-
let s1 = aliceSizes.reduce((a, c) => a + c, 0);
137-
let s2 = bobSizes.reduce((a, c) => a + c, 0);
138-
let diff = (s1 - s2) >> 1;
139-
for (let num of aliceSizes) {
140-
let target = num - diff;
141-
if (bobSizes.includes(target)) {
142-
return [num, target];
162+
const s1 = aliceSizes.reduce((acc, cur) => acc + cur, 0);
163+
const s2 = bobSizes.reduce((acc, cur) => acc + cur, 0);
164+
const diff = (s1 - s2) >> 1;
165+
const s = new Set(bobSizes);
166+
for (const a of aliceSizes) {
167+
const b = a - diff;
168+
if (s.has(b)) {
169+
return [a, b];
143170
}
144171
}
172+
return [];
145173
}
146174
```
147175

solution/0800-0899/0888.Fair Candy Swap/Solution.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class Solution {
77
unordered_set<int> s(bobSizes.begin(), bobSizes.end());
88
vector<int> ans;
99
for (int& a : aliceSizes) {
10-
int target = a - diff;
11-
if (s.count(target)) {
12-
ans = vector<int>{a, target};
10+
int b = a - diff;
11+
if (s.count(b)) {
12+
ans = vector<int>{a, b};
1313
break;
1414
}
1515
}
1616
return ans;
1717
}
18-
};
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func fairCandySwap(aliceSizes []int, bobSizes []int) []int {
2+
s1, s2 := 0, 0
3+
s := map[int]bool{}
4+
for _, a := range aliceSizes {
5+
s1 += a
6+
}
7+
for _, b := range bobSizes {
8+
s2 += b
9+
s[b] = true
10+
}
11+
diff := (s1 - s2) / 2
12+
for _, a := range aliceSizes {
13+
if b := a - diff; s[b] {
14+
return []int{a, b}
15+
}
16+
}
17+
return nil
18+
}

solution/0800-0899/0888.Fair Candy Swap/Solution.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
1111
}
1212
int diff = (s1 - s2) >> 1;
1313
for (int a : aliceSizes) {
14-
int target = a - diff;
15-
if (s.contains(target)) {
16-
return new int[] {a, target};
14+
int b = a - diff;
15+
if (s.contains(b)) {
16+
return new int[] {a, b};
1717
}
1818
}
1919
return null;
2020
}
21-
}
21+
}

solution/0800-0899/0888.Fair Candy Swap/Solution.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ def fairCandySwap(self, aliceSizes: List[int], bobSizes: List[int]) -> List[int]
33
diff = (sum(aliceSizes) - sum(bobSizes)) >> 1
44
s = set(bobSizes)
55
for a in aliceSizes:
6-
target = a - diff
7-
if target in s:
8-
return [a, target]
6+
if (b := (a - diff)) in s:
7+
return [a, b]
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
function fairCandySwap(aliceSizes: number[], bobSizes: number[]): number[] {
2-
let s1 = aliceSizes.reduce((a, c) => a + c, 0);
3-
let s2 = bobSizes.reduce((a, c) => a + c, 0);
4-
let diff = (s1 - s2) >> 1;
5-
for (let num of aliceSizes) {
6-
let target = num - diff;
7-
if (bobSizes.includes(target)) {
8-
return [num, target];
2+
const s1 = aliceSizes.reduce((acc, cur) => acc + cur, 0);
3+
const s2 = bobSizes.reduce((acc, cur) => acc + cur, 0);
4+
const diff = (s1 - s2) >> 1;
5+
const s = new Set(bobSizes);
6+
for (const a of aliceSizes) {
7+
const b = a - diff;
8+
if (s.has(b)) {
9+
return [a, b];
910
}
1011
}
12+
return [];
1113
}

0 commit comments

Comments
 (0)