def solution(a, b):
return sum(range(min(a, b), max(a, b)+1))
๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ ๋ฐ๋ก๊ฐ๊ธฐ
from itertools import combinations
def solution(numbers):
return sorted(set(map(sum, combinations(numbers, 2))))
๋๋์ด ๋จ์ด์ง๋ ์ซ์ ๋ฐฐ์ด ๋ฐ๋ก๊ฐ๊ธฐ
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