๋ค์ ์ธ์์ . ์ฐธ๊ณ ใดใด
import sys
sys.setrecursionlimit(400000)
from collections import defaultdict
def set_depth_subtree(node, prev) :
sub_trees, depths = [], []
for child in graph[node] :
if child == prev : continue
set_depth_subtree(child, node)
sub_trees.append((sub_tree[child], child))
depths.append((depth[child], child))
depth[node] = max(depth[node], depth[child] + 1)
sub_trees.sort(reverse=True)
depths.sort(reverse=True)
if len(sub_trees) >= 2 :
if sub_trees[0][1] != depths[0][1] : sub_tree[node] = sub_trees[0][0] + depths[0][0] + 1
else : sub_tree[node] = max(sub_trees[0][0] + depths[1][0] + 1, sub_trees[1][0]+depths[0][0] + 1)
elif len(sub_trees) == 1 : sub_tree[node] = sub_trees[0][0] + 1
def DFS(node, prev, height) :
global answer
sub_trees, depths = [], []
for child in graph[node] :
if child == prev : continue
sub_trees.append((sub_tree[child], child))
depths.append((depth[child], child))
sub_trees.sort(reverse=True)
depths.sort(reverse=True)
for child in graph[node] :
if child == prev : continue
next_height = height + 1
if depths[0][1] != child :
next_height = max(next_height, depths[0][0] + 2)
elif len(depths) >= 2 and depths[0][1] == child :
next_height = max(next_height, depths[1][0] + 2)
DFS(child, node, next_height)
if (len(sub_trees) >= 3) :
answer = max(answer,
sub_trees[0][0]+sub_trees[1][0]+height,
sub_trees[0][0]+sub_trees[2][0]+depths[1][0]+1,
sub_trees[0][0]+sub_trees[1][0]+depths[2][0]+1)
elif (len(sub_trees) == 2) :
answer = max(answer, sub_trees[0][0]+sub_trees[1][0]+height)
def solution(t):
global answer, N, graph, sub_tree, depth
N = len(t) + 1
graph = defaultdict(list)
sub_tree, depth = [1]*N, [1]*N
answer = 0
for v1, v2 in t:
graph[v1].append(v2)
graph[v2].append(v1)
set_depth_subtree(0, -1)
DFS(0, -1, 1)
return answer
๊ณ์ ์๊ฐ์ด๊ณผ๊ฐ ๋จ๊ธธ๋ list๋ง๊ณ dictionary๋ฅผ ์ฐ๋ ํ๋ฌดํ๊ฒ ๋ฐ๋ก ํต๊ณผ๋์์ต๋๋ค. ์ ์ ์ธ ์๋ฃ๊ตฌ์กฐ๋ list๋ฅผ ์ฐ๊ณ ๋์ ์ธ ์๋ฃ๊ตฌ์กฐ๋ dictionary๋ฅผ ์ฐ๋๊ฒ์ด ํจ์ฌ ์ด๋์ธ๊ฒ ๊ฐ์ต๋๋ค.
from collections import defaultdict
def add(x, y) :
return [(x[0]+y[0]/2, x[1]+y[1]/2), (x[0]+y[0], x[1]+y[1])]
def check(point, next_, arrow) :
return next_ in points and arrow not in points[point] and (arrow+4)%8 not in points[next_]
def solution(arrows):
global points
answer = 0
move = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
points = defaultdict(set)
point = (0, 0)
for arrow in arrows :
for next_ in add(point, move[arrow]) :
answer += 1 if check(point, next_, arrow) else 0
points[point].add(arrow)
point = next_
return answer
์ ํํ๊ฒ ๊ธฐ์ต์ ๋์ง ์์ง๋ง ์นด์นด์ค 2020 ๊ฒจ์ธ ์ธํด ์ฝ๋ฉํ ์คํธ์ ํธ์ง๊ฑฐ๋ฆฌ์ ๊ดํ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค.
ํธ์ง๊ฑฐ๋ฆฌ๋ ๋ ๋จ์ด์ ์ ์ฌ๋์ ํฌ๊ธฐ๋ฅผ ์ธก์ ํ๋ ๋จ์์ ๋๋ค. ์ ์์์ด์ง๋ง ๊ฒ์์์ง์ ์๋ชป๋ ๋จ์ด๋ฅผ ๊ฒ์ ํ์ ๋, ๋จ์ด๋ฅผ ์ถ์ฒํด์ฃผ๋ ์๊ณ ๋ฆฌ์ฆ์ด ๋ฐ๋ก ํธ์ง๊ฑฐ๋ฆฌ ์๊ณ ๋ฆฌ์ฆ์ด ์๋๊น ์๊ฐํฉ๋๋ค.

๋ ๋จ์ด economy ์ yummy ๋ฅผ ํ๋ฒ ๋ณด๊ฒ ์ต๋๋ค. ๋ ๋จ์ด๋ ๊ธธ์ด๋ ํ๋ฆฌ๊ณ , ์์ ์ํ๋ฒณ๋ ํ๋ฆฝ๋๋ค.
๋ธ๋ก ์ด๋ํ๊ธฐ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
import numpy as np
from collections import deque
def sort_(state) : return sorted(state, key=lambda x : (x[0], x[1]))
def add(x, y) : return (x[0]+y[0], x[1]+y[1])
def check(head, tail) : return Board[head[0], head[1]] == 0 and Board[tail[0], tail[1]] == 0
def check_(state, move, cord) : return cord != state and cord not in move
def moving(head, tail) :
state = sort_([head, tail])
moving = [[1, 0], [-1, 0], [0, 1], [0, -1]]
move = [sort_([add(head,x), add(tail, x)]) for x in moving if check(add(head, x), add(tail, x))]
rotate = [sort_([head, add(head, x)]) for x in moving if check(head, add(head, x)) and check(tail, add(tail, x)) and check_(state, move, sort_([head, add(head, x)]))] +\
[sort_([tail, add(tail, x)]) for x in moving if check(head, add(head, x)) and check(tail, add(tail, x)) and check_(state, move, sort_([head, add(head, x)]))]
return move+rotate
def solution(board):
global Board, N
N = len(board)
Board = np.pad(board, ((1,1),(1,1)), 'constant', constant_values=1)
que = deque([[(1, 1), (1, 2), 0]])
visted = [[(1, 1), (1, 2)]]
while que:
head, tail, cost = que.popleft()
if head == (N, N) or tail == (N, N):
return cost
for child in moving(head, tail):
if child not in visted:
que.append([*child, cost+1])
visted.append(child)
์๋ฌผ์ ์ ์ด์ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
import numpy as np
def solution(key, lock):
N, M = len(lock), len(key)
lock = np.pad(lock, ((M-1,M-1),(M-1,M-1)), 'constant', constant_values=0)
for _ in range(4) :
key = rotate(key)
for i in range(M+N-1) :
for j in range(M+N-1) :
lock_ = np.array(lock)
lock_[i:i+M, j:j+M] ^= key
if lock_[M-1 : N + M-1, M-1 : N + M-1].sum() == N**2 :
return True
return False
def rotate(key):
return np.array(list(zip(*key[::-1])))
์ธ๋ฒฝ ์ ๊ฒ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
from itertools import permutations
def solution(n, weak, dist):
answer = []
dists = [list(x) for x in permutations(dist)]
weaks = [weak] + [weak[i+1:]+[x+n for x in weak[:i+1]] for i, _ in enumerate(weak[:-1])]
for weak in weaks :
for dist in dists :
check = weak[0]
for i, d in enumerate(dist) :
check += d
if check >= weak[-1] :
answer.append(i)
break
else :
check = [x for x in weak if x > check][0]
return min(answer)+1 if answer else -1
๋ฌธ์์ด ์์ถ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
def solution(s):
return min(press(s, index) for index in list(range(1, int(len(s)/2) + 1)) + [len(s)])
def press(s, index) :
result = ''
coef = 1
while(len(s) > index) :
piv, s = s[:index]
s = s[index:]
if piv != s[:index] :
result += str(coef) + piv if coef != 1 else piv
coef = 0
coef += 1
result += str(coef)+ s if coef != 1 else s
return len(result)
๋๋ค์ฌ์ , Yang Sang-Ho ๋์ ์ฝ๋๋ฅผ ์กฐ๊ธ ์ฐธ๊ณ ํ์์ต๋๋ค.
๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
๋๋ฌด ์ด๋ ค์์ 5๋ช ์ ๋ ๋ค๋ฅธ์ฌ๋์ด ํผ ์ฝ๋๋ฅผ ๋ณด๊ณ ๊ณต๋ถํด์ ๋ค์ ์งฐ์ต๋๋ค. ๋จธ๋ฆฌ๊ฐ ๋ฉ์ฒญํ๋ฉด ๋ชธ์ด ๊ณ ์ํ๋ค๋ ๋ง์ด ๊ดํ ์๋๊ฒ ์๋์์ต๋๋ค. ๋ฌธ์ ์์ ์๋ ค์ฃผ๋ ์ ํ์ฌํญ์ ๊ผญ ๊ผผ๊ผผํ๊ฒ 3~4๋ฒ ์ฝ๊ณ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋ค๋ ์ฌ์ค์ ๊นจ๋ฌ์์ต๋๋ค.
def solution(n, build_frame):
global N
N = n
answer = []
for frame in build_frame :
if frame[3] == 1 : # ์ค์น
answer.append(frame[:-1])
if not check_rule(answer) : answer.pop() # ์ค์น ๊ท์น์ ์๋ฐฐ๋๋ฉด ๋ฃ์๊ฑฐ pop
else : # ์ญ์
del answer[answer.index(frame[:-1])]
if not check_rule(answer) : answer.append(frame[:-1]) # ์ญ์ ๊ท์น์ ์๋ฐฐ๋๋ฉด ๋ค์ ์ฝ์
answer.sort()
return answer
def check_rule(answer) :
for frame in answer :
x, y, structure = frame
if x < 0 or y < 0 or x > N or y > N : return False
if structure == 0 :
if y == 0 or [x, y, 1] in answer or [x-1, y, 1] in answer or [x, y-1, 0] in answer : continue
else : return False
else :
if [x, y-1, 0] in answer or [x+1, y-1, 0] in answer or ([x-1, y, 1] in answer and [x+1, y, 1] in answer) : continue
else : return False
return True
๊ดํธ ๋ณํ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
def solution(p):
if p == '' : return ''
u, v = split(p)
return u + solution(v) if check_right(u) else '('+ solution(v) + ')' + reverse(u[1:-1])
def check_balance(p) :
return str.count('(') == str.count(')')
def check_right(p) :
count = 0
for i in p :
count += 1 if i == '(' else -1
if count < 0 : return False
return True
def split(p) :
for i in range(1, len(p)+1) :
if check_balance(p[:i]) :
return p[:i], p[i:]
def reverse(p) :
return ''.join(['(' if x==')' else ')' for x in p])
๊ฐ์ฌ ๊ฒ์ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
์ฒ์๋ณด๋ ํธ๋ผ์ด ๊ตฌ์กฐ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํด ๋ฌธ์ ๋ฅผ ํ์ด์ผํด์ ๋งค์ฐ ๊ฐ์ด ์์กํ์์ต๋๋ค. ์ญ์ ์ด๋ฐ๋ฌธ์ ๋ ํ๋ฒ ๋ง์๋ด์ผ ๋ค์๋ถํฐ ์ ๋๋ก ํ ์ ์์ต๋๋ค. ์ ๊ธฐ์ต์ผ๋ก 2021 ๊ณต์ฑ ์ํ์์๋ ํธ๋ผ์ด ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ ๋ฌธ์ ๊ฐ 3๋ฒ์ธ๊ฐ 4๋ฒ์ผ๋ก ๋์์๋๋ฐ ๊ผญ ์ดํดํ๊ณ ๋์ด ๊ฐ์๊ธธ ๋ฐ๋๋๋ค.
import re
def solution(words, queries):
answer = []
trees = [Trie() for _ in range(10000)]
inv_trees = [Trie() for _ in range(10000)]
for word in words :
trees[len(word)-1].insert(word)
inv_trees[len(word)-1].insert(word[::-1])
for query in queries :
if query[0] == '?' :
answer.append(inv_trees[len(query)-1].query(re.sub('[^a-z]', '', query[::-1])))
else :
answer.append(trees[len(query)-1].query(re.sub('[^a-z]', '', query)))
return answer
class TrieNode:
def __init__(self, char):
self.char = char
self.counter = 0
self.children = {}
class Trie(object):
def __init__(self):
self.root = TrieNode("")
def insert(self, word):
node = self.root
for char in word:
node.counter += 1
if char in node.children:
node = node.children[char]
else:
new_node = TrieNode(char)
node.children[char] = new_node
node = new_node
node.counter += 1
def query(self, word) :
node = self.root
for char in word :
if char not in node.children : return 0
node = node.children[char]
return node.counter