https://programmers.co.kr/learn/courses/30/lessons/72412
Code
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
32
from collections import defaultdict
from itertools import product
def solution(info, query):
data = defaultdict(list)
for *keys, val in [i.split() for i in info]:
for new_keys in product([keys[0], '-'],
[keys[1], '-'],
[keys[2], '-'],
[keys[3], '-']):
data[new_keys].append(int(val))
for key in data:
data[key].sort()
answer = []
for q in query:
*target_keys, target_val = q.replace('and', '').split()
target_val = int(target_val)
target_keys = tuple(target_keys)
vals = data[target_keys]
l = 0
r = len(vals)
while l < r:
m = (l+r)//2
if vals[m] >= target_val:
r = m
else:
l = m+1
answer.append(len(vals)-l)
return answer
Complexity
$O(nmlogn)$
- $n$:
|info|
- $m$:
|query|
PREVIOUSEtc