개발일지/TIL
TIL 23-06-06 백준 - 3의 배수
sdoram
2023. 6. 6. 21:54
1. 백준 - 3의 배수
문제점
https://www.acmicpc.net/problem/1769
시도해 본 것들
X = [int(n) for n in input()]
COUNT = 0
while len(X) > 1:
X = [int(n) for n in str(sum(X))]
COUNT += 1
print(COUNT)
print("YES" if X[0] % 3 == 0 else "NO")
해결 방법
X = input()
COUNT = 0
while len(X) > 1:
X = str(sum([int(y) for y in X]))
COUNT += 1
print(COUNT, "YES" if int(X) % 3 == 0 else "NO", sep="\n")
188ms
X = input()
COUNT = 0
while len(X) > 1:
X = str(sum(map(int, list(X))))
COUNT += 1
print(COUNT, "YES" if int(X) % 3 == 0 else "NO", sep="\n")
160ms
알게 된 점
list comprehension보다 map을 사용하는 게 조금 더 빠른 것을 확인할 수 있었다.
https://github.com/sdoram/algorithm_solving_process/commit/585ef842762026a4a4ac4a815fcc81200c6ed9e3
map을 활용한 시간복잡도 개선 · sdoram/algorithm_solving_process@585ef84
list comprehension도 시간 복잡도가 괜찮은 편이지만, 사용 가능한 경우 map을 적극적으로 활용하자
github.com