Notice
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 | 31 |
Tags
- Level2
- 백준
- KT 에이블스쿨
- 티스토리챌린지
- Java
- 프로그래머스
- 알고리즘
- join
- 정렬
- 패스트캠퍼스
- python
- dfs
- kt에이블스쿨5기
- Level3
- dp
- sql
- AIVLE
- Level1
- 그래프
- AI트랙
- KT에이블스쿨
- 구현
- BFS
- 딕셔너리
- spring
- 실버
- 오블완
- 파이썬
- 트리
- 골드
Archives
- Today
- Total
rose_brown
[프로그래머스] 디스크 컨트롤러 본문
1. 문제
https://school.programmers.co.kr/learn/courses/30/lessons/42627
2. 코드
python 1
from heapq import heappush as push, heappop as pop
def solution(jobs):
answer, now, i = 0, 0, 0
start = -1
heap = []
while i < len(jobs):
for job in jobs:
if start < job[0] <= now:
push(heap, job[::-1])
if len(heap) > 0:
cur = pop(heap)
start = now
now += cur[0]
answer += now - cur[1]
i += 1
else:
now += 1
return answer // len(jobs)
풀이
- answer - 작업시간의 총합, heap - 우선 큐 담당 배열
- now - 현재시점, start - 시작시점
- 현재 시점까지 도착한 작업들을 heap에 추가 → 가장 짧은 처리시간을 가진 작업을 우선함
- 현재 작업 중일때
- 가장짧은 처리시간을 가진 작업 꺼냄
- start를 now로 변경
- now를 현재 처리시간을 더해서 변경함
- answer = 작업완료시간 - 도착시간
- 작업이 없으면 → 시간증가해서 도착시간이 되게 함
python 2
import heapq
from collections import deque
def solution(jobs):
tasks = deque(sorted([(x[1], x[0]) for x in jobs], key=lambda x: (x[1], x[0])))
q = []
heapq.heappush(q, tasks.popleft())
current_time, total_response_time = 0, 0
while len(q) > 0:
dur, arr = heapq.heappop(q)
current_time = max(current_time + dur, arr + dur)
total_response_time += current_time - arr
while len(tasks) > 0 and tasks[0][1] <= current_time:
heapq.heappush(q, tasks.popleft())
if len(tasks) > 0 and len(q) == 0:
heapq.heappush(q, tasks.popleft())
return total_response_time // len(jobs)
3. 메모
- heapq로 가장 작은 값을 빠르게 꺼낼 수 있어서 사용함
- python 2가 속도가 더 빠름
- 나는 heapq 사용까지만 생각함(python 1)
'코딩 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 섬 연결하기 (0) | 2025.02.14 |
---|---|
[프로그래머스] 보석쇼핑 (0) | 2025.02.14 |
[프로그래머스] 길 찾기 게임 (0) | 2025.02.06 |
[프로그래머스] 순위 (0) | 2025.02.06 |
[프로그래머스] 가장 먼 노드 (1) | 2025.02.06 |