Skip to content

Commit b4ecf2d

Browse files
authored
승연 책 숙제(4/12/월) (#60)
* 책 숙제 * 11-6 fix * 11-6 fix * 11-6 fix * fix * fix * 무지의 먹방 라이브 최종
1 parent 40c913a commit b4ecf2d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#CH11 그리디 기출
2+
#예제 11-4
3+
#만들 수 없는 금액
4+
5+
N = int(input())
6+
coins = list(map(int,input().split()))
7+
answer = 1
8+
coins.sort()
9+
for i in range(N):
10+
if coins[i]<=answer:
11+
answer+=coins[i]
12+
else:
13+
print(answer)
14+
15+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#CH11 그리디 기출
2+
#예제 11-5
3+
#볼링공 고르기
4+
5+
#N은 볼링공 갯수, M은 공의 최대 무게
6+
N , M = map(int,input().split())
7+
8+
balls = list(map(int,input().split()))
9+
balls.sort()
10+
sum = 0
11+
postSum = 0
12+
curSum = 0
13+
cur = 0
14+
#현재 공 무게와 다른 이전 공의 갯수 * 현재 공 무게의 갯수를 sum에 더해나감
15+
for ball in balls:
16+
if ball!=cur:
17+
cur = ball
18+
sum+=curSum*postSum
19+
postSum+=curSum
20+
curSum=1
21+
else:
22+
curSum+=1
23+
sum+=curSum*postSum
24+
print(sum)
25+
26+
27+
28+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#CH11 그리디 기출
2+
#예제 11-6
3+
#무지의 먹방 라이브
4+
#카카오 신입 공채 2019
5+
6+
#엄청 오래 걸림
7+
#효율성 있게 하기 어려웠음.
8+
import heapq
9+
from collections import deque
10+
11+
def solution(food_times, k):
12+
answer = 0
13+
rest = k # 남은 시간
14+
length = len(food_times)
15+
#k 시간 안에 현재 음식을 다먹을 수 있을 경우
16+
if sum(food_times)<=k:
17+
return -1
18+
19+
q = []
20+
#(식사시간, 음식 위치)를 우선순위 큐에 넣어줌 O(logN)xN
21+
for i in range(length):
22+
heapq.heappush(q,(food_times[i],i))
23+
24+
rotation = q[0][0] # 진행할 로테이션 횟수
25+
prev_rot = 0
26+
#큐에 음식이 있고, 로테이션 진행 가능시
27+
while rest>=len(q)*(rotation-prev_rot):
28+
#남은 횟수
29+
rest-=(rotation-prev_rot)*len(q)
30+
#rotation만큼 돌릴 수 있는 음식 제거
31+
while q[0][0]==rotation:
32+
heapq.heappop(q)
33+
#로테이션 갱신
34+
prev_rot = rotation
35+
rotation = q[0][0]
36+
37+
#음식 번호로 정렬 O(NlogN)
38+
q.sort(key=lambda x : x[1])
39+
answer = q[rest%len(q)][1]+1
40+
41+
return answer

0 commit comments

Comments
 (0)