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

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฃผ์‹๊ฐ€๊ฒฉ (level.2)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 23. 18:10
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฃผ์‹๊ฐ€๊ฒฉ

์ดˆ ๋‹จ์œ„๋กœ ๊ธฐ๋ก๋œ ์ฃผ์‹๊ฐ€๊ฒฉ์ด ๋‹ด๊ธด ๋ฐฐ์—ด prices๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ธฐ๊ฐ„์€ ๋ช‡ ์ดˆ์ธ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ์ œํ•œ์‚ฌํ•ญ prices์˜ ๊ฐ ๊ฐ€๊ฒฉ์€ 1 ์ด์ƒ 10,00

programmers.co.kr

 

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

deque๋ฅผ์จ์•ผ ํšจ์œจ์„ฑํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์˜์™ธ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฆฌ์ŠคํŠธํ˜•ํƒœ๋กœ ํ’€์—ˆ๋”๋‹ˆ ๊ทธ๋ƒฅ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค. 

def solution(prices):
    answer = []
    for i in range(len(prices)-1):
        price = prices[i]
        for j in range(i+1, len(prices)):
            if price > prices[j]:
                answer.append(j-i)
                break
            elif j == len(prices)-1:
                answer.append(j-i)
    answer.append(0)
                
    return answer

 

 

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

https://programmers.co.kr/learn/courses/30/lessons/42584/solution_groups?language=python3 

 

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

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

programmers.co.kr

๋‚˜์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๋ฅผ appendํ•ด์ฃผ๋Š” ๋ฐฉ์‹์€ ์•„๋‹ˆ๊ณ  0์œผ๋กœ answer๋ฆฌ์ŠคํŠธ๋ฅผ ์ฑ„์šดํ›„ ํ•˜๋‚˜์”ฉ +1 ํ•ด๋‚˜๊ฐ€๋Š”๊ฒŒ ์‹ ๊ธฐํ–ˆ๋‹ค. ๋ฌธ์ œํ’€์ด์— ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•ต์‹ฌ์„ ์ƒ๊ฐํ•œ ๋ฐฉ์‹์€ ๋™์ผํ–ˆ๋‹ค.

def solution(prices):
    answer = [0] * len(prices)
    for i in range(len(prices)):
        for j in range(i+1, len(prices)):
            if prices[i] <= prices[j]:
                answer[i] += 1
            else:
                answer[i] += 1
                break
    return answer

๊ทธ๋ž˜๋„ ์ด๋ ‡๊ฒŒ ํ•˜๋Š”๊ฒŒ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ ๊ฐ™์•˜๋‹ค.

๋ฐ˜์‘ํ˜•