나의 답안

def task(str):
    tmp = []
    for i in range(len(str) - 1):
        if str[i].isalpha() and str[i + 1].isalpha():
            tmp.append(str[i] + str[i + 1])
    return tmp

def solution(str1, str2):
    str1 = str1.upper()
    str2 = str2.upper()
    a, b = task(str1), task(str2)
    c = set(a) & set(b)
    c2 = 0
    for i in c:
        c2 += min(a.count(i), b.count(i))

    d = set(a) | set(b)
    d2 = 0
    for i in d:
        d2 += max(a.count(i), b.count(i))

    if any(d):
        return int((c2 / d2) * 65536)
    else:
        return 65536

최초 코드 - 시간 초과

def solution(n, left, right):
    answer = [[] for _ in range(n)]
    
    for i in range(n):
        for j in range(i + 1):
            answer[i].append(i + 1)
        for k in range(i + 1, n):
            answer[i].append(k + 1)

    result = []
    for i in answer:
        result = result + i
    
    return result[left:right + 1]

 

정답 코드

def solution(n, left, right):
    answer = []
    for i in range(left, right + 1):
        answer.append(max(i // n, i % n) + 1)
    return answer

나의 풀이

def solution(s):
    data = []
    tmp = []
    cur = ""

    for i in s:
        if i.isdigit():
            cur = cur + i

        if i == ',' and any(cur):
            tmp.append(cur)
            cur = ""

        elif i == '}':
            if any(cur):
                tmp.append(cur)
                cur = ""
            if any(tmp):
                data.append(tmp)
                tmp = []

    data.sort(key=lambda x:len(x))
    data = [set(i) for i in data]

    result = [data[0]]
    for i in range(1, len(data)):
        result.append(data[i] - data[i - 1])
    result = [int(list(i)[0]) for i in result]
    return result

 

다른 풀이

def solution(s):
    answer = []

    s1 = s.lstrip('{').rstrip('}').split('},{')

    new_s = []
    for i in s1:
        new_s.append(i.split(','))

    new_s.sort(key = len)

    for i in new_s:
        for j in range(len(i)):
            if int(i[j]) not in answer:
                answer.append(int(i[j]))

    return answer

 

strip으로 {, }을 처리 해줄 수도 있다.

나의 풀이

def solution(clothes):
    answer = 1
    data = {}
    for i, j in clothes:
        data[j] = data.get(j, 0) + 1

    for i in data:
        answer *= data[i] + 1

    return answer - 1

 

파이썬 dictionary의 .get 함수를 사용하였다.

 

data.get(x, y)

딕셔너리 data에서 x 값을 가져오되, 없다면 y를 반환(default value)

+ Recent posts