231213 기지국 설치

Jongleee·2023년 12월 13일
0

TIL

목록 보기
441/576
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

0개의 댓글