나의 코드

def check(data):
    tmp = []
    for i in data:
        if i in ['(', '{', '[']:
            tmp.append(i)

        else:
            if tmp:
                if i == ')' and tmp[-1] == '(':
                    tmp.pop()

                elif i == '}' and tmp[-1] == '{':
                    tmp.pop()

                elif i == ']' and tmp[-1] == '[':
                    tmp.pop()
            else:
                return False

    return False if any(tmp) else True

def solution(s):
    answer = 0
    for i in range(len(s)):
        if check(s[i + 1:] + s[:i + 1]):
            answer += 1
    return answer

나의 코드

def solution(cacheSize, cities):
    answer = 0
    data = []

    if cacheSize == 0:
        return len(cities) * 5

    for i in cities: 
        i = i.lower()
        if i not in data:
            if len(data) == cacheSize:
                data.pop(0)
            data.append(i)
            answer += 5

        else:
            data.pop(data.index(i))
            data.append(i)
            answer += 1

    return answer

 

다른 코드

def solution(cacheSize, cities):
    import collections
    cache = collections.deque(maxlen=cacheSize)
    time = 0
    for i in cities:
        s = i.lower()
        if s in cache:
            cache.remove(s)
            cache.append(s)
            time += 1
        else:
            cache.append(s)
            time += 5
    return time

deque의 maxlen 인자를 이용

def solution(n):
    
    if n < 3:
        return n
    
    dp = [0] * (n + 1)
    dp[1], dp[2] = 1, 2
    
    for i in range(3, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
        
    return dp[n] % 1234567

나의 풀이

def solution(n,a,b):
    answer = 0
    while a != b:
        answer += 1
        a, b = (a + 1) // 2, (b + 1) // 2
    return answer

 

다른 풀이

def solution(n):
	return bin(n).count(1)

+ Recent posts