알고리즘/백준

[백준] 20438번: 출석체크 (JavaScript, NodeJS)

정현수 2022. 6. 15. 16:20
반응형

 

정답 풀이

const path = process.platform === "linux" ? "/dev/stdin" : "input.txt"; // 리눅스로 테스트할 땐 따로 설정해주어야 합니다.

const input = require("fs")
  .readFileSync(path)
  .toString()
  .trim()
  .split("\n")
  .map(i => i.split(' ').map(Number));

function solution(input) {
  /* input 정리 */
  const [N, K, Q, M] = input[0];
  const sleeps = input[1];
  const students = input[2].filter(student => !sleeps.includes(student));

  /* 문제 풀이 */
  const preSum = [0, 0, 0];
  
  for (let i = 3; i <= N + 2; i++) {
    preSum[i] = preSum[i - 1];
    
    if (sleeps.includes(i)) continue;
    
    for (const student of students) {
      if (i % student === 0) {
        preSum[i] += 1;
        break;
      }
    }
  }

  let answer = '';
  for (let i = 3; i < M + 3; i++) {
    const [S, E] = input[i];
    answer += E - S + 1 - (preSum[E] - preSum[S - 1]) + '\n';
  }

  console.log(answer.substring(0, answer.length - 1));
}

solution(input);

 

반응형