Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-08-28] sumin #145 #169

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Programmers/야근 지수/sumin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
풀이시간: 20분

<input>
- works: 길이 1 이상, 20,000 이하인 배열
- works의 원소는 50000 이하인 자연수
- n: 1,000,000 이하인 자연수

<solution>
a^2 + b^2 + ... + x^2이 최소가 되려면 계속해서 가장 작업량이 큰 수를 먼저 처리해야 함
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 다음의 아이디어에서 항상 어떻게 최대값을 가져오지 했는데 Heap이 있었네요! 안다고 생각했던 것들을 적절하게 사용하는 것도 체크가 필요할 것 같네요! 많은 걸 배워갑니다 ㅎㅎ

예를 들어, [7, 8] 작업이 있다고 생각했을 때 당연하게도 8을 7로 줄이는 것이 야근 피로도가 최소가 되고
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아이디어를 떠올리기까지 과정을 간략하게 적어주신 점이 좋은 것 같아요!

[7, 7]일 때는 어떤 것을 줄이든 상관없음. [7, 6]이 됐을 때는 다시 7을 6으로 줄이는 것이 야근 피로도를 최소화할 수 있게 됨
-> 이를 통해 항상 전체 작업 중 작업량이 가장 큰 값을 먼저 처리해야 됨을 알 수 있음
-> works의 길이가 최대 20,000이기 때문에 n번 max함수를 쓰면 시간초과가 나기 때문에 우선순위큐를 사용해서 최대값을 찾을 수 있음

<시간복잡도>
O(n): 최대 퇴근까지 남은 시간 n번만 확인하면 됨

"""
import heapq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

힙을 활용해서 효율적이고도 간결하게 문제를 풀이해내실 수 있었던 것 같습니다. 발생할 수 있는 예외 처리 과정도 완벽했던 것 같아요!

from typing import List

def solution(n: int, works: List) -> int:
heap = []
for x in works: # 최대힙으로 구현(작업량이 가장 큰 값)
heapq.heappush(heap, -x)
for i in range(n): # 퇴근까지 남은 시간동안 반복
if not heap: # 더 이상 작업량이 없으면 종료
break
max_num = -heapq.heappop(heap) # 가장 작업량이 큰 업무
max_num -= 1 # 업무 처리
if max_num != 0: # 여전히 작업량이 남은 일만 다시 힙에 추가
heapq.heappush(heap, -max_num)
answer = sum([(-work)**2 for work in heap]) # 야근 피로도 계산
return answer

"""
정확성 테스트
테스트 1 〉 통과 (0.05ms, 10.2MB)
테스트 2 〉 통과 (0.01ms, 10.2MB)
테스트 3 〉 통과 (0.01ms, 10.4MB)
테스트 4 〉 통과 (0.07ms, 10.2MB)
테스트 5 〉 통과 (0.01ms, 10.3MB)
테스트 6 〉 통과 (0.01ms, 10.3MB)
테스트 7 〉 통과 (0.01ms, 10.2MB)
테스트 8 〉 통과 (0.48ms, 10.2MB)
테스트 9 〉 통과 (0.77ms, 10.3MB)
테스트 10 〉 통과 (0.01ms, 10.2MB)
테스트 11 〉 통과 (0.01ms, 10.2MB)
테스트 12 〉 통과 (0.01ms, 10.3MB)
테스트 13 〉 통과 (0.01ms, 10.2MB)

효율성 테스트
테스트 1 〉 통과 (418.78ms, 10.1MB)
테스트 2 〉 통과 (427.85ms, 10.2MB)
"""