Coding Test/프로그래머스(Python)
[프로그래머스/Python] Level 3_야근 지수(연습문제)
syunze
2024. 2. 1. 14:06
📌문제 유형
연습문제
📌문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
📌나의 문제풀이
- 정확성 만점, 효율성 0
- heapq 이용
import heapq
def solution(n, works):
answer = 0
max_heap = [-i for i in works]
heapq.heapify(max_heap)
while n > 0:
n -= 1
max_num = -heapq.heappop(max_heap)
heapq.heappush(max_heap, -(max_num - 1))
if sum(max_heap) == 0:
break
# 각각의 값을 제곱 -> sum
while len(max_heap) > 0:
answer += (heapq.heappop(max_heap)) ** 2
return answer
- 정답
- n이 sum(works)보다 클 때 계산하지 않고 바로 return하기
import heapq
def solution(n, works):
if n >= sum(works):
return 0
max_heap = [-i for i in works]
heapq.heapify(max_heap)
while n > 0:
n -= 1
max_num = -heapq.heappop(max_heap)
heapq.heappush(max_heap, -(max_num - 1))
return sum([w**2 for w in max_heap])
######
# 하단의 코드로도 가능
while len(max_heap) > 0:
answer += (heapq.heappop(max_heap)) ** 2
return answer
#####
📌다른사람의 문제풀이
[프로그래머스] 야근 지수 #파이썬 #heap #level3 [연습문제]
https://programmers.co.kr/learn/courses/30/lessons/12927 코딩테스트 연습 - 야근 지수 회사원 Demi는 가끔은 야근을 하는데요, 야근을 하면 야근 피로도가 쌓입니다. 야근 피로도는 야근을 시작한 시점에서 남
roomedia.tistory.com
📌리뷰
- 내림차순, 오름차순 시간복잡도 걸릴 때 -> heapq나 priorqueue 사용
- 시간 많이 차지하는 예시는 먼저 처리하기
728x90