์ƒˆ์†Œ์‹

๐Ÿงฎ PS

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

  • -

* ๋ฌธ์ œ ๋งํฌ

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

 

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

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

programmers.co.kr

 

* ๋ฌธ์ œ ๋ถ„๋ฅ˜ , ๋‚œ์ด๋„

์Šคํƒ/ํ  , Lv2

 

* ๋ฌธ์ œ ์š”์•ฝ

์ดˆ ๋‹จ์œ„๋กœ ๊ธฐ๋ก๋œ ์ฃผ์‹๊ฐ€๊ฒฉ ๋ฐฐ์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ธฐ๊ฐ„์ด ๋ช‡์ดˆ์ธ์ง€ Return ํ•˜๋ผ.

 

* ํ’€์ด ๋ฐฉ๋ฒ•

  • 2์ค‘ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹ ๋ณด๋‹ค ๋’ท ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋“ค์„ ๊ฒ€์‚ฌํ•œ๋‹ค.
  • ๊ฒ€์‚ฌ ๋„์ค‘ ๊ฐ€๊ฒฉ์ด ํ•˜๋ฝํ•˜๋ฉด break
  • ๊ฐ€๊ฒฉ์ด ํ•˜๋ฝํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด cnt์— 1์„ ๋”ํ•ด์ค€๋‹ค.
  • ์ตœ์ข…์ ์œผ๋กœ cnt (๊ฐ€๊ฒฉ์ด ํ•˜๋ฝํ•œ ์š”์†Œ์˜ ์ธ๋ฑ์Šค) - idx(์ž๊ธฐ ์ž์‹ ์˜ ์ธ๋ฑ์Šค) ๋ฅผ answer์— ๋„ฃ์–ด์ค€๋‹ค.

 

* ํŒŒ์ด์ฌ(Pyhton) ์ฝ”๋“œ

def solution(prices):
    maxIndex = len(prices) - 1
    answer =  [0] * (maxIndex + 1)
    for idx, price in enumerate(prices) :
        cnt = idx
        for i in range(maxIndex - idx) :
            cnt += 1
            if(not price <= prices[cnt]) :
                answer[idx] = cnt - idx
                break
            answer[idx] = cnt - idx
    return answer

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.