*문제 출처는 백준에 있습니다.
문제 제목: 약속 / 1183번 (실버 2단계)
문제 사이트: https://www.acmicpc.net/problem/1183
문제 설명

나의 풀이
def solution(n, wizard):
# 시간
times = []
for a, b in wizard:
# 시간을 뺀걸 사용
times.append(a - b)
# 정렬
times.sort()
if len(times) % 2 == 1:
return 1
else:
return abs(times[len(times) // 2] - times[(len(times) // 2) - 1] + 1)
# 메인 함수
def main():
# n명의 마법사
n = int(input())
# 마법사
wizard = []
for _ in range(n):
# 약속 시간 a, 도착 시간 b
a, b = map(int, input().split())
wizard.append([a, b])
print(solution(n, wizard))
if __name__=="__main__":
main()

※ 알아야 할 것

절대값 함수를 이용하여 홀수일 때는 가장 작아지는 부분이 하나 밖에 없고 짝수 일때는 정렬 후 중간 부분에서 +1를 더해주면 끝난다.
'Coding Test > 백준-Python' 카테고리의 다른 글
| 백준 / 적록색약 / 10026번 / Python (0) | 2025.02.11 |
|---|---|
| 백준 / D-Day / 1308번 / Python (2) | 2025.02.06 |
| 백준 / 복권 / 1359번 / Python (0) | 2025.02.04 |
| 백준 / 통계학 / 2108번 / Python (0) | 2025.02.03 |
| 백준 / 인구 이동 / 16234번 / Python (0) | 2025.01.31 |