[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] ์Šคํƒ/ํ : ์ฃผ์‹๊ฐ€๊ฒฉ

2021. 7. 21. 21:02

๋ฌธ์ œ ์„ค๋ช…

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

 

์ œํ•œ์‚ฌํ•ญ

  • prices์˜ ๊ฐ ๊ฐ€๊ฒฉ์€ 1 ์ด์ƒ 10,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • prices์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

 

์ •๋‹ต

def solution(prices):
    answer = [i for i in range(len(prices)-1,-1,-1)]
    
    for before in range(len(prices)):
        for after in range(before+1,len(prices)):
            if prices[after] < prices[before]:
                answer[before] = after-before
                break
    
    return answer

๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

 

ํ˜„์žฌ์˜ ์‹œ์ ์—์„œ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์งˆ ๋•Œ๊นŒ์ง€์˜ ์‹œ๊ฐ„์„ return ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๋๊นŒ์ง€ ๋–จ์–ด์ง€์ง€์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  prices ๊ธธ์ด๋งŒํผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ answer ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค€ ๋‹ค์Œ

์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹ ๋ณด๋‹ค ํฐ ๊ฐ€๊ฒฉ์ด ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์šฉํ•ด answer ๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.

728x90

BELATED ARTICLES

more