HR - Breaking the Records

Goody·2021년 2월 3일
0

알고리즘

목록 보기
26/122

문제

Maria plays college basketball and wants to go pro.
Each season she maintains a record of her play.
She tabulates the number of times she breaks her season record for most points
and least points in a game.
Points scored in the first game establish her record for the season, and she begins counting from there.

She broke her careerhigh 2 times, and careerlow 4 times.
Thus return 2 4.

예시

INPUT

10
3 4 21 36 10 28 35 5 24 42

OUTPUT

4 0

풀이

  • 최고, 최저점을 기록했을 때 마다 변수에 저장한다.
  • 새로운 기록이 나올 때 마다 기존 최고, 최저점과 비교해서 갱신하고 카운트를 늘린다.

코드

function breakingRecords(scores) {
    let careerLow = scores[0];
    let careerHigh = scores[0];
    let recordsCounter = [0, 0];

    scores.forEach((score) => {
        if(score > careerHigh) {
            careerHigh = score;
            recordsCounter[0]++;
        }

        if(score < careerLow) {
            careerLow = score;
            recordsCounter[1]++;
        }
    });

   return [recordsCounter[0], recordsCounter[1]];
}

0개의 댓글