소스코드 (시간초과가 났지만 맞는..)
M = int(input())
S = [0]*21
for i in range (M):
command = input().split()
if command[0] == 'add':
S[int(command[1])] = 1
if command[0] == 'remove':
S[int(command[1])] = 0
if command[0] == 'check':
print(S[int(command[1])])
if command[0] == 'toggle':
S[int(command[1])] = ~ S[int(command[1])]+2
if command[0] == 'all':
S = [1]*21
if command[0] == 'empty':
S = [0]*21
알고리즘
요즘 DP 문제를 풀다보니 거기에 꽂혀서 풀었는데 시간초과가 나버렸따..
그래서 집합 문제니깐 파이썬에 있는 set()을 활용하기로 했다.
소스코드
M = int(input())
S = set()
for _ in range(M):
temp = input().split()
if temp[0] == "all":
S = set([i for i in range(1, 21)])
elif temp[0] == 'empty':
S = set()
else:
func, x = temp[0], temp[1]
x = int(x)
if func == "add":
S.add(x)
elif func == "remove":
S.discard(x)
elif func == "check":
print(1 if x in S else 0)
elif func == "toggle":
if x in S:
S.discard(x)
else:
S.add(x)
알고리즘
간단한 집합 연산을 가지고 풀 수 있는 문제이다.
백준 가보면 정답률이 굉장히 낮은데 왜 낮은지는 잘 모르겠다.
'Coding test' 카테고리의 다른 글
[백준/12869/파이썬] 뮤탈리스크 (0) | 2023.01.13 |
---|---|
[백준/6603/파이썬] 로또 (0) | 2023.01.12 |
[백준/10972/파이썬] 다음 순열 (0) | 2023.01.10 |
[백준/9095/파이썬] 1,2,3 더하기 - DP (0) | 2023.01.09 |
[백준/15649/파이썬]N과M(1) - 백트래킹 (0) | 2023.01.08 |