문제: 1931번 회의실 배정

Sungmin·2023년 3월 27일
0

https://www.acmicpc.net/problem/1931


Solution

import sys
input = sys.stdin.readline
n = int(input())
arr = []
for i in range(n):
    s, e = map(int, input().split())
    arr.append((s, e))

arr.sort(key = lambda x : (x[1], x[0]))

n = 0
cnt = 0

for i, j in arr:
    if n <= i:
        n = j
        cnt += 1
print(cnt)

배운점

정렬만 잘 하면 간단하게 풀리는 문제였다. 처음엔

arr.sort(key = lambda x : x[1])
arr.sort(key = lambda x : x[0])

이렇게 정렬하였더니 첫줄은 무시되고 두번째 줄만 적용된 정렬이 만들어졌다.
그래서 한번에 정렬하는 방법을 찾아보니

arr.sort(key = lambda x : (x[1], x[0]))

key에 튜플로 여러 인자를 주면 해당 인자의 순서대로 정렬이 된다는 것을 알게 되었다.

profile
Let's Coding

0개의 댓글