[백준] - 19941 햄버거 분배 (node.js)

밀루·2025년 1월 27일
0

BOJ

목록 보기
63/82

문제링크

코드

const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const arr = fs
  .readFileSync(filePath)
  .toString()
  .trim()
  .split("\n")
  .map((line) => line.replace("\r", ""));

const [n, k] = arr[0].split(" ").map(Number);

const input = arr[1].split("");
let cnt = 0;

// 나한테서 가장 먼 햄버거 && 왼쪽부터
for (let i = 0; i < input.length; i++) {
  if (input[i] === "P") { // 사람이면
    const max = i + k; // max, min 정해두고
    const min = i - k;
    for (let j = min; j <= max; j++) { // 그 사이 돌면서
      if (input[j] === "H") { // 햄버거라면
        input[j] = 0; // 없애고 
        cnt++; // cnt ++
        break;
      }
    }
  }
}

console.log(cnt);

참고링크

profile
이밀루의 도전

0개의 댓글