728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
나의 풀이
- 프린트 인쇄물의 priorities가 있고 prioritie가 높은 인쇄물을 우선적으로 뽑아야 한다.
- 그럴 때 location의 인쇄물은 몇 번 만에 인쇄될까를 구하는 문제이다.
priorities | location | return |
[2,1,3,2] | 2 | 1 |
[1, 1, 9, 1, 1, 1] | 0 | 5 |
max 값보다 작다면 temp_list에 담고 max값이 들어오면 for문을 중단하고 남은 list를 temp_list에 합친다.
def solution(priorities, location):
print_list = list()
print_temp = list()
answer = 0
for i in range(len(priorities)):
print_list.append((i, priorities[i]))
for i in range(len(priorities)):
pri = max(print_list, key=lambda x: x[1])[1]
for _ in range(len(priorities)):
if print_list[0][1] == pri:
pri_pop = print_list.pop(0)
answer += 1
if pri_pop[0] == location:
return answer
break
else:
pri_pop = print_list.pop(0)
print_temp.append(pri_pop)
print_list = print_list+print_temp
print_temp = list()
return answer
다른 사람의 풀이
any()를 사용하여 문제를 해결함
https://pasongsong.tistory.com/359
[Python] 파이썬 any(), all()
파이썬의 내장 함수로 iterable 한 객체를 받아 조건에 맞으면 True 맞지 않으면 False를 출력한다. any() 하나라도 True가 있다면 True를 반환(OR과 유사함) any([False, True, False]) any([False, False, False]) any([True
pasongsong.tistory.com
pop으로 뽑고 제일 큰 수라면 answer+= 1을 하고 아니라면 다시 배열에 넣는다.
def solution(priorities, location):
queue = [(i,p) for i,p in enumerate(priorities)]
print('queue',queue)
answer = 0
while True:
cur = queue.pop(0)
print('cur',cur)
if any(cur[1] < q[1] for q in queue):
queue.append(cur)
else:
answer += 1
if cur[0] == location:
return answer
728x90
'Coding Test > programmers' 카테고리의 다른 글
[Python] 파이썬 프로그래머스 2차원 동전 뒤집기 (0) | 2023.03.20 |
---|---|
[Python] 파이썬 프로그래머스 뉴스 클러스터링 (0) | 2023.03.10 |
[Python] 파이썬 프로그래머스 선입 선출 스케줄링 (0) | 2023.03.09 |
[Python] 파이썬 프로그래머스 빛의 경로 사이클 (0) | 2023.03.08 |
[Python] 파이썬 프로그래머스 연속 부분 수열 합의 개수 (0) | 2023.02.28 |