일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- lv4
- 파이썬
- 코딩테스트
- counter
- 조합
- 프로그래머스
- CodingTest
- 데이터분석
- Stack
- join
- mysql
- 시간복잡도
- 코딩
- coding
- itertools
- Level2
- coding test
- 완전탐색
- 연습문제
- collections
- import re
- BFS
- 코테
- programmers
- sql
- lambda
- python
- time complexity
- Queue
- level4
- Today
- Total
ror_coding
[Programmers] 프로세스 - 42587 본문
728x90
from collections import deque를 사용하여 우선 순위를 제거 !
now에 넣어서 비교하고 다시 queue 뒤에 넣는 방법 생각을 못 했다..!
Question
현재 실행 대기 큐(Queue)에 있는 프로세스의 중요도가 순서대로 담긴 배열 priorities와, 몇 번째로 실행되는지 알고싶은 프로세스의 위치를 알려주는 location이 매개변수로 주어질 때, 해당 프로세스가 몇 번째로 실행되는지 return 하도록 solution 함수를 작성해주세요.
Point
- index를 번호를 부여.
- if 문 ) queue의 맨 앞을 now로 넣고 now의 값과 queue안에 있는 모든 값들 중 어느 하나라도 now 값보다 크면 다시 queue 맨 뒤에 추가함. ( cycle처럼 도는 느낌 )
- else 문 ) now의 값이 가장 크기 때문에 q에 다시 넣지 않고 지움. => 이때, location을 비교하여 구하려던 거였으면 그대로 return. 아니라면 cnt += 1.
Code (After)
from collections import deque
def solution(priorities, location):
l = len(priorities)
queue = deque([(i,p) for i,p in enumerate(priorities)])
max_value = max(priorities)
while queue:
idx, p = queue.popleft()
if p == max_value and idx == location: break
elif p == max_value: priorities.remove(p); max_value = max(priorities)
else : queue.append((idx,p))
return l - len(queue)
Code (Before)
from collections import deque
def solution(priorities, location):
prior = [[idx,i] for idx, i in enumerate(priorities)]
q = deque(prior)
cnt = 0
while q:
now = q.popleft()
if any(now[1] < queue[1] for queue in q): q.append(now)
else :
if now[0] == location : return cnt + 1
cnt += 1
return -1 # cnt + 1
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/045.gif)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 롤케이크 자르기 - 132265 (1) | 2024.12.05 |
---|---|
[Programmers] 타겟 넘버 - 43165 (0) | 2024.12.05 |
[Programmers] 튜플 - 64065 (0) | 2024.12.04 |
[Python] re (정규표현식) (0) | 2024.12.04 |
[Python] Permutations, Combination, Product (Itertools) (0) | 2024.12.04 |