백준14467 / 소가 길을 건너간 이유 / JS

Jihoo·2021년 9월 10일
0

Algorithm

목록 보기
7/16

https://www.acmicpc.net/problem/14467

Case1.
8
3 1
3 0
6 0
2 1
4 1
3 0
4 0
3 1

예제를 이해하고 코드로 옮겨봅시다.

Case1.
1 2 3 4 5 6 7 8 9 10
    1 => just record
    0 => answer++
          0
  1
      1
    0 => continue
      0 => answer++
    1 => answer++

⬇️

const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = require('fs')
  .readFileSync(filePath)
  .toString()
  .trim()
  .split('\n');
const N = input[0];
let check = Array(11).fill(-1);
let answer = 0;
for (let i = 1; i <= N; i++) {
  const tmp = input[i].split(' ');
  const cow = Number(tmp[0]);
  const record = Number(tmp[1]);
  if (check[cow] === -1) {
    check[cow] = record;
  } else if (check[cow] !== record) {
    check[cow] = record;
    answer += 1;
  }
}
console.log(answer);

0개의 댓글