<문제 링크>
https://www.acmicpc.net/problem/1931
<정답 코드>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import sys
input = sys.stdin.readline
n = int(input())
schedule = []
for _ in range(n):
x, y = map(int, input().split())
schedule.append((x, y))
# schedule을 끝나는 시간 기준으로 정렬
schedule.sort(key=lambda x:(x[1], x[0]))
last_ending_time = 0
cnt = 0
for start, end in schedule:
# 시작 시간이 방금 회의 끝나는 시간 이후면
if start >= last_ending_time:
cnt += 1
last_ending_time = end
print(cnt)
|
cs |
🔷 쌍으로 구성된 리스트 요소들을 두번째 값 기준으로 정렬하는 방법
: lambda식 이용
line12) schedule.sort(key=lambda x:(x[1], x[0]))
schedule 배열은 (시작시간, 종료시간)을 의미하는 튜플로 구성되어있음
-> lambda 함수로 (종료시간, 시작시간) 반환
-> 이 반환값을 대상으로 sort 함수로 정렬 실행
=> schedule 배열의 요소들이 두번째 인자(종료시간) 기준으로 정렬됨
'프로그래밍 > Python' 카테고리의 다른 글
백준_python 1269번 대칭 차집합 (0) | 2024.06.05 |
---|---|
파이썬_zip() 함수 (0) | 2024.06.04 |
백준_python 11726번 2×n 타일링(DP) (0) | 2024.05.27 |
itertools(permutations, combinations, product) 정리 (0) | 2024.05.27 |
백준_python 16953번 A → B (BFS, 그래프 깊이) (1) | 2024.05.20 |