728x90
https://www.acmicpc.net/problem/11723
- 생각
- 시간초과가 계속 난 문제..
- sys, pypy, 조건 합치기
- 추가 조건: add, toggle일 때 요소가 없다면
- 삭제 조건: remove, toggle일 때 요소가 있다면
- keyError가 남
- 원인 : set.remove(x)를 실행할 때 set에 x가 존재하지 않음
- 해결 : remove 대신 discard
- 시간초과가 계속 난 문제..
- 코드
from sys import stdin
# 수행해야 하는 연산의 수
M = int(input())
S = set()
for _ in range(M):
wordNum = stdin.readline().split()
# 추가
if wordNum[0] == 'add' or (wordNum[0] == 'toggle' and int(wordNum[1]) not in S):
S.add(int(wordNum[1]))
# 삭제
elif wordNum[0] == 'remove' or (wordNum[0] == 'toggle' and int(wordNum[1]) in S):
S.discard(int(wordNum[1]))
# 체크
elif wordNum[0] == 'check':
if int(wordNum[1]) in S:
print(1)
else:
print(0)
# 집합 all
elif wordNum[0] == 'all':
S = {1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20}
# 공집합
elif wordNum[0] == 'empty':
S = set()
728x90
반응형
'TIL - 프로그래밍 > Python 알고리즘' 카테고리의 다른 글
[백준] 4949. 균형잡힌 세상 - Python (0) | 2022.06.13 |
---|---|
[백준] 7586. 덩치 - Python (0) | 2022.06.12 |
[백준] 1764. 듣보잡 - Python (0) | 2022.06.11 |
[백준] 10773. 제로 - Python (0) | 2022.06.10 |
[백준] 2839. 설탕 배달 - Python (0) | 2022.06.09 |
댓글