๊ธ€ ์ž‘์„ฑ์ž: ํƒ์‹œ ์šด์ „์‚ฌ
๋ฐ˜์‘ํ˜•

๐Ÿ–จ ํ”„๋ฆฐํ„ฐ ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ

๐Ÿ˜ƒ ๋‚˜์˜ ์ฝ”๋“œ

def solution(priorities, location):
    pos = []
    for i in range(len(priorities)):
        if i == location:
            pos.append(True)
        else: pos.append(False)
    answer = 0
    count = 0
    m = max(priorities)
    while True:
        if m > priorities[0]:
            priorities.append(priorities.pop(0))
            pos.append(pos.pop(0))
        else:
            count += 1
            priorities.pop(0)
            if pos.pop(0):
                return count
            m = max(priorities)

์Šคํƒ(stack)/ํ(queue) ์ž๋ฃŒ๊ตฌ์กฐ์— ์†ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. location์˜ ์œ„์น˜๋ฅผ ๊ณ„์† ์•Œ๊ณ  ์žˆ์œผ๋ฉด์„œ, ์ „์ฒด์—์„œ ๊ฐ€์žฅ ํฐ ์›์†Œ์ธ ์ง€ ์•„๋Š” ๊ฒƒ์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๊ฑด ๋งค์šฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋‘ ๊ฐ€์ง€ ์ •๋„์˜ ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ๋‚ฌ์Šต๋‹ˆ๋‹ค. location์˜ ์œ„์น˜๋ฅผ ๊ฐ’์œผ๋กœ ๋†“๊ณ  ์‹œํ–‰์— ๋”ฐ๋ผ location์˜ ์œ„์น˜๋ฅผ ๋”ฐ๋ผ๊ฐ€๋Š” ๋ฐฉ์‹๊ณผ priorities์™€ ๊ฐ™์€ ๊ธธ์ด์˜ location์˜ ์œ„์น˜์— True๋ฅผ ์ด์™ธ์—๋Š” False์˜ ๊ฐ’์„ ๊ฐ–๋Š” pos๋ผ๋Š” list๋ฅผ ๋งŒ๋“ค์–ด ๋งŒ์•ฝ pos์—์„œ ์ง€์›Œ์ง€๋Š” ๊ฐ’์ด True๋ผ๋ฉด ๊ทธ ๋•Œ์˜ count ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ œ ์ฝ”๋“œ๋Š” ํ›„์ž์˜ ๋ฐฉ์‹์ด๊ณ  ์ „์ž์˜ ๋ฐฉ์‹์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

def solution(p, l):
    ans = 0
    m = max(p)
    while True:
        v = p.pop(0)
        if m == v:
            ans += 1
            if l == 0:
                break
            else:
                l -= 1
            m = max(p)
        else:
            p.append(v)
            if l == 0:
                l = len(p)-1
            else:
                l -= 1
    return ans

Written with StackEdit.

๋ฐ˜์‘ํ˜•