Skip to content

Commit 05bd6ae

Browse files
committed
[BOJ] #1931. 회의실 배정 / 실버1 / 47분(힌트/성공)
1 parent 70f3db6 commit 05bd6ae

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed
Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
1-
def dfs(start, graph, visited, check_link):
2-
cnt = 1
3-
visited[start] = True # 방문 체크
4-
for v in graph[start]: # v에 연결되어 있는 다른 노드 탐색.
5-
if visited[v] == False and check_link[start][v] == True:
6-
# 연결되어 있지만 방문한 적 없다면 -> DFS 호출하고, 반환값을 cnt에 누적해서 더함
7-
cnt += dfs(v, graph, visited, check_link)
8-
return cnt
9-
10-
def solution(n, wires):
11-
answer = float("inf") # answer 초기화
12-
check_link = [[True]*(n+1) for _ in range(n+1)] # 끊은 간선인지 아닌지 체크하는 용도
13-
graph = [[] for _ in range(n+1)] # 송전탑 그래프
14-
15-
# 그래프 연결
16-
for u, v in wires:
17-
graph[u].append(v)
18-
graph[v].append(u)
19-
20-
# 전력망 나누기
21-
for a, b in wires:
22-
# 1. 간선 정보를 확인하면서 a, b 그룹에 연결된 송전탑 개수를 세기 위해서 a에서 b로 가는 간선을 끊어본다.
23-
visited = [False] * (n+1)
24-
check_link[a][b] = False # 양망향 연결 끊기
25-
# 2. a와 b에 붙어있는 송전탑 개수
26-
cnt_a = dfs(a, graph, visited, check_link) # a랑 붙어있는 송전탑 개수
27-
cnt_b = dfs(b, graph, visited, check_link) # b랑 붙어있는 송전탑 개수
28-
check_link[a][b] = True
29-
30-
answer = min(answer, abs(cnt_a - cnt_b)) # 송전탑 개수의 차이(절대값) 가장 작은 걸로 저장함
31-
32-
return answer
1+
def solution(n, lost, reserve):
2+
answer = 0
3+
# 집합을 이용해서 중복값 제거
4+
lost_set = set(lost) - set(reserve)
5+
reserve_set = set(reserve) - set(lost)
6+
7+
# 빌려줄 수 있는지 판단
8+
for can in list(reserve_set):
9+
if can-1 in lost_set:
10+
lost_set.remove(can-1)
11+
elif can+1 in lost_set:
12+
lost_set.remove(can+1)
13+
14+
# 정답은 전체 학생 수에서 잃어버린 학생 수를 빼는 것
15+
answer = n - len(list(lost_set))
16+
return answer
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n = int(input())
5+
meetings = [[0,0] for _ in range(n)]
6+
7+
for i in range(n):
8+
start, end = map(int, input().strip().split())
9+
meetings[i][0] = start
10+
meetings[i][1] = end
11+
12+
# 끝나는 시간을 기준으로 정렬, 끝나는 시간이 같으면 시작 시간을 기준으로 정렬
13+
meetings.sort(key=lambda x: (x[1], x[0]))
14+
15+
cnt = 0
16+
last_end_time = 0
17+
18+
for start, end in meetings:
19+
if start >= last_end_time:
20+
cnt += 1
21+
last_end_time = end
22+
23+
print(cnt)

0 commit comments

Comments
 (0)