Skip to content

Commit ff1c6d4

Browse files
[L-0] add 575
1 parent 10c083b commit ff1c6d4

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Solution](../master/src/main/java/com/stevesun/solutions/_575.java) | O(nlogn) |O(1) | Easy | Array
2324
|567|[Permutation in String](https://leetcode.com/problems/permutation-in-string/)|[Solution](../master/src/main/java/com/stevesun/solutions/_567.java) | O(max(m,n)) |O(1) | Medium | Sliding Windows, Two Pointers
2425
|566|[Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/)|[Solution](../master/src/main/java/com/stevesun/solutions/_566.java) | O(m*n) |O(1) | Easy |
2526
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/)|[Solution](../master/src/main/java/com/stevesun/solutions/_563.java) | O(n) |O(n) | Easy | Tree Recursion
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 575. Distribute Candies
7+
*
8+
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.
9+
10+
Example 1:
11+
Input: candies = [1,1,2,2,3,3]
12+
Output: 3
13+
Explanation:
14+
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
15+
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
16+
The sister has three different kinds of candies.
17+
Example 2:
18+
Input: candies = [1,1,2,3]
19+
Output: 2
20+
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
21+
The sister has two different kinds of candies, the brother has only one kind of candies.
22+
Note:
23+
24+
The length of the given array is in range [2, 10,000], and will be even.
25+
The number in given array is in range [-100,000, 100,000].
26+
*/
27+
public class _575 {
28+
public int distributeCandies(int[] candies) {
29+
Arrays.sort(candies);
30+
int sisCount = 0;
31+
for (int i = 0; i < candies.length; i++) {
32+
int val = candies[i];
33+
sisCount++;
34+
if (sisCount >= candies.length/2) return candies.length/2;
35+
while (i < candies.length && candies[i] == val) {
36+
i++;
37+
}
38+
i--;
39+
}
40+
return sisCount;
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._575;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 5/6/17.
11+
*/
12+
public class _575Test {
13+
private static _575 test;
14+
private static int expected;
15+
private static int actual;
16+
private static int[] candies;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new _575();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
candies = new int[]{1,1,2,3};
26+
expected = 2;
27+
actual = test.distributeCandies(candies);
28+
assertEquals(expected, actual);
29+
}
30+
31+
@Test
32+
public void test2(){
33+
candies = new int[]{1,1,2,2,3,3};
34+
expected = 3;
35+
actual = test.distributeCandies(candies);
36+
assertEquals(expected, actual);
37+
}
38+
39+
@Test
40+
public void test3(){
41+
candies = new int[]{1000,1,1,1};
42+
expected = 2;
43+
actual = test.distributeCandies(candies);
44+
assertEquals(expected, actual);
45+
}
46+
}

0 commit comments

Comments
 (0)