public int solution(int n, int[] stations, int w) {
int answer = 0;
int start = 1;
for (int station : stations) {
if (start < station - w) {
answer = calculateStations(start, station - w, w, answer);
}
start = station + w + 1;
}
if (start <= n) {
return calculateStations(start, n + 1, w, answer);
}
return answer;
}
private int calculateStations(int start, int end, int w, int answer) {
int length = end - start;
int coverage = w * 2 + 1;
int count = length / coverage;
if (length % coverage != 0) {
count++;
}
answer += count;
return answer;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/12979