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๋ค ์ค ์ต์๊ฐ์ ๊ฐฏ์
from collections import defaultdict
to_minute = lambda time : sum([int(x)*y for x, y in zip(time.split(':'), [60, 1])])
to_time = lambda minute : ':'.join(map('{0:02d}'.format, divmod(minute, 60)))
def solution(n, t, m, timetable):
minute_table = sorted(map(to_minute, timetable))
bus_table = {to_minute('09:00')+t*i:[] for i in range(n)}
for bus in bus_table :
for j in range(m) :
if minute_table and minute_table[0] <= bus :
bus_table[bus].append(minute_table.pop(0))
last_time, last_bus = list(bus_table.items())[-1]
return to_time(last_time) if len(last_bus) < m else to_time(max(last_bus)-1)
from collections import defaultdict
import re
from decimal import Decimal
def solution(word, pages) :
word = word.lower()
web = defaultdict(defaultdict)
link_score = defaultdict(int)
url_pattern = re.compile('<meta property=\"og:url\" content=\"(.+?)"')
body_pattern = re.compile(r'\<body>\n(.+?)\n\</body>', re.S)
ext_url_pattern = re.compile('<a href="(.+?)"')
for index, page in enumerate(pages) :
page = page.lower()
url = url_pattern.findall(page).pop()
web[url]['index'] = index
body = ' '.join(body_pattern.findall(page))
web[url]['ext_url'] = ext_url_pattern.findall(body)
web[url]['basic_score'] = Decimal(str(re.sub('[^a-z]', '.', body).split('.').count(word)))
for ext_url in web[url]['ext_url'] :
link_score[ext_url] += web[url]['basic_score'] / len(web[url]['ext_url'])
return sorted([[val['basic_score'] + link_score[url], val['index']] for url, val in web.items()], key = lambda x : (-x[0], x[1]))[0][1]