Skip to content

Commit dc54494

Browse files
teresachenecfishercoder1534
authored andcommitted
Added p888 (#55)
1 parent a9e3b8a commit dc54494

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Your ideas/fixes/algorithms are more than welcome!
6969
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | O(n) | O(n) | |Easy| DFS, recursion
7070
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
7171
|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_890.java) | O(n) | O(1) | |Medium|
72+
|888|[Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_888.java) | O(n) | O(1) | |Easy|
7273
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
7374
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
7475
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | O(n) | O(h) | |Easy| DFS, recursion
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* 888. Fair Candy Swap
7+
*
8+
* Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j]
9+
* is the size of the j-th bar of candy that Bob has.
10+
*
11+
* Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the
12+
* same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.)
13+
*
14+
* Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the
15+
* size of the candy bar that Bob must exchange.
16+
*
17+
* If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: A = [1,1], B = [2,2]
24+
* Output: [1,2]
25+
* Example 2:
26+
*
27+
* Input: A = [1,2], B = [2,3]
28+
* Output: [1,2]
29+
* Example 3:
30+
*
31+
* Input: A = [2], B = [1,3]
32+
* Output: [2,3]
33+
* Example 4:
34+
*
35+
* Input: A = [1,2,5], B = [2,4]
36+
* Output: [5,4]
37+
*/
38+
39+
public class _888 {
40+
public static class Solution1 {
41+
public int[] fairCandySwap(int[] A, int[] B) {
42+
int aSum = 0;
43+
int bSum = 0;
44+
int diff = 0;
45+
int [] ans = new int [2];
46+
for(int bar : A)
47+
{
48+
aSum += bar;
49+
}
50+
for(int bar : B)
51+
{
52+
bSum += bar;
53+
}
54+
diff = aSum - bSum;
55+
HashSet<Integer> set = new HashSet<>();
56+
for(int bar : A)
57+
{
58+
set.add(bar);
59+
}
60+
for(int bar : B)
61+
{
62+
if(set.contains(bar + diff/2))
63+
{
64+
ans[0] = bar + diff/2;
65+
ans[1] = bar;
66+
break;
67+
}
68+
}
69+
return ans;
70+
}
71+
}
72+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._888;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _888Test {
10+
private static _888.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _888.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int [] A = {1, 1};
20+
int [] B = {2, 2};
21+
int [] ans = {1, 2};
22+
assertEquals(ans, solution1.fairCandySwap(A, B));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
int [] A = {1, 2};
28+
int [] B = {2, 3};
29+
int [] ans = {1, 2};
30+
assertEquals(ans, solution1.fairCandySwap(A, B));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
int [] A = {2};
36+
int [] B = {1, 3};
37+
int [] ans = {2, 3};
38+
assertEquals(ans, solution1.fairCandySwap(A, B));
39+
}
40+
41+
@Test
42+
public void test4() {
43+
int [] A = {1, 2, 5};
44+
int [] B = {2, 4};
45+
int [] ans = {5, 4};
46+
assertEquals(ans, solution1.fairCandySwap(A, B));
47+
}
48+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy