개발일기 [Python 파이썬]

[Python Algorithm] Installation of Base Station

neullo 2024. 4. 9. 10:30

 

Installation of Base Station

Suppose that N apartments are placed in a row. In some apartments among these, 4g base station is installed at the rooftop. As demand for 5g increases due to the advancement of technology, you want to replace 4g base station with 5g base station. But, since the coverage of 5g base station is smaller than 4g base station, some apartments cannot receive signal after replacing 4g base station with 5g base station.

For example, assume that 11 apartments are places in a row, and 4g base station is installed at the rooftop of [4, 11]-th apartment. If these 4g base stations are replaced with 5g base station with coverage of length 1, signal is not delivered to all apartments. (When the coverage is length W, signal is delivered within the range of W from both side of apartment where base station is installed.)

At this point, you want to deliver the signal to all apartments by installing the minimum number of base stations. In the above example, installing at least 3 base stations is required to deliver signal to all apartments.

Given the number of apartments N, an 1-dimensional array stations containing the position of apartments where 4g base station is installed, and the coverage of base station W as parameters, write a function solution to return the minimum number of base stations to be installed additionally to deliver the signal for all apartments.

 

 

At the beginning, apartments at the position of 1, 2, 6, 7, 8, and 9 cannot receive the signal.

 

 

 

If the 5g base station is installed at the apartments of position 1, 7, and 9, signal is delivered to all apartments. In the case of installing 5g base station at more than 3 apartments, signal is delivered to all apartments.

 

 


def solution(N, stations, W):
    answer = 0  # 설치할 기지국의 수를 나타내는 변수를 초기화
    
    start = 1  # 아파트의 시작 위치를 나타내는 변수를 초기화
    station_location = 0  # 현재 기지국이 설치된 위치를 나타내는 변수를 초기화

    while start <= N:  # 아파트의 총 수보다 작거나 같은 동안에만 반복
    
        if station_location < len(stations) and start >= stations[station_location] - W:  # 현재 아파트가 기지국의 커버리지 내에 있는 경우
            start = stations[station_location] + W + 1  # 다음 기지국 커버리지 바깥으로 이동
            station_location += 1  # 다음 기지국으로 이동
            
        else:  # 현재 아파트가 커버되지 않은 경우
            start += 2 * W + 1  # 현재 기지국을 기준으로 커버리지만큼 이동
            answer += 1  # 새로운 기지국 설치
            
    return answer

 

 

def solution(N, stations, W):

def solution(N, stations, W):: This line defines the function solution which takes three parameters: N (the total number of apartments), stations (a list containing the positions of existing 4G base stations), and W (the coverage of each base station).

 

count=0

count = 0: This initializes a variable count to keep track of the number of additional base stations needed to cover all apartments. 변수를 초기화하여 모든 아파트를 커버하기 위해 추가로 설치해야 하는 기지국의 수 [Stations] 초기화 [질문에 대한 답]

 

start = 1

start = 1: This initializes a variable start to keep track of the current apartment being considered. We start from apartment 1. 아파트를 추적하기 위한 start 변수를 초기화. 1호 아파트부터 시작 [N]

 

i = 0

 i = 0: This initializes a variable i to keep track of the index of the next base station in the stations list.

i는 stations 리스트에서 다음으로 설치될 기지국의 위치를 가리키는 인덱스 [i] 이는 [station_location] 로도 쓸수 있다.

 


조건이 사실 [다음 커버되지 않은 아파트로 이동]

While start <= N:
        if i < len(stations) and start >= stations[i] - W:
            start = stations[i] + W + 1
            i += 1

 while start <= N:: This starts a while loop that continues until we cover all apartments up to N.

if i < len(stations) and start >= stations [i] - W:: This condition checks if we still have base stations left (i < len(stations)) and if the current apartment start is covered by the next base station (start >= stations [i] - W). 

  • 아직 설치할 기지국이 남아 있고 (i < len(stations))
  • 현재 아파트 start가 다음 기지국에 의해 커버되어 있는지 (start >= stations[i] - W) 확인
  • 두 조건이 모두 참이면 현재 아파트에 새로운 기지국을 설치할 필요가 없음

If both conditions are true, it means we don't need to install a new base station at start because it's already covered by the existing base station.

 

start = stations[i] + W + 1: If the condition in step 6 is true, this updates the value of start to the position of the next base station plus W + 1, ensuring that we move to the next uncovered apartment after the coverage area of the current base station. 커버되지 않은 아파트로 이동

 

 i += 1: This increments the index i to move to the next base station in the stations list. stations 리스트의 다음 기지국으로 이동

 


조건이 거짓 [현재 아파트 start가 기존 기지국에 커버되지 않았음을 의미-새로운 기지국을 설치]

        else:
            start += 2 * W + 1
            count += 1

else:: If the condition in step 6 is false, it means the current apartment start is not covered by any existing base station. In this case, we need to install a new base station.

 

start += 2 * W + 1: This updates the value of start to move to the next uncovered apartment after installing the new base station. Since the coverage area of the new base station extends W units to the left and W units to the right of its position, we move 2 * W + 1 units ahead to skip the covered area.

  • 커버된 영역을 건너뛰기 위해 커버되지 않은 다음 아파트로 이동
  • 커버리지 영역은 해당 위치의 왼쪽과 오른쪽으로 각각 W 단위로 확장

count += 1: This increments the count variable to indicate that we've installed an additional base station.

 


    return count

return count: After covering all apartments, this line returns the total count of installed base stations needed to cover all apartments.