Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- green thread
- deque
- 문자열 내마음대로 정렬하기
- URL
- 크기가 작은 부분 문자열
- Queue
- CPU
- frontPattern
- IO bound
- 십진수 이진수 전환
- TCP/IP
- dns
- 프로그래머스
- port
- 문자열
- process
- Split
- Spring
- 동시성문제
- DICTIONARY
- 2차원 배열 출력
- 코딩테스트
- http
- 가장 가까운 단어
- stack
- springMVC
- reflection
- java
- CPU bound
- annotation
Archives
- Today
- Total
아무나개발하자
프린터 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
"""
1. 2 1 3 2 -> A B C D
2. 1 3 2 2 -> B C D A
3. 3 2 2 1 -> C D A B
4. 2 2 1 -> D A B (C)
5. 2 1 -> A B (D)
6. 1 -> B (A)
7. (B)
출력 순서 -> C D A B
우선 순위 [2, 1, 3, 2] -> 3 2 2 1
인텍스 [0, 1, 2, 3] -> 2 3 0 1
문제해결방법
1. 인덱스와 우선순위를 deque로 만듬
2. 우선순위에서 max idx를 찾음 -> 찾은 idx 번수만큼 인덱스 queue를 popleft -> append
3. 우선순위 dq도 찾은 idx만큼 popleft() -> append
4. 우선순위 popleft(), 인덱스 popleft()
5. 1 ~ 4 반복
"""
from collections import deque
def solution(priorities, location):
answer = 0
dq_pri = deque(priorities)
dq_idx = deque()
for i in range(len(priorities)):
dq_idx.append(i)
while (True):
idx = dq_pri.index(max(dq_pri))
for _ in range(idx):
elem_pri = dq_pri.popleft()
elem_idx = dq_idx.popleft()
dq_pri.append(elem_pri)
dq_idx.append(elem_idx)
dq_pri.popleft()
_location = dq_idx.popleft()
answer += 1
if (_location == location):
break
return answer
출처 : https://school.programmers.co.kr/learn/courses/30/lessons/42587