티스토리 뷰

Algorithms

Breaking the Records

seoca 2020. 9. 4. 21:26

 

Solution in JavaScript

const scores = [10, 5, 20, 20, 4, 5, 2, 25, 1];

function breakingRecords(scores) {

    let [max, min] = [scores[0], scores[0]]; //중복배열로 선언
    let [maxCount, minCount] = [0, 0]; 

    for (let i = 1; i < scores.length; i++) {
    
        if (scores[i] > max) { //array로 넣어야지...
            max = scores[i];
            maxCount++
        }
        if (scores[i] < min) {
            min = scores[i];
            minCount++
        }
    }

    return [maxCount, minCount]; //두개 이상의 값 return할 때 array로 선언하고 리턴하기.
}

const result = breakingRecords(scores);
console.log(result);

 

처음에 max와 min을 숫자 0으로 초기화했어서 실패;;; index 0으로 시작하고 i값을 1부터 시작해서 비교!

 

 

 

 

Reference

www.hackerrank.com/challenges/breaking-best-and-worst-records/problem

'Algorithms' 카테고리의 다른 글

Birthday Chocolate  (0) 2020.09.11
Divisible Sum Pairs  (0) 2020.09.05
Apple and Orange  (0) 2020.08.24
Grading Students  (0) 2020.08.22
Time Conversion  (0) 2020.08.22