백준1931(파이썬) - 회의실 배정
resilient
·2021. 5. 6. 00:20
728x90
반응형
1931번: 회의실 배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
처음에 짰던 코드인데 시간초과도 나고 되게 어렵게 생각했던 코드이다.
n = int(input())
schedule = []
for _ in range(n):
start,end = map(int,input().split())
schedule.append([start,end])
dp = [0 for _ in range(n+1)]
schedule = sorted(schedule,key=lambda x : x[0])
for i in range(len(schedule)):
now = schedule[i][0]
_end = schedule[i][1]
for j in range(1,len(schedule)):
if _end<schedule[j][0]:
now = schedule[j][0]
_end = schedule[j][1]
dp[i] += 1
print(max(dp)+1)
이 코드에서 정렬을 한번 더해서 코드를 짰다.
import sys
input = sys.stdin.readline
n = int(input())
schedule = []
for _ in range(n):
start,end = map(int,input().split())
schedule.append([start,end])
schedule = sorted(schedule,key=lambda x : x[0])
schedule = sorted(schedule,key=lambda x : x[1]) #이게 핵심이다.
end_ = 0
cnt = 0
for start_, end in schedule:
if start_>=end_:
cnt += 1
end_ = end
print(cnt)
반응형
'자료구조 & 알고리즘 > 백준(Baekjoon)' 카테고리의 다른 글
백준7562(파이썬) - 나이트의 이동 (0) | 2021.05.16 |
---|---|
백준11727(파이썬) - 2 x n 타일링2 (0) | 2021.05.14 |
백준15686(파이썬) - 치킨 배달 (0) | 2021.05.04 |
백준2294(파이썬) - 동전2 (0) | 2021.05.04 |
백준 2293(파이썬) - 동전1 (0) | 2021.05.03 |