(
, )
์ ๊ฐฏ์๊ฐ ์ง์ด๋ง๊ณ , ์ด๋ฆฐ๊ฒ ์๋๋ฐ ๋ซ๋ ๊ฒฝ์ฐ๋ฅผ ์ฒดํฌํฉ๋๋ค.
def solution(s) :
open_count = 0
for type in s :
if open_count <= 0 and type == ')' : return False
open_count += 1 if type == '(' else -1
return True and open_count == 0
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
def solution(land) :
for h in range(1, len(land)) :
for w in range(4) :
prev_layer = land[h-1]
land[h][w] += max(prev_layer[:w] + prev_layer[w+1:])
return max(land[-1])
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)