가오리의 코딩일기

[10816] 숫자카드2 본문

Python/백준

[10816] 숫자카드2

류경혜 2022. 8. 21. 22:30

bisect: 파이썬 내장 이진탐색 모듈 
bisect_left(literable, value): 왼쪽 인덱스 구하기
bisect_right(literable, value): 오른쪽 인덱스 구하기
from bisect import bisect_left, bisect_right
n = int(input())
card = sorted(map(int, input().split()))
m = int(input())
compare = list(map(int, input().split()))


def count_by_range(array, leftValue, rightValue):
    rightIndex = bisect_right(array, rightValue)
    leftIndex = bisect_left(array, leftValue)
    return rightIndex - leftIndex


for i in range(len(compare)):
    print(count_by_range(card, compare[i], compare[i]), end=' ')

'Python > 백준' 카테고리의 다른 글

[11725] 트리의 부모 찾기  (0) 2022.08.22
[1780] 종이의 개수  (0) 2022.08.21
[1991] 트리 순회  (0) 2022.08.21
[2331] 반복수열  (0) 2022.08.21
[2178] 미로 탐색  (0) 2022.08.21