๋๋์ด ๋จ์ด์ง๋ ์ซ์ ๋ฐฐ์ด ๋ฐ๋ก๊ฐ๊ธฐ
def solution(arr, divisor):
answer = sorted([num for num in arr if num%divisor == 0])
return answer if answer else [-1]
๊ฐ์ ์ซ์๋ ์ซ์ด ๋ฐ๋ก๊ฐ๊ธฐ
def solution(arr):
new_arr = []
for num in arr :
if num not in new_arr[-1:]: new_arr.append(num)
return new_arr
๊ฐ์ด๋ฐ ๊ธ์ ๊ฐ์ ธ์ค๊ธฐ ๋ฐ๋ก๊ฐ๊ธฐ
def solution(s):
return s[len(s)//2-1:len(s)//2+1]
def solution(array, commands):
return [sorted(array[i-1:j])[k-1] for i, j, k in commands]
def solution(n):
ternary = ''
while n > 0: #3์ง๋ฒ ์๋ฅผ ๋ค์ง์ผ๋ฉฐ ์์ฑ
n, remainder = divmod(n, 3)
ternary += str(remainder)
return int(ternary, 3)
import datetime
def solution(a, b):
answer = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']
return answer[datetime.date(2016,a,b).weekday()]
๋ค์ ์ธ์์ . ์ฐธ๊ณ ใดใด
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)