-
Notifications
You must be signed in to change notification settings - Fork 1
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
3-kjs254 #10
3-kjs254 #10
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,16 @@ | ||
import heapq | ||
def solution(n, works): | ||
max_heap = [] | ||
for w in works: | ||
heapq.heappush(max_heap, (-w,w)) | ||
|
||
for i in range(n): | ||
max_work = heapq.heappop(max_heap)[1]-1 | ||
if max_work<0: | ||
return 0 | ||
|
||
heapq.heappush(max_heap,(-max_work,max_work)) | ||
Comment on lines
+7
to
+12
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. ii μμ μ€λͺ ν΄μ£Όμ ¨λ κ²μ²λΌ μκ° λ³΅μ‘λμ λν΄ μ€λͺ ν΄μ£Όμλ©° μκ°λ³΅μ‘λκ° O(n)μ²λΌ λμ€λ©° μκ°μ΄ μ€λκ±Έλ € heapμ μ΄μ©νμ¬ μκ°μ μ€μ΄κ² λ€λ μ λ΅μ μ μ΄ν΄νμμ΅λλ€. μ’μ λ°©λ²μΈκ² κ°μ΅λλ€. μ λ μ²μμ 맀 μν μ sort νμ¬ μ μΌ ν° κ°μ μ°Ύμ -1 νκ² λ€κ³ μκ°νμμΌλ, μ‘°κΈ κ³ λ―Όν΄λ³΄λ μκ°λ³΅μ‘λκ° λ λκ² λμ€λ κ² κ°λ€μ. μ΄ λΆλΆμ λν΄μλ μ μκ°μ΄ λ§λμ§ νΌλλ°±ν΄μ£Όμλ©΄ μ’μ κ² κ°μ΅λλ€. μ κ° μκ°ν΄λ³΄μ§ λͺ»νλ λ°©λ²μΈ κ² κ°μ΅λλ€. μ’μ λ°©λ² κ°μ¬ν©λλ€.π 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.
맨 μ²μ λ¬Έμ μ μ κ·Όνμμ λ μ€λͺ ν΄μ£Όμ λ°©λ²κ³Ό κ°μ λ°©λ²μΌλ‘ μλ μ½λλ₯Ό μμ±ν΄ 보μμ΅λλ€. def solution(n, works):
answer = 0
for i in range(n):
works.sort()
if works[-1]:
works[-1]-=1
return sum([x**2 for x in works]) λ§ κ·Έλλ‘ λ§€ μκ° μ€λ¦μ°¨μ νμ¬ μ΅λκ°μμ μ νλ λΆλΆμμλ λ§μ μ λ°μμΌλ ν¨μ¨μ±μμ λͺ¨λ μκ°μ΄κ³Όκ° λ¨λ κ²°κ³Όκ° μμμ΅λλ€. μ½λλ₯Ό μ λλ‘ νμ ν΄ μ£Όμ μ κ°μ¬ν©λλ€ π π π |
||
|
||
print(max_heap) | ||
answer = sum([x[1]**2 for x in max_heap]) | ||
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.
μ½λμμ μ΄ λΆλΆμ΄ μ μ΄ν΄κ° λμ§ μμμμ§λ§ μ€λͺ μ μ ν΄μ£Όμ μ Max Heapμ ꡬννλ λ°©λ²μ΄λΌλ κ²μ μ μ μ μμμ΅λλ€.
μ’μ μ€λͺ κ°μ¬ν©λλ€.π