from collections import defaultdict
def solution(genres, plays):
dic = defaultdict(list)
for i, info in enumerate(zip(genres, plays)) :
kind, play_time = info
dic[kind].append([i, play_time])
answer = []
for k in sorted(dic.values(), key = lambda info : sum(play_time for i, play_time in info), reverse = True) :
answer.extend(sorted(k, key = lambda info : info[1], reverse = True)[:2])
return [index for index, _ in answer]
import numpy as np
def solution(m, n, puddles):
board = [[0 for _ in range(m+1)] for _ in range(n+1)]
board[0][0]=1
for h in range(n) :
for w in range(m) :
if [w+1, h+1] in puddles: continue # ํ์ฌ ์์น๊ฐ ๋ฌผ์
๋ฉ์ด๋ผ๋ฉด continue
board[h][w+1] += board[h][w]
board[h+1][w] += board[h][w]
return board[n-1][m-1] % 1000000007
def solution(s):
for end in range(len(s), 0, -1):
for start in range(0, len(s)-end+1):
substr = s[start:start+end]
if substr == substr[::-1]:
return len(substr)
import numpy as np
def solution(n, times):
times = np.array(sorted(times))
min_ = times[0] * (n//len(times))
max_ = times[-1]* (n//len(times))
while min_ < max_ :
estimate = (min_+max_) // 2
total_p = (estimate//times).sum()
if total_p < n :
min_ = estimate+1
else :
max_ = estimate
return int(min_)
import bisect
def solution(operations):
answer = []
for operation in operations :
order, num = operation.split(' ')
if order == 'I' :
bisect.insort_left(answer, int(num))
elif answer :
if num == '-1' :
answer.pop(0)
else :
answer.pop(-1)
return [answer[-1], answer[0]] if answer else [0, 0]
from collections import defaultdict
import bisect
def solution(tickets):
graph = defaultdict(list)
for v1, v2 in tickets :
bisect.insort_left(graph[v1], v2)
stack = ['ICN']
visited = []
while stack :
airport = stack[-1]
if graph[airport] :
stack.append(graph[airport].pop(0))
else :
visited.append(stack.pop())
return visited[::-1]
from collections import Counter
def solution(begin, target, words):
if target not in words : return 0
visited = [begin]
que = [x for x in words if sum((Counter(begin)&Counter(x)).values()) == len(begin)-1]
t = 1
while que :
for _ in range(len(que)) :
child = que.pop(0)
if child == target : return t
if child not in visited :
que.extend([x for x in words if sum((Counter(child)&Counter(x)).values()) == len(child)-1])
t += 1
def solution(routes):
routes = sorted(routes, key=lambda x: x[1])
last_camera = -30000
answer = 0
for route in routes:
if last_camera < route[0]:
answer += 1
last_camera = route[1]
return answer
def solution(triangle):
for i in range(len(triangle) -1 ) :
triangle[i+1][0] += triangle[i][0]
triangle[i+1][-1] += triangle[i][-1]
for i in range(2, len(triangle)) :
for j in range(1, len(triangle[i]) -1) :
triangle[i][j] += max(triangle[i-1][j-1], triangle[i-1][j])
return max(triangle[-1])
def ancestor(node, parents):
if parents[node] == -1:
return node
else:
return ancestor(parents[node], parents)
def solution(n, costs):
answer = 0
costs.sort(key = lambda x : x[2])
parents = [-1]*n
bridges = 0
while bridges < n-1 :
v1, v2, cost = costs.pop(0)
if (next_v1 := ancestor(v1, parents)) != ancestor(v2, parents):
answer += cost
parents[next_v1] = v2
bridges += 1
return answer