[프로그래머스 lv 2] 프로세스 (스택/큐)

2025. 2. 28. 21:40·✨ 공부 기록/알고리즘

아이디어 : deque를 사용하면 편할 것 같다.

주의할 점 : 프로세스가 '실행'된 값이 answer이다.(큐를 옮긴 횟수가 아님)

 

# 첫 번째 시도

from collections import deque

def solution(priorities, location):
    answer = 0
    queue = deque(priorities)
    while True:
        if location==0:
            if queue[location]==max(priorities):
                answer+=1
                return answer
            else:
                target = queue.popleft()
                queue.append(target)
                location = len(queue)-1
        else:
            if queue[0]==max(priorities):
                answer+=1
                queue.popleft()
                location -= 1
            else:
                target = queue.popleft()
                queue.append(target)
                location -= 1

 

여기서 while문을 계속 도는 것 같은데 어떤 부분이 문제가 있는지 잘 못 찾겠다. -> max(queue)가 아니라 max(priorities)를 찾으니... 당연히 종료가 안되는 거였다.

 

# 두 번째 시도

 

from collections import deque

def solution(priorities, location):
    answer = 0
    queue = deque(priorities)
    while True:
        if location==0:
            if queue[0]==max(queue):
                answer+=1
                return answer
            else:
                target = queue.popleft()
                queue.append(target)
                location = len(queue)-1
        else:
            if queue[0]==max(queue):
                answer+=1
                queue.popleft()
                location -= 1
            else:
                target = queue.popleft()
                queue.append(target)
                location -= 1
저작자표시 비영리 변경금지 (새창열림)

'✨ 공부 기록 > 알고리즘' 카테고리의 다른 글

[프로그래머스 lv 1] 체육복 (탐욕법(Greedy))  (0) 2025.03.02
[프로그래머스 lv 2] 주식가격 (스택/큐)  (0) 2025.03.02
[프로그래머스 lv 2] 올바른 괄호 (스택/큐)  (0) 2025.02.28
[프로그래머스 lv 2] 기능개발 (스택/큐)  (0) 2025.02.28
[프로그래머스 lv 3] 단어 변환 (깊이/너비 우선 탐색(DFS/BFS))  (0) 2025.02.28
'✨ 공부 기록/알고리즘' 카테고리의 다른 글
  • [프로그래머스 lv 1] 체육복 (탐욕법(Greedy))
  • [프로그래머스 lv 2] 주식가격 (스택/큐)
  • [프로그래머스 lv 2] 올바른 괄호 (스택/큐)
  • [프로그래머스 lv 2] 기능개발 (스택/큐)
LaonMoon
LaonMoon
  • LaonMoon
    스토리생성연구블로그
    LaonMoon
  • 전체
    오늘
    어제
  • 공지사항

    • About me👋
    • 분류 전체보기
      • ✨ Story Generation
        • 논문 리뷰
        • 연구 관련 생각
      • ✨ 자연어 처리
        • (짧은) 논문 리뷰
        • HuggingFace
        • Transformer 구현
      • ✨ 공부 기록
        • 알고리즘
        • 딥러닝
        • 웹 개발
        • Flutter
        • Flask
        • Android
        • NLP
        • Docker&k8s
        • Database
        • [24-1] 데이터 분석
        • [24-1] RL
      • ✨ 포트폴리오
        • 2020
        • 2021
        • 2022
        • 2023
        • 2024
      • 프로그래밍
        • 오류(Error)정리
        • 시행착오
        • 리눅스 명령어
        • 공부내용 정리
      • AI Playground
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
LaonMoon
[프로그래머스 lv 2] 프로세스 (스택/큐)
상단으로

티스토리툴바