인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

happydy08192331's profile image
happydy08192331

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

백준 10815번 list와 set차이

Resolved

Written on

·

469

·

Edited

0

안녕하세요, 선생님

항상 강의 잘 듣고 있습니다.

 


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

 

제가 백준을 푸는데, 이 10815번 문제가 list로 찾으면 시간 초과가 뜨고, set으로 바꿔서 찾으면 시간 초과가 안뜨더라고요..

set이 순서가 없고, 중복이 안된다는것은 알고있습니다.

하지만 왜 set으로 바꿔서 속도가 빨라지는건지 궁금합니다.

 

list를 사용한 코드 - 시간 초과

N = int(input())
a = list(map(int, input().split()))
M = int(input())
b = list(map(int, input().split()))

for i in b:
    if i in a:
        print(1)
    else:
        print(0)

 

set을 사용한 코드

N = int(input())
a = set(map(int, input().split()))
M = int(input())
b = set(map(int, input().split()))

for i in b:
    if i in a:
        print(1)
    else:
        print(0)

 

감사합니다.

코테 준비 같이 해요! 코딩-테스트python

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

리스트는 원소가 있는지 확인하는 in은 평균 시간복잡도가 O(n)이고,

셋은 in의 평균 시간복잡도가 O(1) 입니다. 셋은 해쉬를 쓰기 때문에 그렇습니다.

happydy08192331's profile image
happydy08192331

asked

Ask a question