Skip to content

Commit 30f7e65

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Warm up: Birthday Cake Candles solved ✓
1 parent 30641c0 commit 30f7e65

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ae.hackerrank.warmup;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Birthday Cake Candles.
7+
*
8+
* @link Problem definition [[docs/hackerrank/warmup/solve_me_first.md]]
9+
*/
10+
public class BirthdayCakeCandles {
11+
12+
private BirthdayCakeCandles() {
13+
}
14+
15+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
16+
17+
/**
18+
* birthdayCakeCandles.
19+
*/
20+
public static int birthdayCakeCandles(List<Integer> candles) {
21+
if (candles == null || candles.isEmpty()) {
22+
throw new IllegalArgumentException("Parameter cannot be empty");
23+
}
24+
25+
int counter = 0;
26+
int maximum = candles.get(0);
27+
28+
for (Integer element : candles) {
29+
if (element > maximum) {
30+
maximum = element;
31+
counter = 1;
32+
} else {
33+
if (element == maximum) {
34+
counter += 1;
35+
}
36+
}
37+
}
38+
39+
return counter;
40+
}
41+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ae.hackerrank.warmup;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.TestInstance;
12+
import org.junit.jupiter.api.TestInstance.Lifecycle;
13+
14+
15+
@TestInstance(Lifecycle.PER_CLASS)
16+
class BirthdayCakeCandlesTest {
17+
public class BirthdayCakeCandlesTestCase {
18+
public List<Integer> candles;
19+
public Integer expected;
20+
21+
public BirthdayCakeCandlesTestCase(List<Integer> _candles, Integer _expected) {
22+
this.candles = _candles;
23+
this.expected = _expected;
24+
}
25+
}
26+
27+
public List<BirthdayCakeCandlesTestCase> testCases;
28+
29+
@BeforeAll
30+
public void setup() {
31+
this.testCases = Arrays.asList(
32+
new BirthdayCakeCandlesTestCase(
33+
Arrays.asList(3, 2, 1, 3),
34+
2
35+
),
36+
new BirthdayCakeCandlesTestCase(
37+
Arrays.asList(1, 2, 3, 3),
38+
2
39+
)
40+
);
41+
}
42+
43+
@Test void birthdayCakeCandles() {
44+
for (BirthdayCakeCandlesTestCase testCase : this.testCases) {
45+
Integer solutionFound = BirthdayCakeCandles.birthdayCakeCandles(testCase.candles);
46+
47+
assertEquals(testCase.expected, solutionFound,
48+
String.format("%s(%s) answer must be: %d",
49+
"CompareTriplets.compareTriplets",
50+
testCase.candles.toString(),
51+
testCase.expected)
52+
);
53+
}
54+
}
55+
56+
@Test
57+
void testMiniMaxSumNullInput() {
58+
59+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
60+
BirthdayCakeCandles.birthdayCakeCandles(null);
61+
});
62+
63+
String expectedMessage = "Parameter cannot be empty";
64+
String actualMessage = exception.getMessage();
65+
66+
assertTrue(actualMessage.contains(expectedMessage));
67+
}
68+
69+
@Test
70+
void testMiniMaxSumEmptyInput() {
71+
List<Integer> input = Arrays.asList();
72+
73+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
74+
BirthdayCakeCandles.birthdayCakeCandles(input);
75+
});
76+
77+
String expectedMessage = "Parameter cannot be empty";
78+
String actualMessage = exception.getMessage();
79+
80+
assertTrue(actualMessage.contains(expectedMessage));
81+
}
82+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
You are in charge of the cake for a child's birthday.
7+
You have decided the cake will have one candle for each year of their total
8+
age. They will only be able to blow out the tallest of the candles.
9+
Count how many candles are tallest.
10+
11+
## Example
12+
13+
The maximum height candles are 4 units high.
14+
There are 2 of them, so return 2.
15+
16+
## Function Description
17+
18+
Complete the function birthdayCakeCandles in the editor below.
19+
birthdayCakeCandles has the following parameter(s):
20+
21+
- int candles[n]: the candle heights
22+
23+
## Returns
24+
25+
- int: the number of candles that are tallest
26+
27+
## Input Format
28+
29+
The first line contains a single integer, n, the size of candles[].
30+
The second line contains space-separated integers, where each integer i describes
31+
the height of candles[i].
32+
33+
## Constraints
34+
35+
$ 1 \leq n \leq 10^5 $
36+
$ 1 \leq candles[i] \leq 10^7 $
37+
38+
## Sample Input 0
39+
40+
```text
41+
4
42+
3 2 1 3
43+
```
44+
45+
## Sample Output 0
46+
47+
```text
48+
2
49+
```
50+
51+
## Explanation 0
52+
53+
Candle heights are $ [3, 2, 1, 3] $. The tallest candles are $ 3 $ units, and there
54+
are $ 2 $ of them.

0 commit comments

Comments
 (0)