알고리즘/프로그래머스
[프로그래머스] Summer/Winter Coding(~2018) - 기지국 설치, 파이썬
그레고리력
2021. 4. 1. 22:52
문제
풀이
- 레벨 3
- 예전에 나온 문제들은 확실히 쉬운 편이다. 점점 문제도 길어지고 시간이 걸리는 문제가 나오는 것 같다.
- 컨디션 난조로 집중이 안돼서 그냥 대충 풀었다.
- 좀 더 고민하면 깔끔한 코드를 구현할 수 있을 것이다.
코드
def solution(n, stations, w):
prev = 0
ans = 0
for i, station in enumerate(stations) :
pos = station - w - prev - 1
prev = station + w
num = 2*w + 1
if pos % num == 0 :
ans += pos//num
else :
ans += pos//num + 1
if i == len(stations) - 1 and station + w < n :
pos = n - (station + w)
if pos % num == 0 :
ans += pos//num
else :
ans += pos//num + 1
return ans