본문 바로가기
코딩테스트 문제집/Programmers(Lv3)

[Programmers] Lv3: 기지국 설치(12979)

by cogito30 2025. 2. 12.
반응형

문제

- 링크: https://school.programmers.co.kr/learn/courses/30/lessons/12979?language=java

 

풀이

더보기
class Solution {
    public int solution(int n, int[] stations, int w) {
        int answer = 0;
        
        int location = 1;
        int i = 0;
        while (location <= n) {
            // && 조건 순서 주의
            if (i < stations.length && location >= stations[i] - w) {
                location = stations[i] + w + 1;
                i++;
            } else {
                location += 2 * w + 1;
                answer++;
            }
        }

        return answer;
    }
}

 

반응형