상세 컨텐츠

본문 제목

[프로그래머스] 기지국 설치 - unsolved

💯ProblemSolving/문제 풀이-Java

by :부셔져버린개발자 2025. 1. 16. 21:14

본문

       문제 요약        

- 문제

https://school.programmers.co.kr/learn/courses/30/lessons/12979

         아이디어        

인덱스 처리 

인덱스 처리가 세상에서 제일 어렵다 

 

 

         소스코드        

 

class Solution {
    public int solution(int n, int[] stations, int w) {
        int answer = 0;
        
        int apt = 1;
        for(int i = 0; i < stations.length; i++) {
            int start = stations[i] - w;
            int end = stations[i] + w;
            if(apt < start) {
                answer += build(start-apt, w);
            }
            apt = end + 1; // 커버되지 않은 아파트 
        }
        if(apt <= n) {
            answer += build(n-apt+1, w); // n까지의 모든 아파트를 포함해야 하므로 + 1
        }
        return answer;
    }
    static int build(int dist, int w) {
        return dist / (2 * w + 1) + (dist % (2 * w + 1) > 0 ? 1 : 0);
    }
}

 

728x90

관련글 더보기