From ee541253b89a589a1219ceceaafc8c5f921971a5 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 11 Sep 2024 20:35:52 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20Birthday=20Cake=20?= =?UTF-8?q?Candles=20solved=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../warmup/birthday_cake_candles.md | 54 +++++++++++++++++++ .../warmup/birthday_cake_candles.hpp | 7 +++ .../warmup/birthday_cake_candles.cpp | 27 ++++++++++ .../warmup/birthday_cake_candles.cpp | 26 +++++++++ .../birthday_cake_candles.testcases.json | 4 ++ 5 files changed, 118 insertions(+) create mode 100644 docs/hackerrank/warmup/birthday_cake_candles.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/warmup/birthday_cake_candles.hpp create mode 100644 src/lib/exercises/src/hackerrank/warmup/birthday_cake_candles.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json diff --git a/docs/hackerrank/warmup/birthday_cake_candles.md b/docs/hackerrank/warmup/birthday_cake_candles.md new file mode 100644 index 0000000..8e8b3ef --- /dev/null +++ b/docs/hackerrank/warmup/birthday_cake_candles.md @@ -0,0 +1,54 @@ +# [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles) + +Difficulty: #easy +Category: #warmup + +You are in charge of the cake for a child's birthday. +You have decided the cake will have one candle for each year of their total +age. They will only be able to blow out the tallest of the candles. +Count how many candles are tallest. + +## Example + +The maximum height candles are 4 units high. +There are 2 of them, so return 2. + +## Function Description + +Complete the function birthdayCakeCandles in the editor below. +birthdayCakeCandles has the following parameter(s): + +- int candles[n]: the candle heights + +## Returns + +- int: the number of candles that are tallest + +## Input Format + +The first line contains a single integer, n, the size of candles[]. +The second line contains space-separated integers, where each integer i describes +the height of candles[i]. + +## Constraints + +$ 1 \leq n \leq 10^5 $ +$ 1 \leq candles[i] \leq 10^7 $ + +## Sample Input 0 + +```text +4 +3 2 1 3 +``` + +## Sample Output 0 + +```text +2 +``` + +## Explanation 0 + +Candle heights are $ [3, 2, 1, 3] $. The tallest candles are $ 3 $ units, and there +are $ 2 $ of them. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/birthday_cake_candles.hpp b/src/lib/exercises/include/exercises/hackerrank/warmup/birthday_cake_candles.hpp new file mode 100644 index 0000000..6276330 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/birthday_cake_candles.hpp @@ -0,0 +1,7 @@ +#include + +#pragma once + +namespace hackerrank::warmup { + int birthdayCakeCandles(const std::vector& candles); +} diff --git a/src/lib/exercises/src/hackerrank/warmup/birthday_cake_candles.cpp b/src/lib/exercises/src/hackerrank/warmup/birthday_cake_candles.cpp new file mode 100644 index 0000000..cac8b69 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/birthday_cake_candles.cpp @@ -0,0 +1,27 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/birthday_cake_candles.md]] + */ + +#include +#include + +namespace hackerrank::warmup { + int birthdayCakeCandles(const std::vector& candles) { + int counter = 0; + int maximum = candles[0]; + + for (const int& element : candles) { + if (element == maximum) { + counter += 1; + } + if (element > maximum) { + maximum = element; + counter = 1; + } + } + + return counter; + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.cpp b/src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.cpp new file mode 100644 index 0000000..0a89092 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.cpp @@ -0,0 +1,26 @@ +#include + +#include +#include +#include + +#include +#include +#include +using json = nlohmann::json; + +TEST_CASE("birthdayCakeCandles JSON Test Cases", "[warmup]") +{ + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json"; + + INFO("birthdayCakeCandles JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + long result = hackerrank::warmup::birthdayCakeCandles(testcase["input"]); + CHECK(result == testcase["expected"]); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json b/src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json new file mode 100644 index 0000000..86fa2d2 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/birthday_cake_candles.testcases.json @@ -0,0 +1,4 @@ +[ + {"input": [3, 2, 1, 3], "expected": 2}, + {"input": [1, 2, 3, 3], "expected": 2} +]