ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

 

๐ŸŒ Summer/Winter Coding(~2018)

 

๋ฌธ์ œ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ธฐ์ง€๊ตญ ์„ค์น˜

N๊ฐœ์˜ ์•„ํŒŒํŠธ๊ฐ€ ์ผ๋ ฌ๋กœ ์ญ‰ ๋Š˜์–ด์„œ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ์ผ๋ถ€ ์•„ํŒŒํŠธ ์˜ฅ์ƒ์—๋Š” 4g ๊ธฐ์ง€๊ตญ์ด ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์ˆ ์ด ๋ฐœ์ „ํ•ด 5g ์ˆ˜์š”๊ฐ€ ๋†’์•„์ ธ 4g ๊ธฐ์ง€๊ตญ์„ 5g ๊ธฐ์ง€๊ตญ์œผ๋กœ ๋ฐ”๊พธ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ 5

programmers.co.kr

 

 

thinking

์ „ํŒŒ๊ฐ€ ์ „๋‹ฌ ์•ˆ๋˜๋Š” ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์€๋‹ค์Œ, w ๋ฒ”์œ„๋กœ ๋‚˜๋ˆ ์„œ ๊ฐœ์ˆ˜ ์นด์šดํŒ…

 

 

์ฝ”๋“œ

import math

def solution(n, stations, w):
    answer = 0
    dist = []  # ์ „ํŒŒ ์ „๋‹ฌ ์•ˆ๋˜๋Š” ๊ตฌ๊ฐ„ ๊ธธ์ด ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
    for i in range(1, len(stations)):
        dist.append((stations[i]-w-1)-(stations[i-1]+w))
    
    dist.append(stations[0]-w-1)  # ๋งจ์•ž
    dist.append(n-(stations[-1]+w))  # ๋งจ๋’ค
    
    for i in dist:
        if i <= 0:
            continue
        else:
            answer += math.ceil(i/(2*w+1))  # ์˜ฌ๋ฆผ
    return answer

 

๋Œ“๊ธ€