Notice
Recent Posts
Recent Comments
Link
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 너비우선탐색
- 비지도학습
- Merge sort
- bineary search
- 머신러닝
- 자바
- 코테
- 파이썬
- 깊이우선탐색
- 파이썬 오류
- 스택과 힙
- 딕셔너리
- HTTP
- 딥러닝
- 백준
- 알고리즘
- BOJ
- 코딩테스트
- 코딩
- 캐싱
- 오버라이딩
- rest api
- 멱등
- 강화학습
- 이진탐색
- 해시
- 지도학습
- post
- 프로그래머스
- 파이썬 알고리즘
Archives
- Today
- Total
chae._.chae
[Algorithm] 백준 #10816 숫자 카드 2 본문
728x90
반응형
내 풀이
SOLVE1) 파이썬 defaultdict 이용
import sys
from collections import defaultdict
input = sys.stdin.readline
N = int(input())
nums = list(map(int, input().split()))
dict = defaultdict(int)
for i in nums:
dict[i] += 1
M = int(input())
targets = list(map(int, input().split()))
for i in targets:
print(dict[i], end=' ')
SOLVE2) Counter 모듈 사용
import sys
from collections import Counter
input = sys.stdin.readline
N = int(input())
nums = list(map(int, input().split()))
M = int(input())
targets = list(map(int, input().split()))
cnt = Counter(nums)
# print(cnt)
# Counter({10: 3, 3: 2, -10: 2, 6: 1, 2: 1, 7: 1})
for target in targets:
print(cnt[target], end=' ')
Counter 모듈 사용
- 여러 형태의 데이터를 인자로 받아서, 각 원소가 몇번씩 나오는지 저장된 객체를 얻는다.
- 데이터의 갯수를 셀때 유용
- 데이터의 갯수가 많은 순으로 졍렬된 배열을 리턴하는 most_common( )메서드 제공
- 안에 숫자 K인자로 넣으면, 가장 갯수가 많은 K개의 데이터 얻을 수 있다
from collections import Counter
counter = Counter("hello world")
>>> counter
Counter({'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
Counter('hello world').most_common()
[('l', 3), ('o', 2), ('h', 1), ('e', 1), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]
728x90
'파이썬 알고리즘 > BOJ' 카테고리의 다른 글
[Algorithm] 백준 #1205 등수 구하기 (0) | 2024.07.07 |
---|---|
[Algorithm] 백준 #20125 쿠키의 신체 측정 (0) | 2024.07.07 |
[Algorithm] 백준 DP #11053 가장 긴 증가하는 부분 수열 (0) | 2023.07.08 |
[Algorithm] 백준 #2178 미로탐색 (0) | 2023.06.25 |
[Algorithm] 백준 #2110 공유기 설치 (0) | 2023.06.25 |