系统会向您查询。每个查询的形式为以下所述的两个整数:
1-:在数据结构中插入x。
2-:从数据结构中删除y的一次出现(如果存在)。
3-:检查是否存在频率为的整数。如果是,则打印1否则为0。
示例输入: 查询= [(1,1),(2,2),(3,2),(1,1),(1,1),(2,1),(3,2)]
这个问题很容易解释,我认为我有一个不错的解决方案
循环查询,并在字典中相应地增加和减少每个数字的频率 ...同时在一个单独的词典中跟踪另一个词典的每个键出现了多少次
在检查是否存在任何频率为QUERY 3的y整数时,您将检查第二个字典中y的计数是否存在...
我通过了大多数测试案例,但没有通过。这杀了我!
def freqQuery(queries):
frequency = {}
results = []
frequencyValues = {}
for query in queries:
q = query[0]
val = query[1]
if q == 1:
frequency[val] = frequency.get(val, 0) + 1
freq = frequency[val]
frequencyValues[freq] = frequencyValues.get(freq, 0) + 1
frequencyValues[freq-1] = frequencyValues.get(freq-1, 1) - 1
elif q == 2:
if val in frequency.keys():
frequency[val] += - 1
if frequency[val] < 0:
frequency[val] = 0
freq = frequency[val]
frequencyValues[freq + 1] = frequencyValues.get(freq + 1, 1) - 1
frequencyValues[freq] = frequencyValues.get(freq, 1) + 1
elif q == 3:
if val in frequencyValues.keys():
if frequencyValues[val] > 0:
results.append(1)
else:
results.append(0)
else:
results.append(0)
return results
答案 0 :(得分:2)
# --stackoverflow help fixing op code
# minor code refactor and your code passes all the test cases.
elif q == 2:
if val in frequency:
freq = frequency[val]
frequencyValues[freq] = frequencyValues.get(freq, 1) - 1
frequency[val] += - 1 # <---- decrement line
frequencyValues[freq-1] = frequencyValues.get(freq-1, 1) + 1
#--------------------
if frequency[val] < 0:
frequency[val] = 0
#--------------------
# this condition should have been checked at the end as after decrement line ( frequency[val] -= 1 ) value of frequency[val] can get negative
# also frequency[val] += - 1 ---> can be better written as frequency[val] -= 1
我的解决方案被所有tc接受
from collections import defaultdict
n = int(input())
a = defaultdict(int) # num:cnt
b = defaultdict(int) # cnt: how many nums have this cnt
for tc in range(n):
op, data = map(int, input().strip().split())
if op == 1:
# insert
b[a[data]] -= 1
a[data]+=1
b[a[data]] += 1
elif op == 2:
# delete
if data in a:
b[a[data]] -= 1
a[data] -= 1
b[a[data]] += 1
a[data] = 0 if a[data] < 0 else a[data]
else:
# check if any key in b = data and has count > 0
print('1' if data in b and b[data] > 0 else '0')
第一种方法,但我在4个测试用例中超时
from collections import defaultdict
n = int(input())
data_freq_dict = defaultdict(int)
for tc in range(n):
op, data = map(int, input().strip().split())
if op == 1:
# insert
data_freq_dict[data]+=1
elif op == 2:
# delete
if data in data_freq_dict:
data_freq_dict[data] -= 1
data_freq_dict[data] = 0 if data_freq_dict[data] < 0 else data_freq_dict[data]
else:
# check if any key in data_freq_dict has count = data
print('1' if data in set(data_freq_dict.values()) else '0')
答案 1 :(得分:1)
def freqQuery(queries):
arr={}
res=[]
for i,j in queries:
if i==1:
if j in arr:
arr[j]+=1
else:
arr[j]=1
elif i==2:
if j in arr:
if arr[j]>0:
arr[j]-=1
elif i==3:
if j in arr.values():
res.append(1)
else:
res.append(0)
return res
由于超时,只有一个测试用例在python3编译器中失败 所以我在pypy3中运行它,编译速度比python3 ...
注意:此处仅使用一个字典。我认为在最佳解决方案中,使用了2格。