Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 프로그래머스 파이썬
- 우분투
- 파이썬
- 캐치카페
- hackerrank
- AI 경진대회
- 프로그래머스
- 자연어처리
- SW Expert Academy
- Git
- Docker
- ubuntu
- Baekjoon
- programmers
- 편스토랑
- 더현대서울 맛집
- 맥북
- ChatGPT
- Kaggle
- 백준
- gs25
- 금융문자분석경진대회
- github
- dacon
- 편스토랑 우승상품
- leetcode
- 데이콘
- Real or Not? NLP with Disaster Tweets
- 코로나19
- PYTHON
Archives
- Today
- Total
솜씨좋은장씨
[Programmers] 스택/큐 : 프린터 (Python) 본문
728x90
반응형

1일 1문제 24일차!
오늘의 문제는 프로그래머스 스택 큐 프린터입니다.
코딩테스트 연습 - 프린터 | 프로그래머스
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린터를 개발했습니다. 이 새롭게 개발한 프린터는 아래와 같은 방식으로 인쇄 작업을 수행합니다. 1. 인쇄 대기목록의 가장 앞에 있는 문서(J)를 대기목록에서 꺼냅니다. 2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면 J를 대기목록의 가장 마지막에
programmers.co.kr
첫번째 시도
def solution(priorities, location):
answer = 0
pri_dict = {}
for i in range(len(priorities)):
pri_dict.update({i+1:priorities[i]})
pri_dict = sorted(pri_dict.items(), key=lambda x: -x[1])
for i in range(len(pri_dict)):
if pri_dict[i][0] == location + 1:
answer = i + 1
break
return answer


두번째 시도
def solution(priorities, location):
answer = 0
stack = []
pri = [(i, p) for i, p in enumerate(priorities)]
for i in range(len(priorities)-1):
max_pri = max(priorities[i+1:])
temp = pri[0]
pri = pri[1:]
if priorities[i] < max_pri:
pri.append(temp)
else:
stack.append(temp)
for i in range(len(pri)):
stack.append(pri[i])
for i in range(len(stack)):
if stack[i][0] == location:
answer = i + 1
break
return answer


세번째 시도
def solution(priorities, location):
answer = 0
while priorities!=[]:
max_pri=max(priorities)
pop_num=priorities.pop(0)
if max_pri==pop_num:
answer+=1
if location==0:
break
else:
location -=1
else:
priorities.append(pop_num)
if location==0:
location=len(priorities)-1
else:
location-=1
return answer


SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 151. Reverse Words in a String (Python) (0) | 2020.03.03 |
---|---|
[BaeKJoon] 11656번: 접미사 배열 (Python) (0) | 2020.03.02 |
[leetCode] 7. Median of Two Sorted Arrays (Python) (2) | 2020.02.29 |
[Programmers] 완전탐색 : 소수 찾기 (Python) (0) | 2020.02.28 |
[Programmers] 해시 : 베스트앨범 (Python) (2) | 2020.02.27 |