Haribo ML, AI, MATH, Algorithm

순위 검색


순위 검색

코드

from collections import defaultdict
from bisect import insort, bisect_left
def solution(info, query):
    answer = []
    table = [x.split(' ') for x in info]
    queries = [x.replace('and ', '') for x in query]
    masks = [bin(x)[2:].zfill(4) for x in range(16)]
    group = defaultdict(list)

    for info_single in table :
        condition, score = info_single[:-1], info_single[-1]
        for mask in masks :
            key = [x if m == '1' else '-' for x, m in zip(condition, mask)]
            insort(group[' '.join(key)], int(score))

    for query in queries :
        score_index = query.rfind(' ')
        condition, score = query[:score_index], query[score_index+1:]

        scores = group[condition]
        answer.append(len(scores) - bisect_left(scores, int(score)))

    return answer

넘사벽 코드 출처

from functools import reduce
from collections import defaultdict
from bisect import insort, bisect_left

def solution(info, query):
    table = {"c": 3, "j": 5, "p": 6, "b": 6, "f": 5, "s": 6, "-": 0}
    conv = lambda l, t: (reduce(lambda a, k: (a << 3) + t(table[k[0]]), l[:-1], 0), int(l[-1]))
    info = list(map(lambda s: conv(s.split(" "), lambda x: 7 - x), info))
    query = list(map(lambda s: conv([c for c in s.split(" ") if c != "and"], lambda x: x), query))
    d = defaultdict(list)
    for k, v in info:
        insort(d[k], v)
    return [sum([len(l) - bisect_left(l, v) for k, l in d.items() if not k & q]) for q, v in query]


Similar Posts

이전 포스트 땅따먹기

다음 포스트 올바른 괄호

Comments