functools
๋ชจ๋์์ ํจ์๊ฐ ๊ฝค๋ง์ง๋ง ๋ด๊ฐ ํ๋จํ์ ๋, ์ฝ๋ฉํ
์คํธ์ ํ์ํ ํจ์ ๋ช๊ฐ์ง๋ค๋ง ์ถ๋ ค๋ณด์๋ค. ์ฝ๋ฉํ
์คํธ์๋ ๋ฑํ ๊ฐ์ฒด์งํฅ์ ์ธ ํ๋ก๊ทธ๋จ์ด ํ์ํ์ง ์๊ธฐ ๋๋ฌธ์, class
์ ๊ด๋ จ๋ ํจ์๋ ์ ์ธํ์๋ค.
lru_cache
reduce
Java
๋ฌธ์ (15) + SQL
๋ฌธ์ (9) ์ ์ธ
from collections import defaultdict
from itertools import groupby
def solution(s):
lumps = defaultdict(lambda: defaultdict(int))
for char, group in groupby(s):
lumps[char][len(list(group))] += 1
unpretty = ((n := len(s)) - 1) * n * (n + 1) // 6
for lump in lumps.values():
total = sum(l * count for l, count in lump.items())
both_side = sum(lump.values())
for i in range(1, max(lump) + 1):
unpretty -= total * (total - 1) // 2
total -= both_side
both_side -= lump[i]
return unpretty
from collections import defaultdict
def fill_black(board, h, w):
board[h][w] = (-1 if h == 0 or board[h - 1][w] == -1 else 0)
def check(block, blocks, board) :
if len(blocks) < 4 : return False
h_list = sorted([h for h, w in blocks])
w_list = sorted([w for h, w in blocks])
for h in range(h_list[0], h_list[-1] + 1):
if any(color not in (-1, block) for color in board[h][w_list[0]:w_list[-1] + 1]):
return False
return True
def solution(board):
blocks = defaultdict(list)
answer = 0
for h in range((n := len(board))) :
for w in range(n) :
if (block := board[h][w]) > 0 :
(block_points := blocks[block]).append((h, w))
if not check(block, block_points, board) :
continue
answer += 1
for bh, bw in block_points :
fill_black(board, bh, bw)
elif block == 0 :
fill_black(board, h, w)
return answer
๊ฐ์ฌ๊ฒ์ ๋ฌธ์ ์ ๋น์ทํจ. Trie Tree
๋ฅผ ์ด์ฉํ ๋ฌธ์
def solution(words):
tree = Trie()
for word in words :
tree.insert(word)
return sum(tree.query(word) for word in words)
class TrieNode:
def __init__(self, char):
self.char = char
self.counter = 0
self.children = {}
class Trie(object):
def __init__(self):
self.root = TrieNode("")
def insert(self, word):
node = self.root
for char in word:
node.counter += 1
if char in node.children:
node = node.children[char]
else:
new_node = TrieNode(char)
node.children[char] = new_node
node = new_node
node.counter += 1
def query(self, word) :
node = self.root
for i, char in enumerate(word) :
node = node.children[char]
if node.counter == 1 :
return i+1
return len(word)
from itertools import accumulate
def solution(food_times, k):
n = len(food_times)
food_acc = list(accumulate((f := [0] + sorted(food_times))))
for i in range(1, n+1) :
if food_acc[i] + (n-i)*f[i] > k :
remain_food = [index+1 for index, food in enumerate(food_times) if food >= f[i]]
idx = (k - (food_acc[i-1] + (n-(i-1))*f[i-1])) % (n-(i-1))
return remain_food[idx]
return -1
from itertools import chain
def solution(land, P, Q):
land = sorted(chain.from_iterable(land))
k = len(land) * Q // (P + Q)
h = land[k]
return sum(h - x for x in land[:k])*P + sum(x - h for x in land[k:])*Q
def solution(strs, t):
DP = [0] * ((n := len(t)) + 1)
for i in range(1, n+1) :
DP[i] = min((DP[k] + 1 for k in range(max(0, i - 5), i) if t[k:i] in strs), default = 1e6)
return DP[-1] if DP[-1] < 1e6 else -1
from itertools import accumulate,combinations
def solution(cookie):
acc = set(accumulate([0] + cookie))
answer = [abs(x - y) // 2 for x, y in combinations(acc, 2) if (m := x + y) % 2 == 0 and m // 2 in acc]
return max(answer, default=0)
import sys
sys.setrecursionlimit(10**6)
def solution(k, room_number):
global pointer
pointer = {}
return [check_in(room) for room in room_number]
def check_in(room) :
if room not in pointer :
pointer[room] = room + 1
return room
pointer[room] = (avail := check_in(pointer[room]))
return avail