def utopianTree(n):
    # Write your code here
    init_height = 1
    
    for i in range(n):
        if (i + 1) % 2:
            init_height *= 2
        else:
            init_height += 1
            
    return init_height
import string

def designerPdfViewer(h, word):
    # Write your code here
    alphabet_list = list(string.ascii_lowercase)
    
    max_idx = -1
    for i in word:
        if max_idx < h[alphabet_list.index(i)]:
            max_idx = h[alphabet_list.index(i)]
            
    return max_idx * len(word)
def hurdleRace(k, height):
    # Write your code here
    return 0 if max(height) < k else max(height) - k

binary search는 동일하게, 같은 숫자가 나온 시점에서(예를 들어, 30이 있는 배열에서 30을 삽입하고 싶을때) 왼쪽으로 갈지(start == mid), 오른쪽으로 갈지(mid + 1 == end)의 차이

1. bisect_right

def climbingLeaderboard(ranked, player):
    # Write your code here
    def bisect_right(arr, value, start, end):
        if start >= end:
            return start
        
        mid = start + (end - start) // 2
        
        if value < arr[mid]:
            return bisect_right(arr, value, start, mid)
        else:
            return bisect_right(arr, value, mid + 1, end)
        
    ranked = sorted(list(set(ranked)))
    
    ranks = []
    len_ = len(ranked)
    for score in player:
        rank = len_ - bisect_right(ranked, score, 0, len_) + 1
        ranks.append(rank)
        
    return ranks

arr = [10, 20, 30, 40, 40, 50, 60, 70] 일때,

# mid, 선택된 arr
4 [10, 20, 30, 40, 40, 50, 60, 70]
6 [50, 60, 70]
5 [50]
5

2. bisect_left

def bisect_left(arr, value, start, end):
    if start >= end:
        return start

    mid = start + (end - start) // 2

    if arr[mid] < value:
        return bisect_left(arr, value, mid + 1, end)
    else:
        return bisect_left(arr, value, start, mid)

arr은 동일,

# mid, 선택된 arr
4 [10, 20, 30, 40, 40, 50, 60, 70]
2 [10, 20, 30, 40]
3 [40]
3

단순

def pickingNumbers(a):    
    m = [0] * 100    
    
    for num in a:
        m[num] += 1

    cnt = 0    
    for i in range(1,n):        
        cnt = max(cnt, m[i - 1] + m[i])
        
    return maxval

복잡

def pickingNumbers(a):
    # Write your code here
    _a = list(set(sorted(a)))
    cnt = -1
    
    subsets = []
    for i in range(len(_a)):
        temp = _a[i]
        subset = [temp]
        
        for j in range(i + 1, len(_a)):
            if abs(temp - _a[j]) <= 1:
                subset.append(_a[j])
                
        subsets.append(subset)
        
    for subset in subsets:
        _sum = 0
        
        for v in subset:
            _sum += a.count(v)
            
        cnt = max(cnt, _sum)
        
    return cnt