-
Notifications
You must be signed in to change notification settings - Fork 2
65-9kyo-hwang #224
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
65-9kyo-hwang #224
Conversation
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.
๋ฌธ์ ๋ค ํ๊ณ , ๊ตํฉ๋ ์ฝ๋ ๋ณด๋ round ๋ณ์๋ช ์ด ๋ ๋ช ํํด์ ๋ฐ๋ก ๋ ๋ค ์์ ํ์ต๋๋ค ใ ใ ใ ใ
import heapq
def solution(n, k, enemy):
answer = len(enemy)
pq = []
my = n
card = k
for round in range(len(enemy)):
my -= enemy[round]
heapq.heappush(pq, -enemy[round])
if my < 0:
if card > 0:
my += heapq.heappop(pq) * -1
card -= 1
else :
return round
return answer
gjsk132
left a comment
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.
์ค๋๋ง์ ๋ณด๋ ์ต๋ํ ์ฅ์ญ~
from heapq import *
def solution(n, k, enemy):
round = 0
fight = []
for e in enemy:
round += 1
n -= e
heappush(fight, -e)
while n < 0 and k > 0:
k -= 1
n -= heappop(fight)
if n < 0 :
round -= 1
break
return round
๐ ๋ฌธ์ ๋งํฌ
๋ํ์ค ๊ฒ์
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ
โจ ์๋ ์ฝ๋
์ต๋$1,000,000$ ๋ผ์ด๋์ ๊ฑธ์ณ ์ ๋ค์ด ๋ค์ด์จ๋ค. ๊ทธ๋ฆฌ๊ณ ์ต๋ $500,000$ ๋ผ์ด๋๋ฅผ
๋ฌด์ ๊ถ์ ์ฌ์ฉํด ๋ณ์ฌ ์๋ชจ๋ฅผ 0์ผ๋ก ๋ง๋ค ์ ์๋ค.๋ฐฑํธ๋ํน ๋ฑ์ ์ด์ฉํด ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๊ธฐ์๋ ์ซ์๊ฐ ๋๋ฌด ํฌ๋ค. ๋ผ์ด๋๊ฐ ์ต๋$1,000,000$ ์ด๊ธฐ ๋๋ฌธ์ $O(N^2)$ ํ์ด๋ฒ๋ ๋ถ๊ฐ๋ฅํ๋ค. ์ฆ $O(N logN)$ ํ์ด๋ฒ์ ์ฌ์ฉํด์ผ ํ๋ค.
์ง๊ด์ ์ผ๋ก ์๊ฐํ์ ๋๋, ๋ฌด์ ๊ถ์ ๊ฐ๋ฅํ "์ ์ ์๊ฐ ๋ง์ ๋ผ์ด๋"์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค. ๊ทธ๋ฌ๋ ์ด๋ฌํ ๋ฐฉ๋ฒ์ "์ ํํ์"์ผ๋ก ๋ถ๊ฐ๋ฅํ๊ณ , ๊ทธ๋ ๋ค๊ณ ์์์ ์ธ๊ธํ ๊ฒ์ฒ๋ผ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ํ์ธํ ์๋ ์๋ค.
๋ฐ๋ผ์ ์ฌ๊ธฐ์๋ ์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํ๋ค. ์๋ฆฌ๋ ๊ฐ๋จํ๋ค.
i. ๋ง์ฝ ๋ ์ด์ ๋ฌด์ ๊ถ์ ์ฌ์ฉํ ์ ์๋ค๋ฉด ๋ผ์ด๋๋ฅผ ์ค๋จํ๋ค.
ii. ์๋๋ผ๋ฉด MaxHeap์์ ์ง๋ ๋ผ์ด๋ ์ค ๊ฐ์ฅ ์ ์ด ๋ง์๋ ๋ผ์ด๋์ ์ ์๋ฅผ popํด ๊ทธ ์ ๋งํผ ๋ณ์ฌ ์๋ฅผ ๋ณต์์ํค๊ณ ๋จ์ ๋ฌด์ ๊ถ ํ์๋ฅผ 1 ์ฐจ๊ฐ์ํจ๋ค.
์ด๋ฐ ์์ผ๋ก MaxHeap์ ์ฌ์ฉํ๋ฉด$O(logN)$ ์๊ฐ๋ณต์ก๋๋ก ์ง๋ ๋ผ์ด๋ ์ค ๊ฐ์ฅ ์ ์ด ๋ง์ ๋ผ์ด๋ ์ ๋ณด๋ฅผ ์ป์ด์ฌ ์ ์๊ฒ ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ค์ผ ๊ทธ๋ฆฌ๋ ์ ํ์ ์ํ๋ฆฌ๋์ง ๋ชจ๋ฅด๊ฒ ๋ค.
์์ ์๋ ์ฝํ ๋ณผ ๋ ๊ทธ๋ฆฌ๋/์ฐ์ ์์ํ ์ฐ๋ ๋ฌธ์ ๋ชป ํ์์๋๋ฐ, ์ด๊ฒ๋ ํ์ฐธ ์ฝ์งํ๋ค๊ฐ ๊ฒจ์ฐ ํด๊ฒฐํ๋ค ํ ...