Dictionaries and Hashmaps: Frequency Queries
Key-value 관계를 정의할 때 dictionary를 사용한다. 풀려고 하는 문제가 item들에 대한 정보를 모두 가지고 있어야 한다면 list
와 같은 자료형을 사용할 수 있겠으나 출현빈도와 같은 경우에는 dictionary
를 사용해 효율적으로 처리할 수 있다.
Problem
You are given $q$ queries. Each query is one of the form two integers described below:
- $(1, x)$: Insert $x$ in your data structure.
- $(2, y)$: Delete one occurence of $y$ from your data structure, if present.
- $(3, z)$: Check if any integer is present whose frequency is exactly $z$. If yes, print 1 else 0.
The queries are given in the form of a 2-D array queries of size $q$ where queries[i][0]
contains the operation, and queries[i][1]
contains the data element.
Let’s assume following queries are given:
queries = [(1,1), (2,2), (3,2), (1,1), (1,1), (2,1), (3,2)]
Output of this query is [0,1]
from following operations:
|
|
Complete the freqQuery
function. freqQuery
has the following parameters:
int queries[q][2]
: a 2-d array of integers
It returns int[]
, the result of queries of type 3.
Answer
|
|
Explanation
Python의 built-in Counter
를 사용하는 방법도 있겠으나 이 문제에서는 list의 원소를 직접 관리할 필요는 없으므로 dict
를 활용해 처리하였다. Operation 중 1과 2는 dict.get
을 key가 없는 경우를 간단하게 처리해줄 수 있고 operation 3도 dict.values
를 사용해 존재 유무를 쉽게 파악할 수 있다. 다만, 이 문제에서 10, 11번 테스트가 time limit에 대한 처리를 필요로 하므로 약간의 트릭이 필요하다. Operation 3일 때 if value in query_dict.values()
에서 시간이 소요될 것임을 예상할 수 있다. 즉 가능하다면 이 operation을 수행하지 않는 것이 시간 단축에 도움이 될 것이다. 여기서는 간단하게 조회하려는 빈도수가 dictionary길이 전체보다 클 수는 없으므로 이 조건일 경우 continue
를 통해 시간을 단축하였고 결과적으로 모든 테스트에 통과하였다.