Breaking the Records(javascript)

Breaking the Records

javascript


Input Format

  • 1 <= n <= 1000
  • 0 <= scores[i] <= 108
변수
scores number type 1차원 배열

입출력 예

scores return
[10, 5, 20, 20, 4, 5, 2, 25, 1] 2 4

코드

  1. 최대, 최소 점수가 갱신 된 카운트에 대해서 반환
function breakingRecords(scores) {
  let ret = [];
  let min = 0,
    max = 0;
  let minCount = 0,
    maxCount = 0;

  scores.forEach((value, index) => {
    if (!index) {
      min = value;
      max = value;
    } else {
      if (value < min) {
        min = value;
        minCount++;
      } else if (value > max) {
        max = value;
        maxCount++;
      }
    }
  });
  return [maxCount, minCount];
}

출처: hackerrank Algorithms, https://www.hackerrank.com/

© 2021 AnGwangHo, Built with Gatsby