728x90
문제
숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 가지고 있는지 아닌지를 구하는 프로그램을 작성하시오.
풀이
- 시간복잡도 O(N)의 형태로 풀게 되면 시간 초과
- 이분 탐색 알고리즘을 사용해서 풀어야 하거나, 시간 복잡도가 \( O(log 2^N)) \
https://satisfactoryplace.tistory.com/39 - Dictionary 자료형을 이용하면 시간 단축이 가능해서 / 이분 탐색 알고리즘을 사용 할 필요가 없음.
https://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table
[알고리즘] 이분 탐색(Binaray Search)
이분 탐색(Binary Search) 정렬되어 있는(이분 탐색의 주요 조건) 배열에서 데이터를 찾으려 시도할 때, 순차탐색처럼 처음부터 끝까지 하나씩 모든 데이터를 체크하여 값을 찾는 것이 아니라 탐색
satisfactoryplace.tistory.com
Python: List vs Dict for look up table
I have about 10million values that I need to put in some type of look up table, so I was wondering which would be more efficient a list or dict? I know you can do something like this for both: if
stackoverflow.com
import sys
if __name__ == '__main__':
M = int(sys.stdin.readline())
data = list(map(int, sys.stdin.readline().split()))
M2 = int(sys.stdin.readline())
data2 = list(map(int, sys.stdin.readline().split()))
dict1 = {data2[i] : 0 for i in range(len(data2))}
for i in range(M):
if data[i] in dict1.keys():
dict1[data[i]] += 1
print(" ".join(map(str, list(dict1.values())))) # 출력 방식 습득:컴프리핸션을 사용할 필요가 없음.
'코드연습 > BOJ' 카테고리의 다른 글
BOJ 1764 : 듣보잡 (0) | 2022.06.16 |
---|---|
BOJ 2477 : 참외밭 (0) | 2022.06.08 |
BOJ 2004 : 조합 0의 개수 (0) | 2022.05.09 |
BOJ 2981 : 검문 (0) | 2022.04.28 |
BOJ 2609 : 최대공약수와 최소공배수 (0) | 2022.04.27 |