from collections import defaultdict
def solution(sales, links):
global graph, DP
graph = defaultdict(list)
for v1, v2 in links :
graph[v1-1].append(v2-1)
DP = [[0, 0, 0] for _ in range(len(sales))]
dfs(sales, 0)
return min(DP[0][0], DP[0][1])
def dfs(sales, node) :
if node not in graph :
DP[node][1] = sales[node]
else :
for child in graph[node] :
dfs(sales, child)
children = graph[node]
flag = sum(DP[child][2] for child in children)
DP[node][1] = sales[node] + (s := sum(min(DP[child][0] , DP[child][1]) for child in children))
DP[node][0] = s if flag else s + min(DP[child][1] - DP[child][0] for child in children)
DP[node][2] = 1 if DP[node][1] < DP[node][0] else 0
from collections import defaultdict, deque
from copy import deepcopy
def level(node, levels) :
levels[node] = -1
que = deepcopy(graph[node])
level = 0
while que :
for _ in range(len(que)) :
child = que.popleft()
if not levels[child] :
levels[child] = level + 1
que.extend(deepcopy(graph[child]))
level += 1
return levels
def solution(n, edges):
global graph
graph = defaultdict(deque)
for v1, v2 in edges:
graph[v1-1].append(v2-1)
graph[v2-1].append(v1-1)
v1 = level(0, [0]*n)
v2 = level(v1.index(max(v1)), [0]*n)
if v2.count((max_ := max(v2))) >= 2 :
return max_
else :
v3 = level(v2.index(max_), [0]*n)
return max_ if v3.count((max_ := max(v3))) >= 2 else max_ - 1
def solution(a):
x1, y1, z1 = a[0], max(a[:2]), max(a[0]+a[2], a[1])
x2, y2, z2 = 0, a[1], a[2]
for i in a[3:]:
x1, y1, z1 = y1, z1, max(x1, y1)+i
x2, y2, z2 = y2, z2, max(x2, y2)+i
return max(x1, y1, y2, z2)
๋นก๊ณ ์ ์ฝ๋
์ฝ๋ฉํ ์คํธ๋ฅผ ์ํด ์ด๋ ค์ด ๋ฌธ์ , ์ง๋ ์ฝ๋ฉํ ์คํธ ๋ฌธ์ ๋ค๋ง ๊ณจ๋ผ ํ๋ค๋ณด๋ ๋ฌธ๋ ํน์ ์๊ณ ๋ฆฌ์ฆ๋ค์ด ์ฝํ๋ค๋ ๊ฒ์ ๊นจ๋ซ๊ณ ํ๋ก๊ทธ๋๋จธ์ค Level1 ๋ฌธ์ ๋ถํฐ ์ฐจ๊ทผ์ฐจ๊ทผ ํ์ด๋ณด๊ณ ๋๋์ ์ด ๋ฌด์กฐ๊ฑด Level1 ๋ถํฐ ํ๊ณ ๊ณต๋ถํด์ผ ํฉ๋๋ค. ๊ฐ Level์ ๋ฐ๋ผ ํ์์ ์ผ๋ก ์ต์ํด์ ธ์ผํ๋ ๋ถ๋ถ๋ค์ด ๋ช๊ฐ์ง ์์์ต๋๋ค.

