상세 컨텐츠

본문 제목

기지국 설치 (Summer/Winter Coding(~2018))

프로그래머스 코딩테스트 풀이

by 발발개발 2022. 8. 9. 15:22

본문

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

풀이

class Solution {
    public int solution(int n, int[] stations, int w) {
        int answer = 0;
        int start = 1;

        for (int station : stations) {
            answer += fn(w, start, station - w);
            start = station + w + 1;
        }

        answer += fn(w, start, n + 1);

        return answer;
    }

    private int fn(int w, int start, int end) {
        if (start < end) {
            return (int)Math.ceil((end - start) / (double)(2 * w + 1));
        } else {
            return 0;
        }
    }
}

관련글 더보기

댓글 영역