본문 바로가기

Programmers/효율성 시간초과 모음

[프로그래머스] 기지국 설치

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
typedef pair<intint> pp;
 
//효율성 테스트 시간 초과 
int solution(int n, vector<int> stations, int w)
{
    int answer = 0;
    
    vector<int> apart(n, 0);
    //인덱스, 설치 됐는지 아닌지 확인 
    
    for (int i = 0; i < stations.size(); i++) {
        apart[stations[i]-1]++;
        for (int  j = 1; j <= w; j++ ) {
            if (stations[i]-1 + j < n)
                apart[stations[i]-1 + j]++;
            if (stations[i] -1- j >= 0)
                 apart[stations[i]-1 - j]++;
        }
    }
    
    for (int i = 0; i < n;) {
      //자신을 포함해서 w만큼의 양 옆의 칸에 전파를 전달할 수 있다. 
        if (apart[i]) {
             i++//기지국이 이미 있음 (다음 칸을 본다)
        }
        else {
            answer++;
            i += w*2+1;
        }
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter