[프로그래머스] 기지국 설치 / JavaScript / Level 3

KimYoungWoong·2022년 12월 22일
0

Programmers

목록 보기
40/60
post-thumbnail

🚩문제 주소


📄풀이

완전 탐색

start가 각 기지국의 전파 전달 범위 내에 있다면 start를 전파 전달 범위의 끝 지점으로 변경합니다.
그리고 확인한 기지국을 shift로 없애줍니다.

전파 전달 범위 밖이라면 start를 전파 도달 거리 w * 2 만큼 더해주고, 정답을 +1 합니다.

조건 확인이 끝났다면 start를 +1 합니다.



👨‍💻코드

function solution(n, stations, w) {
  let answer = 0;
  let start = 0;
  stations = stations.map((v) => v - 1);

  while (start < n) {
    if (start >= stations[0] - w && start <= stations[0] + w) {
      start = stations[0] + w;
      stations.shift();
    } else {
      start += w * 2;
      answer++;
    }
    start++;
  }

  return answer;
}

profile
블로그 이전했습니다!! https://highero.tistory.com

0개의 댓글