티스토리 뷰
https://school.programmers.co.kr/learn/courses/30/lessons/214288
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제를 푸는 방법은 간단하다
1. 멘토 배정의 조합을 구한다 -> itertools.combinations 사용
2. 해당 조합으로 heapq 를 사용하여 순회한다 -> heapq push, pop
3. 조합 돌리면서 최소의 시간을 구한다
import heapq
from itertools import combinations
def calculate_wait_time(mentor_distribution, reqs):
queues = {i: [] for i in range(1, len(mentor_distribution) + 1)}
for i, count in enumerate(mentor_distribution, 1):
# 주제: [0 for in 멘토수] 으로 초기화 -> 모든 주제의 각 멘토는 0부터 시작 가능
# e.g) 조합 -> [1,2,3] -> {1: [0], 2: [0,0], 3:[0,0,0]}
queues[i] = [0 for _ in range(count)]
wait_time_sum = 0
for request in reqs:
a, b, c = request # 시각 a, 상담 시간 b, 상담 유형 c
end_time = heapq.heappop(queues[c])
start_time = max(end_time, a)
wait_time_sum += (start_time - a)
heapq.heappush(queues[c], start_time + b)
return wait_time_sum
def find_optimal_distribution(k, n, reqs):
min_wait_time = float('inf')
# 가능한 멘토 분배 조합을 모두 탐색
combs = combinations(range(n-1), k-1)
for comb in combs:
distribution = [0] * k
last = 0
for i, v in enumerate(comb):
distribution[i] = v + 1 - last
last = v + 1
distribution[-1] = n - last
print(f"{comb} | {distribution}")
wait_time = calculate_wait_time(distribution, reqs)
min_wait_time = min_wait_time if min_wait_time < wait_time else wait_time
return min_wait_time
def solution(k, n, reqs):
answer = 0
answer = find_optimal_distribution(k,n,reqs)
return answer
combinations 를 지금까지 몰랐던 내가 한심해지는 순간이다
'연습' 카테고리의 다른 글
모듈러 연산과 시간 초과의 관계 (1) | 2024.06.22 |
---|---|
DP 풀기 전 정독하기 (0) | 2024.06.21 |
프로그래머스 파일명 정렬 (0) | 2024.06.17 |
프로그래머스 h-index (0) | 2024.06.17 |
python cmp_to_key (프로그래머스 가장 큰 수) (0) | 2024.06.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- docker-compose update
- 삽질
- 이것도모르면바보
- 코딩테스트
- SQL
- 우선순위큐
- endl을절대쓰지마
- Remote
- 불필요한 값 무시하기
- django testcase
- 파이썬
- BOJ
- 최대한 간략화하기
- cipher suite
- jwt
- 프로그래머스
- 그리디
- 위상정렬
- Javascript
- factory_pattern
- vscode
- 백준
- requests
- Til
- Event Sourcing
- Python
- 힙
- SSL
- 스택
- django test
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함