[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฃผ์‹๊ฐ€๊ฒฉ /ํŒŒ์ด์ฌ /Python /์Šคํƒ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฃผ์‹๊ฐ€๊ฒฉ /ํŒŒ์ด์ฌ /Python /์Šคํƒ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

โœ… ์ „์ฒด ๊ธธ์ด total์„ ๊ตฌํ•˜๊ณ  prices๋ฐฐ์—ด์— ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•ด์„œ ๊ทธ ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ๊ฒƒ๋“ค(next)์™€ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค

โœ… tmp ๋ณด๋‹ค ํฐ ๊ฐ’์ด ๋‚˜์˜ค๋ฉด second ์ดˆ๋งŒ ๊ณ„์‚ฐ, ์ž‘์€ ๊ฐ’์ด ๋‚˜์˜ค๋ฉด ์ดˆ ๊ณ„์‚ฐ ํ•˜๊ณ  break -> result ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์ฃผ๊ธฐ

๐ŸŽซcode )

def solution(prices):
    result = []
    total = len(prices)
    for i in range(total):
        tmp = prices[i]
        for j in range(i, total):
            next = prices[j]
            if tmp <= next:
                second = j - i
            else:
                second = j - i
                break
        result.append(second)
    return result

 

๐Ÿ“Œ description )

๋ฌธ์ œ์ถœ์ฒ˜ : https://programmers.co.kr/learn/courses/30/lessons/42584

 

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

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

programmers.co.kr

 

๋ฌธ์ œ ์„ค๋ช…

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

์ œํ•œ์‚ฌํ•ญ

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

์ž…์ถœ๋ ฅ ์˜ˆ

pricesreturn
[1, 2, 3, 2, 3] [4, 3, 1, 1, 0]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

  • 1์ดˆ ์‹œ์ ์˜ โ‚ฉ1์€ ๋๊นŒ์ง€ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
  • 2์ดˆ ์‹œ์ ์˜ โ‚ฉ2์€ ๋๊นŒ์ง€ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
  • 3์ดˆ ์‹œ์ ์˜ โ‚ฉ3์€ 1์ดˆ๋’ค์— ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์ดˆ๊ฐ„ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ๋ด…๋‹ˆ๋‹ค.
  • 4์ดˆ ์‹œ์ ์˜ โ‚ฉ2์€ 1์ดˆ๊ฐ„ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
  • 5์ดˆ ์‹œ์ ์˜ โ‚ฉ3์€ 0์ดˆ๊ฐ„ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•