Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions syheo/codingTest1/11-4-만들 수 없는 금액.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#CH11 그리디 기출
#예제 11-4
#만들 수 없는 금액

N = int(input())
coins = list(map(int,input().split()))
answer = 1
coins.sort()
for i in range(N):
if coins[i]<=answer:
answer+=coins[i]
else:
print(answer)


28 changes: 28 additions & 0 deletions syheo/codingTest1/11-5-볼링공 고르기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#CH11 그리디 기출
#예제 11-5
#볼링공 고르기

#N은 볼링공 갯수, M은 공의 최대 무게
N , M = map(int,input().split())

balls = list(map(int,input().split()))
balls.sort()
sum = 0
postSum = 0
curSum = 0
cur = 0
#현재 공 무게와 다른 이전 공의 갯수 * 현재 공 무게의 갯수를 sum에 더해나감
for ball in balls:
if ball!=cur:
cur = ball
sum+=curSum*postSum
postSum+=curSum
curSum=1
else:
curSum+=1
sum+=curSum*postSum
print(sum)




41 changes: 41 additions & 0 deletions syheo/codingTest1/11-6-무지의 먹방 라이브.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#CH11 그리디 기출
#예제 11-6
#무지의 먹방 라이브
#카카오 신입 공채 2019

#엄청 오래 걸림
#효율성 있게 하기 어려웠음.
import heapq
from collections import deque

def solution(food_times, k):
answer = 0
rest = k # 남은 시간
length = len(food_times)
#k 시간 안에 현재 음식을 다먹을 수 있을 경우
if sum(food_times)<=k:
return -1

q = []
#(식사시간, 음식 위치)를 우선순위 큐에 넣어줌 O(logN)xN
for i in range(length):
heapq.heappush(q,(food_times[i],i))

rotation = q[0][0] # 진행할 로테이션 횟수
prev_rot = 0
#큐에 음식이 있고, 로테이션 진행 가능시
while rest>=len(q)*(rotation-prev_rot):
#남은 횟수
rest-=(rotation-prev_rot)*len(q)
#rotation만큼 돌릴 수 있는 음식 제거
while q[0][0]==rotation:
heapq.heappop(q)
#로테이션 갱신
prev_rot = rotation
rotation = q[0][0]

#음식 번호로 정렬 O(NlogN)
q.sort(key=lambda x : x[1])
answer = q[rest%len(q)][1]+1

return answer