본문 바로가기

Research/Coding Test24

HackerRank_Birthday Cake Candles Problem Intuition 요소를 키로 개수를 값으로 하는 해시테이블을 만든 뒤 가장 큰 수의 값을 리턴한다. Code function birthdayCakeCandles(candles) { // intuition : 요소를 키로 개수를 값으로 하는 해시테이블을 만든 뒤 가장 큰 수의 값을 리턴하면 되지 않을까 const dict = {} candles.forEach(el => { if (!dict[el]) { dict[el] = 1 } else { dict[el]++; } }) const keys = Object.keys(dict); return dict[keys[keys.length-1]] } const items = [4, 4, 1, 3] console.log(birthdayCakeCandle.. 2023. 4. 5.
HackerRank_Staircase Problem Approach Just simple pyramid making problem. Fill the gap with new array and add '#'. Code function staircase(n) { for(let i = 1; i 2023. 4. 2.
HackerRank_Plus Minus Problem Code function plusMinus(arr) { const totalLen = arr.length; let positives = 0; let negatives = 0; let zeros = 0; arr.forEach(el => { if(el > 0) positives++; else if(el < 0) negatives++; else zeros++; }) positives /= totalLen negatives /= totalLen zeros /= totalLen positives = Number.parseFloat(positives).toFixed(6); negatives = Number.parseFloat(negatives).toFixed(6); zeros = Number.pars.. 2023. 4. 2.
HackerRank_Diagonal Difference Problem https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true&h_r=next-challenge&h_v=zen Approach So absolute difference can be calculated like this sum(left to right) - sum(right to left). I was thinking maybe I can calculate both sides at the same time. But I just found easier way by doing loop two times. Solution function diagonalDifference(arr) { const length = .. 2023. 3. 30.