def solution(matrix_sizes):
n = len(matrix_sizes)
d = matrix_sizes[0] + [x[1] for x in matrix_sizes[1:]]
dp = [[0]*n for _ in range(n)]
for gap in range(1, n) :
for i in range(n-gap) :
for k in range(i, (j := i+gap)) :
l = dp[i][k] + dp[k+1][j] + d[i]*d[k+1]*d[j+1]
dp[i][j] = min(dp[i][j], l) if dp[i][j] else l
return dp[0][-1]
๋ ๋ฌธ์ ๋ฅผ ํฉ์ณ๋์ ๋ฌธ์ ๋ค. ๋์ถฉ์ค๋ช ํ ๊บผ๋๊น ์์ ๋ ๋ฌธ์ ๋ชจ๋ฅด๋ฉด ๊ณต๋ถใฑ
import math
def solution(distance, rocks, n):
rocks.sort()
min_, max_ = 1, distance
while min_ < max_ :
destroy = 0; piv_rock = 0
estimate_min_distance = (min_+max_) // 2
for rock in rocks :
if rock - piv_rock < estimate_min_distance : destroy += 1
else : piv_rock = rock
if destroy > n : max_ = estimate_min_distance
else : min_ = estimate_min_distance + 1
return min_ - 1
def solution(n):
if n%2 != 0 : return 0
n_2 = 3
n_1 = 11
for i in range(3, n//2+1) :
n_1, n_2 = (4*n_1 - n_2) % 1000000007, n_1
return n_1
ํ์ธ๊ฐ ์ฝ๋. ์ด๋ป๊ฒ ์ด๋ฐ ํจํด์ ๋ฐ๊ฒฌํ๊ฑด์ง ๊ฒฝ์ด๋ก์์ ๊ฐ์ ธ์๋ด. ์ด๊ฑฐํจํด ๋ฌด์จ ๋ฐฉ์์ธ์ง ์ข ์๋ ค์ฃผ์ ๋ ๋์ ํ ๋ชจ๋ฅด๊ฒ ์
def solution(n):
pa, pb, pc, a, b, c = 1, 0, 0, 0, 0, 2
for _ in range(1, n):
pa, pb, pc, a, b, c = a, b, c, (c + pa) % 1000000007, c, (b + a * 2) % 1000000007
return a
from collections import defaultdict
from math import comb
from functools import lru_cache
@lru_cache(maxsize=None)
def C(n,k): return comb(n,k)
def solution(a):
one_cnt = [sum(ones) for ones in zip(*a)] # ๊ฐ์ด 1์ ๊ฐฏ์
DP = defaultdict(int,{(rows := len(a))-one_cnt[0]:comb(rows, one_cnt[0])}) # 1์ด๊น์ง ๊ณ์ฐํ DP
for ones in one_cnt[1:]: # DP[2][j] ๋ถํฐ ๊ณ์ฐ
next_DP = defaultdict(int)
for even_rows in DP:
odd_rows = rows-even_rows
for add_one in range(max(0,ones-odd_rows), min(ones,even_rows)+1): # range ๋ฒ์๋ ๋ฏธ๋ง์ด๊ธฐ๋๋ฌธ์ +1
next_DP[even_rows+ones-2*add_one] += DP[even_rows] * C(even_rows, add_one) * C(odd_rows, ones-add_one)%(10**7+19)
DP = next_DP
return DP[rows]
import heapq
def solution(land, height):
N = len(land)
d = [(0, 1), (0, -1), (1, 0), (-1, 0)]
board = [[0]*N for _ in range(N)]
que = [(0, 0, 0)]
answer = 0
while que :
cost, h, w = heapq.heappop(que)
if board[h][w] : continue
board[h][w] = 1
answer += cost
cur_height = land[h][w]
for next_h, next_w in [[h+dh, w+dw] for dh, dw in d if 0 <= h+dh < N and 0 <= w+dw < N] :
next_height = land[next_h][next_w]
next_cost = n_cost if (n_cost := abs(cur_height - next_height)) > height else 0
heapq.heappush(que, (next_cost, next_h, next_w))
return answer
def solution(A, B):
A.sort()
B.sort()
a = b = 0
for _ in range(len(A)) :
if A[a] < B[b] :
a += 1
b += 1
return a
A ์ ์์ a ๋ณด๋ค ํฐ B์ ์์ ์ค b ์ ์ต์๊ฐ๋ค์ ๊ฐฏ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๋ค.
a๋ณด๋ค ํฐb๋ค ์ค ์ต์๊ฐ์ ๊ฐฏ์