def solution(n = 78):
b = bin(n)[2:]
if (index := b.rfind('01')) != -1 :
b = b[:index] + '10' + b[index+2:][::-1]
else :
b = b[0]+'0'+ b[1:][::-1]
return int(b, 2)
๊ฐ์ฅ ํฐ ์ ์ฌ๊ฐํ ์ฐพ๊ธฐ
from itertools import chain
def solution(board) :
height = len(board)
width = len(board[0])
for h in range(1, height) :
for w in range(1, width) :
if board[h][w] != 0:
board[h][w] += min(board[h-1][w], board[h][w-1], board[h-1][w-1])
return max(chain(*board))**2
def solution(number, k):
num_len = len(number) - k
stack = []
for num in number :
stack.append(num)
while len(stack) >= 2 and k > 0 and stack[-1] > stack[-2] :
stack.pop(-2)
k -= 1
return ''.join(stack[:num_len])
def greedy_search(distance, cur_pos, moves, change_count):
if moves >= len(distance): return
change_at_current = distance[cur_pos]
distance[cur_pos] = 0
if sum(distance) == 0:
answer.append(moves + change_count + change_at_current)
greedy_search(distance.copy(), cur_pos + 1, moves + 1, change_count + change_at_current)
greedy_search(distance.copy(), cur_pos - 1, moves + 1, change_count + change_at_current)
def solution(name):
global answer
answer = []
distance = [min(ord(char) - ord('A'), 26 - (ord(char) - ord('A'))) for char in name]
greedy_search(distance, 0, 0, 0)
return min(answer)
def solution(phoneBook):
phoneBook.sort()
for p1, p2 in zip(phoneBook, phoneBook[1:]):
if p2.startswith(p1):
return False
return True
from collections import Counter
from functools import reduce
from operator import mul
def solution(clothes) :
closet = Counter(kind for name, kind in clothes)
total_case = reduce(mul, [c + 1 for c in closet.values()], 1) - 1
return total_case
from collections import Counter
from itertools import combinations
def solution(orders, course) :
result = []
for course_num in course :
counter = Counter()
for order in orders:
counter += Counter(combinations(sorted(order), course_num))
counter = counter.most_common()
result.extend(''.join(key) for key, val in counter if val > 1 and val == counter[0][1])
return sorted(result)
from collections import deque
def solution(people, limit):
people = deque(sorted(people))
answer = 0
while people :
first = people.pop()
if people and first + people[0] <= limit :
people.popleft()
answer += 1
return answer
def solution(numbers):
str_numbers = list(map(str, numbers))
str_numbers.sort(key=lambda x: x*3, reverse=True)
result = ''.join(str_numbers)
return str(int(result))
def solution(citations):
citations.sort(reverse = True)
for i, citation in enumerate(citations) :
if citation <= i :
return i
return len(citations)