프로그래머스

기지국 설치 C++

paysmile 2022. 3. 24. 19:30

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

 

코딩테스트 연습 - 기지국 설치

N개의 아파트가 일렬로 쭉 늘어서 있습니다. 이 중에서 일부 아파트 옥상에는 4g 기지국이 설치되어 있습니다. 기술이 발전해 5g 수요가 높아져 4g 기지국을 5g 기지국으로 바꾸려 합니다. 그런데 5

programmers.co.kr

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int solution(int n, vector<int> stations, int w)
{
	int answer = 0;
	int cur = 1;
	int num = 2 * w + 1;

	for (int i = 0; i < stations.size(); i++) {
		int index = stations[i];
		int start = index - w;
		int end = index + w;
		if (end > n) end = n;
		if (start <= 0) start = 1;
		
		int count = 0;
		count += (start - cur);
		if (count > 0) {
			int ans = count / num;
			if (count % num > 0) ans++;
			answer += ans;
		}

		cur = end + 1;
	}

	if (cur <= n) {
		int count = 0;
		count += (n - cur + 1);
		if (count > 0) {
			int ans = count / num;
			if (count % num > 0) ans++;
			answer += ans;
		}
	}

	return answer;
}