Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์Šคํƒ,ํ - ๊ธฐ๋Šฅ๊ฐœ๋ฐœ (level.2)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 13. 17:06
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ธฐ๋Šฅ๊ฐœ๋ฐœ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํŒ€์—์„œ๋Š” ๊ธฐ๋Šฅ ๊ฐœ์„  ์ž‘์—…์„ ์ˆ˜ํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค. ๊ฐ ๊ธฐ๋Šฅ์€ ์ง„๋„๊ฐ€ 100%์ผ ๋•Œ ์„œ๋น„์Šค์— ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜, ๊ฐ ๊ธฐ๋Šฅ์˜ ๊ฐœ๋ฐœ์†๋„๋Š” ๋ชจ๋‘ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์•ž์— ์žˆ๋Š”

programmers.co.kr

 

๋‚˜์˜ํ’€์ด

  1. progresses์—์„œ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„์„œ ์ผ์„ ๋๋งˆ์น  ์ˆ˜์žˆ๋Š” day๋ฅผ ๊ตฌํ•˜๊ณ 
  2. ์ด์ „ day๋ฅผ ์ €์žฅํ•ด๋‘” ๋ณ€์ˆ˜ pre ์™€ ๋น„๊ตํ•˜๋ฉด์„œ countํ•ด์คŒ
def solution(progresses, speeds):
    answer = []
    count = 0
    pre = 0
    while progresses:
        p = progresses.pop(0)
        s = speeds.pop(0)

        if (100-p)%s == 0: 
            day = (100-p)//s
        else:
            day = ((100-p)//s)+1

        if pre == 0 :
            pre = day
            count += 1
        elif pre >= day :
            count += 1
            if len(progresses) == 0:
                answer.append(count)
        elif pre < day:
            answer.append(count)
            pre = day
            count = 1
            if len(progresses) == 0:
                answer.append(count)
    return answer

 

๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

https://programmers.co.kr/learn/courses/30/lessons/42586/solution_groups?language=python3&type=all 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

zip์„ ํ™œ์šฉํ•œ ํ’€์ด

def solution(progresses, speeds):
    Q=[]
    for p, s in zip(progresses, speeds):
        if len(Q)==0 or Q[-1][0]<-((p-100)//s):
            Q.append([-((p-100)//s),1])
        else:
            Q[-1][1]+=1
    return [q[1] for q in Q]

zip์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜์ž ๊ณต๋ถ€ํ•˜์ž ํ•ด๋†“๊ณ  ๊ณต๋ถ€ํ•˜์ง€ ์•Š์•˜๋‹ค. ์˜ค๋Š˜ ์ง„์งœ zip๋ถ€์‹œ๊ณ  ์ž”๋‹ค ใ… ใ…  zip๊ธฐ๋Šฅ์ด ๋ญ๊ธธ๋ž˜ ์ด๋ ‡๊ฒŒ๋‚˜ ํšจ์œจ์ ์ธ ํ’€์ด ๋ฐฉ๋ฒ•์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์ธ๊ฐ€!

๋ฐ˜์‘ํ˜•