문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/12979
코딩테스트 연습 - 기지국 설치
N개의 아파트가 일렬로 쭉 늘어서 있습니다. 이 중에서 일부 아파트 옥상에는 4g 기지국이 설치되어 있습니다. 기술이 발전해 5g 수요가 높아져 4g 기지국을 5g 기지국으로 바꾸려 합니다. 그런데 5
programmers.co.kr
알고리즘은 아래와 같습니다.
1. 전파가 터지지 않는 범위들을 구합니다.
2. 전파가 터지지 않는 범위 내 설치할 기지국 갯수는 (전파 터지지 않는 범위 / 기지국 범위)의 올림입니다.
2.1 기지국이 소수점 개수로 있을 수 없기 때문입니다.
3. 2번 공식을 통해 1번에서 구한 범위들 내 기지국 개수를 모두 파악한 뒤 반환해줍니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int solution(int n, vector<int> stations, int w) | |
{ | |
vector<pair<int, int>> notReachedRanges; | |
int start = 1; | |
for (int station : stations) | |
{ | |
if (start < station - w) | |
{ | |
notReachedRanges.push_back({ start, station - w - 1 }); | |
} | |
start = station + w + 1; | |
} | |
if (start <= n) | |
{ | |
notReachedRanges.push_back({ start, n }); | |
} | |
int answer = 0; | |
int radioWave = 2 * w + 1; | |
for (pair<int, int> range : notReachedRanges) | |
{ | |
int len = range.second - range.first + 1; | |
if (len <= radioWave) | |
{ | |
answer++; | |
} | |
else | |
{ | |
answer += len / radioWave + (len % radioWave != 0); | |
} | |
} | |
return answer; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 스티커 모으기(2) (0) | 2021.10.01 |
---|---|
[Programmers] 숫자 게임 (0) | 2021.10.01 |
[Programmers] 방문 길이 (0) | 2021.10.01 |
[Programmers] 스킬트리 (0) | 2021.10.01 |
[Programmers] 점프와 순간 이동 (0) | 2021.10.01 |