ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ”„๋ฆฐํ„ฐ

์ผ๋ฐ˜์ ์ธ ํ”„๋ฆฐํ„ฐ๋Š” ์ธ์‡„ ์š”์ฒญ์ด ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•œ ๋ฌธ์„œ๊ฐ€ ๋‚˜์ค‘์— ์ธ์‡„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์„œ๋ฅผ ๋จผ์ € ์ธ์‡„ํ•˜๋Š” ํ”„๋ฆฐ๏ฟฝ๏ฟฝ

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

 ์ค‘์š”๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋ฌธ์„œ๋ถ€ํ„ฐ ์ธ์‡„๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์—ฌ, ๋‚ด๊ฐ€ ์š”์ฒญํ•œ ๋ฌธ์„œ๋Š” ๋ช‡ ๋ฒˆ์งธ์— ์ธ์‡„๊ฐ€ ๋˜๋Š”์ง€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ์œ„์น˜(location)๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์ค‘์š”๋„๊ฐ€ ๋‚ฎ์„ ๊ฒฝ์šฐ ํ๋ฅผ ํ™œ์šฉํ•ด ๋’ค๋กœ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋‚ด๊ฐ€ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์–ธ์ œ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 ์˜ˆ๋ฅผ ๋“ค์–ด ์˜ˆ์ œ #1์˜ ๊ฒฝ์šฐ, [2, 1, 3, 2]์ด๊ณ  ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ 2๋Š” ๊ฐ€์žฅ ๋จผ์ € ์ถœ๋ ฅ๋œ๋‹ค. ์ด๋Š” ์ถœ๋ ฅ ๋Œ€๊ธฐ์—ด์˜ ์ตœ๋Œ€ ๊ฐ’๊ณผ ์ธ์‡„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์ผ์น˜ํ•œ๋‹ค๋ฉด ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • priorities๋ฅผ `deque`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ๋กœ ๋งŒ๋“ ๋‹ค.
  • ํ˜„์žฌ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์ธ์‡„ ๋Œ€๊ธฐ ์ค‘์ธ ๋ฌธ์„œ ์ค‘ ์ตœ๋Œ€๊ฐ’์ด๋ผ๋ฉด, ์ถœ๋ ฅํ•œ๋‹ค.
    • ๋ฌธ์„œ๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด, answer๋ฅผ 1 ์ฆ๊ฐ€ ์‹œํ‚จ๋‹ค. 
    • location์ด 0์ด๋ผ๋ฉด ๋‚ด๊ฐ€ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์ถœ๋ ฅ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.
    • location์ด 0์ด ์•„๋‹ˆ๋ผ๋ฉด, ๋‚ด๊ฐ€ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์•„๋‹ˆ๊ณ  ๋‹ค๋ฅธ ๊ฒƒ์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์ด๋ฏ€๋กœ location์„ 1 ๊ฐ์†Œ์‹œํ‚จ๋‹ค.
  • ์•„๋‹Œ ๊ฒฝ์šฐ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๋ฅผ ์ธ์‡„ ๋Œ€๊ธฐ์—ด์˜ ๋์œผ๋กœ ๋ณด๋‚ธ๋‹ค.
    • location์ด 0 ์ธ๊ฒฝ์šฐ ๋‚ด๊ฐ€ ์ธ์‡„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ์˜ ์œ„์น˜๋ฅผ ํ˜„์žฌ ์ธ์‡„ ๋Œ€๊ธฐ์—ด์˜ ๋์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
    • location์ด 0์ด ์•„๋‹ˆ๋ผ๋ฉด, ๋‹ค๋ฅธ ๋ฌธ์„œ๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  ์•ž์œผ๋กœ ํ•œ ์นธ ๋‚˜์•„๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ location์„ 1 ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

 

์ฝ”๋“œ

from collections import deque


def solution(priorities, location):
    answer = 0
    max_priority = max(priorities)
    priorities = deque(priorities)

    while True:
        priority = priorities.popleft()
        if max_priority == priority:
            answer += 1
            if not location:
                break
            else:
                location -= 1        
            max_priority = max(priorities)
        else:
            priorities.append(priority)
            location = len(priorities) - 1 if not location else location - 1
    return answer
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€