Skip to content

Commit dd7d9b6

Browse files
refactor 575
1 parent 5ff0d3d commit dd7d9b6

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/main/java/com/fishercoder/solutions/_575.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ There are three different kinds of candies (1, 2 and 3), and two candies for eac
2525
The number in given array is in range [-100,000, 100,000].
2626
*/
2727
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) {
35-
return candies.length / 2;
28+
public static class Solution1 {
29+
public int distributeCandies(int[] candies) {
30+
Arrays.sort(candies);
31+
int sisCount = 0;
32+
for (int i = 0; i < candies.length; i++) {
33+
int val = candies[i];
34+
sisCount++;
35+
if (sisCount >= candies.length / 2) {
36+
return candies.length / 2;
37+
}
38+
while (i < candies.length && candies[i] == val) {
39+
i++;
40+
}
41+
i--;
3642
}
37-
while (i < candies.length && candies[i] == val) {
38-
i++;
39-
}
40-
i--;
43+
return sisCount;
4144
}
42-
return sisCount;
4345
}
4446
}

src/test/java/com/fishercoder/_575Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@
1010
* Created by fishercoder on 5/6/17.
1111
*/
1212
public class _575Test {
13-
private static _575 test;
13+
private static _575.Solution1 solution1;
1414
private static int expected;
1515
private static int actual;
1616
private static int[] candies;
1717

1818
@BeforeClass
1919
public static void setup() {
20-
test = new _575();
20+
solution1 = new _575.Solution1();
2121
}
2222

2323
@Test
2424
public void test1() {
2525
candies = new int[]{1, 1, 2, 3};
2626
expected = 2;
27-
actual = test.distributeCandies(candies);
27+
actual = solution1.distributeCandies(candies);
2828
assertEquals(expected, actual);
2929
}
3030

3131
@Test
3232
public void test2() {
3333
candies = new int[]{1, 1, 2, 2, 3, 3};
3434
expected = 3;
35-
actual = test.distributeCandies(candies);
35+
actual = solution1.distributeCandies(candies);
3636
assertEquals(expected, actual);
3737
}
3838

3939
@Test
4040
public void test3() {
4141
candies = new int[]{1000, 1, 1, 1};
4242
expected = 2;
43-
actual = test.distributeCandies(candies);
43+
actual = solution1.distributeCandies(candies);
4444
assertEquals(expected, actual);
4545
}
4646
}

0 commit comments

Comments
 (0)