-
Notifications
You must be signed in to change notification settings - Fork 5
Hongjoo/ 7월 1주차 /2 #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hongjoo/ 7월 1주차 /2 #232
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| # 우선순위 | ||
| - wait_que = [[작업 번호 , 작업 요청 시간 , 소요시간]] | ||
| - if wait_que : | ||
| #우선순위 높은 순 부터 작업 할당 | ||
| 우선순위 : [소요시간 short , 요청시간이 fast , 번호가 작은 것] | ||
| - intercept 없음 | ||
| - 같은 time 에 HD에 작업이 끝나는 시점 == 다른 작업 요청이 들어오는 시점일 경우, | ||
| HD 작업 종료 -> 바로 wait queue에서 ready queue로 할당 | ||
| - 출력 : 각 jobs들의 반환 시각의 평균값의 정수부분 | ||
| # INSPECT | ||
| 1. 다중 변수에 대한 우선순위 큐 사용하기 | ||
| 2. time += 1 각 time 별로 (1) wait 큐에 <- jobs 할당하기 (2) HD 에 <- wait 큐속 작업 할당하기 등 전체 작업 진행 | ||
| """ | ||
| import heapq | ||
| def solution(jobs): | ||
| answer = 0 ; a = [] | ||
| wait = [] | ||
| # 반복문 종료 조건 : 모든 jobs가 HD 작업 종료 | ||
| finished = 0 ; t = 0 ; hd_timeout = -1 | ||
| # time += 1 로 wait 큐와 HD 할당을 각 시각별로 확인 및 처리 | ||
| while finished < len(jobs) : | ||
| # print(f"##t {t}") | ||
| #[1] wait 큐에 요청 시각에 맞춰 작업 할당하기 | ||
| for idx , (accept_time , duration) in enumerate(jobs): | ||
| if accept_time == t : | ||
| heapq.heappush(wait , [duration , accept_time, idx]) | ||
|
|
||
| #2. HD 작업 진행 여부 확인 | ||
| #[1]. HD 작업 진행중 | ||
| if hd_timeout > 0 : # HD 작업 진행중 | ||
| hd_timeout -=1 | ||
| #[2] HD 작업 완료 및 비어 있을 때(hd_timout 음수 , 초기 시점) | ||
| elif hd_timeout <= 0 : | ||
| if hd_timeout == 0 : # (1) 진행중인 HD 작업 완료 -> 완료 작업 개수 += 1 | ||
| a.append([t, start_t]) # 작업의 (종료 시점, 요청 시점) 을 정답 리스트에 반환 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 작업의 결과를 리스트에 저장하고 리스트의 길이를 answer에 나눠서 answer로 반환하는 점이 다른 코드랑 달라서 새로워서 좋네용! |
||
| finished += 1 | ||
| hd_timeout = -1 # hd 비어있음 | ||
|
|
||
| # wait 큐의 존재하면 -> wait큐에서 우선순위 작업을 뽑아 HD에 할당하기 | ||
| if wait : | ||
| hd_timeout , start_t , id = heapq.heappop(wait) | ||
| hd_timeout = hd_timeout - 1 | ||
|
|
||
| t += 1 | ||
|
|
||
| # [4] 작업 반환 시간(turnaround_time) : 요청시간 - 완료 시간 | ||
| for e, s in a : | ||
| answer += (e - s) | ||
| answer = answer // len(a) | ||
|
|
||
| return answer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """ | ||
| - 우선순위 큐 함수 조건 | ||
| - l 숫자 : 큐에 해당 숫자 삽입 | ||
| - D -1 : 큐에서 최소값 삭제(빈 큐면 무시 ) | ||
| - D 1 : 큐에서 최대값 삭제 | ||
| - 출력 : 연산 종료 후 큐가 비어 있음 [0,0] / 비어 있지 않음 [최대값, 최솟값] | ||
| - | ||
| """ | ||
| import heapq | ||
| def solution(operations): | ||
| answer = [] | ||
| heap = [] | ||
|
|
||
| def prior_queue(operate , heap) : # 입력 명령문 종류에 따라 3가지 동작 중 택 1을 적용한다. | ||
|
|
||
| action , num = operate.split() | ||
| # [0] (예외처리)빈 heap에 삭제 명령이면, 무시한다. | ||
| if not heap and action == "D": | ||
| return heap | ||
| # [1] 큐에 삽입하기 | ||
| if action == "I" : | ||
| heapq.heappush(heap , int(num)) | ||
| # [2] 최소값 삭제 | ||
| elif action == "D" and num == "-1": | ||
| heapq.heapify(heap) | ||
| heapq.heappop(heap) | ||
| #[3] 최대값 삭제 | ||
| elif action == 'D' and num == "1" : | ||
| heap = list(map(lambda x : -1*x , heap)) | ||
| heapq.heapify(heap) | ||
| heapq.heappop(heap) | ||
| heap = list(map(lambda x : -1*x , heap)) | ||
|
|
||
| return heap | ||
|
Comment on lines
+18
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 최소힙, 최대힙 2개라 나누고 동기화( if cmd == 'I':
# 삽입
heapq.heappush(min_heap, num)
heapq.heappush(max_heap, -num)
number_count[num] = number_count.get(num, 0) + 1
size += 1
else: # cmd == 'D'
if size == 0:
# 큐가 비어있다면 연산 무시
continue
if num == 1:
# 최댓값 삭제
while max_heap:
x = -heapq.heappop(max_heap)
if number_count.get(x, 0) > 0:
# num이 1번 이상 등장했다면, 카운트 반영
number_count[x] -= 1
size -= 1
break
else:
# num == -1, 최솟값 삭제
while min_heap:
x = heapq.heappop(min_heap)
if number_count.get(x, 0) > 0:
# num이 1번 이상 등장했다면, 카운트 반영
number_count[x] -= 1
size -= 1
break |
||
|
|
||
| #2. 입력 명령문에 대해 반복 동작하기 | ||
| for o in operations : | ||
| heap=prior_queue(o , heap ) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수화해서 표현한 점 좋습니당! |
||
|
|
||
| #3. 출력 형태 맞춰 변형하기 | ||
| if len(heap) >=2 : # (1)안 비어 있으면 -> 최대값 & 최소값 출력 | ||
| heap.sort() | ||
| return [heap[-1] , heap[0]] | ||
| elif len(heap) < 1 : # (2) 비어 있음 -> [0,0] | ||
| return [0,0] | ||
| else : # (3) heap 이 1개 남아 있을떄 -> 최대값 = 최소값 *[테케 8,9,10] | ||
| return [heap[0] , heap[0]] | ||
|
|
||
| return answer | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제 풀이 과정을 이렇게 항상 정리해두시는데 코드 이해하는데 도움이 되는 것 같아요! 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
발제 정답 코드는 여기에 올려두었으니 참고해주세요!
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W29-%5BPriorityQueue%5D/Study_PGS_42627_%E1%84%83%E1%85%B5%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%8F%E1%85%A5%E1%86%AB%E1%84%90%E1%85%B3%E1%84%85%E1%85%A9%E1%86%AF%E1%84%85%E1%85%A5.py