173. 강의실 배정
1) 어떤 전략(알고리즘)으로 해결?
- 강의실 배정은
=> 어떤 아이의 종료 시간이 내 시작 시간이랑 같거나 보다 클 때 그 방 이어서 쓸 수 있음,
2) 코딩 설명
<내 풀이>
import sys
import heapq
n = int(sys.stdin.readline().rstrip())
cnt = 0
heap=[]
for i in range(n) :
s,e = (map(int, sys.stdin.readline().rstrip().split()))
heapq.heappush(heap, (s,e))
room=[]
for j in range(n) :
s,e = heapq.heappop(heap)
if not room : heapq.heappush(room, e)
elif s >= room[0] :
heapq.heappop(room)
heapq.heappush(room, e)
else :
heapq.heappush(room, e)
print(len(room))
<내 틀렸던 풀이, 문제점>
84%까지 갔는데 시간초과~
import sys
import heapq
n = int(sys.stdin.readline().rstrip())
cnt = 0
heap=[]
for i in range(n) :
s,t = (map(int, sys.stdin.readline().rstrip().split()))
heapq.heappush(heap, (-t,-s))
room=[]
for j in range(n) :
(s,e)= heapq.heappop(heap)
s=-s
e=-e
if not room :
room.append(e)
else :
for k in range(len(room)) :
if room[k]>=s:
room[k]=e
break
else : room.append(e)
print(len(room))
- 이중 for문 돌리는 부분 또한 (room 탐색 부분) 우선순위 큐로 바꿔야 할 듯
import sys
import heapq
n = int(sys.stdin.readline().rstrip())
cnt = 0
heap=[]
for i in range(n) :
s,t = (map(int, sys.stdin.readline().rstrip().split()))
heapq.heappush(heap, (-t,-s))
room=[]
for j in range(n) :
(e,s)= heapq.heappop(heap)
s=-s
e=-e
if not room :
heapq.heappush(room, -s)
else :
for k in range(len(room)) :
if -room[k]>=e:
room[k]=-s
break
else : heapq.heappush(room, -s)
print(len(room))
- 마찬가지 시간초과 ㅋ (바뀐게 없으니,, 당욘)
import sys
import heapq
n = int(sys.stdin.readline().rstrip())
cnt = 0
heap=[]
for i in range(n) :
s,t = (map(int, sys.stdin.readline().rstrip().split()))
heapq.heappush(heap, (-t,-s))
room=[]
for j in range(n) :
(e,s)= heapq.heappop(heap)
e=-e
if not room :
heapq.heappush(room, s)
else :
for k in range(len(room)) :
if -room[k]>=e:
room.pop(k)
heapq.heappush(room, s)
break
else : heapq.heappush(room, s)
print(len(room))
- room을 힙으로 바꾸어보았는데도, 여전히 시간초과!
- 당연히 이중 for문 문제
for j in range(n) :
(e,s)= heapq.heappop(heap)
e=-e
if not room :
heapq.heappush(room, s)
else :
for k in range(len(room)) :
if -room[k]>=e:
<반성 점>
<배운 점>