본문 바로가기

Max Heap3

[자료구조] 야근 수당 파이썬 (heap) from heapq import heapify, heappush, heappop from collections import deque def solution(n, works): if n > sum(works): return 0 works = [(-i,i) for i in works] heapify(works) for _ in range(n): w = heappop(works)[1] - 1 heappush(works, (-w,w)) ################ # print works # while works: # w = heappop(works) # print(w) return sum(i[1]**2 for i in works) max value를 하나씩 -1 해줄 것이기 때문에 max heap를 구성해.. 2021. 4. 25.
[자료구조] 게임 아이템 파이썬 (heap, deque) 해답 from heapq import heapify, heappush, heappop from collections import deque def solution(healths, items): healths.sort() # 체력을 오름차순으로 정렬 items = sorted([(item[1], item[0], index+1) for index, item in enumerate(items)]) # 깎는 체력 순으로 정렬 items = deque(items) answer = [] heap = [] for health in healths: # 제일 작은 체력부터 루프 while items: # 아이템 루프 debuff, buff, index = items[0] # 가장 깎는 체력이 낮은 아이템 if healt.. 2021. 4. 25.
[자료구조] 배상 비용 최소화 파이썬 (heap) def solution(no, works): # max_idx = works.index(max(works)) # works[works.index(max(works))] -= 1 while no > 0: # reach 0 exit max_idx = works.index(max(works)) works[max_idx] -= 1 no -= 1 return sum([x**2 if x > 0 else 0 for x in works]) return할때 0보다 큰 조건을 안넣어줘서 오답이 나온것 같아서 넣어줬더니 통과했다. 일단 여기까지는 했는데 효율성에서 통과를 못했다 시간을 줄일 수 있는 부분이 while문 안에서 해결되어야 될거 같은데 음. sorted로 사용하니까 통과하긴 하는데 오래걸리긴 한다. def so.. 2021. 4. 22.