๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.3] ๊ธฐ์ง€๊ตญ ์„ค์น˜

by rindev 2021. 4. 28.

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

 

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

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

programmers.co.kr

 

 

ceil์ด ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน๋Š”๋‹ค๋Š” ๊ธ€์„ ๋ด์„œ ๊ทธ๊ฑธ ๊ณ ์ณ์ฃผ์—ˆ์œผ๋‚˜...! ์—ญ์‹œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

์‹คํŒจํ•œ ์ฝ”๋“œ (์‹œ๊ฐ„์ดˆ๊ณผ) → ์ „ํŒŒ๊ฐ€ ๋‹ฟ์ง€์•Š๋Š” ์•„ํŒŒํŠธ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ด์ค€ ๊ฒƒ์ด ๋ฌธ์ œ์˜€๋‹ค!

๋”๋ณด๊ธฐ
def solution(n, stations, w):
    answer = 0

    apts = [0] * (n+1)
    for s in stations:
        # print(s)
        tmpStart = s-w
        tmpEnd = s+w
        if tmpStart < 1:
            tmpStart = 1
        if tmpEnd > n:
            tmpEnd = n
        for i in range(tmpStart, tmpEnd+1):
            apts[i] = 1 #์ „ํŒŒ ๋‹ฟ๋Š”๊ณณ์€ 1๋กœ ํ‘œ์‹œ
    # print(apts)
    
    oneRange = 1 + (w * 2) #ํ•˜๋‚˜์˜ ๊ธฐ์ง€๊ตญ์ด ๋‹ฟ๋Š” ๋ฒ”์œ„, w๊ฐ€ ์ž์—ฐ์ˆ˜์ด๋ฏ€๋กœ ์ตœ์†Œ 3
    aptCnt = 0 
    for i in range(1, n):
        if apts[i] == 0:
            aptCnt += 1
            if apts[i+1] == 1 or i+1 == n:
                answer += aptCnt // oneRange
                if aptCnt % oneRange != 0:
                    answer += 1
                aptCnt = 0

    return answer

 

๊ทธ๋ž˜์„œ ์ด๋ฅผ ์–ด์ฉ๋‹ค ํ•˜๊ณ  ๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋‹ˆ ์ €๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ์ „ํŒŒ๊ฐ€ ๋‹ฟ์ง€์•Š๋Š” ๊ฑฐ๋ฆฌ๋งŒ ๊ณ„์‚ฐํ•ด์„œ ํ•˜์‹  ๋ถ„์ด ๊ณ„์…”์„œ ๊ทธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค์‹œ ํ’€์–ด๋ดค๋‹ค.

 

๐Ÿ™‹‍โ™€๏ธ์ž‘์„ฑํ•œ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import math
def solution(n, stations, w):
    answer = 0

    noStations = [] #ํ†ต์‹ ์ด ์•ˆ๋˜๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋„ฃ๋Š” ๋ฆฌ์ŠคํŠธ
    noStart = 1 #์ „ํŒŒ๊ฐ€ ๋‹ฟ์ง€์•Š๋Š” ์‹œ์ž‘์ 
    
    for s in stations:
        sStart = s - w #์ „ํŒŒ๊ฐ€ ๋‹ฟ๋Š” ์‹œ์ž‘์ 
        sEnd = s + w #์ „ํŒŒ๊ฐ€ ๋‹ฟ๋Š” ๋์ 
        
        if sStart - noStart > 0:
            noStations.append(sStart - noStart)
        noStart = sEnd + 1
    
    if noStart <= n:
        noStations.append(n+1 - noStart)
    # print(noStations)
    
    oneRange = 1 + (2 * w)
    for ns in noStations:
        answer += math.ceil(ns/oneRange)
        
    return answer

๋Œ“๊ธ€