HackerRank/Algorithm

[HackerRank/Algorithms] Warmup - Birthday Cake Candles

개발자_조이킴 2022. 8. 22. 22:35

Problem. Warmup - Birthday Cake Candles


Link.

https://www.hackerrank.com/challenges/birthday-cake-candles/problem?isFullScreen=true

 

Plus Minus | HackerRank

Calculate the fraction of positive, negative and zero values in an array.

www.hackerrank.com


Description.

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.

 


Key Point. 


My Answer. 

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the 'birthdayCakeCandles' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts INTEGER_ARRAY candles as parameter.
 */

function birthdayCakeCandles(candles) {
    // find max value from array of candles
    let max = Math.max(...candles);
    
    // initialize count 0 for tallest of the candles
    let cnt = 0;
    
    for(let i = 0; i < candles.length; i++) {
        if(candles[i] === max)
            cnt++;
    }
    
    return cnt;

}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const candlesCount = parseInt(readLine().trim(), 10);

    const candles = readLine().replace(/\s+$/g, '').split(' ').map(candlesTemp => parseInt(candlesTemp, 10));

    const result = birthdayCakeCandles(candles);

    ws.write(result + '\n');

    ws.end();
}

References.