์๋ฌผ์ ์ ์ด์ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
import numpy as np
def solution(key, lock):
N, M = len(lock), len(key)
lock = np.pad(lock, ((M-1,M-1),(M-1,M-1)), 'constant', constant_values=0)
for _ in range(4) :
key = rotate(key)
for i in range(M+N-1) :
for j in range(M+N-1) :
lock_ = np.array(lock)
lock_[i:i+M, j:j+M] ^= key
if lock_[M-1 : N + M-1, M-1 : N + M-1].sum() == N**2 :
return True
return False
def rotate(key):
return np.array(list(zip(*key[::-1])))
์ธ๋ฒฝ ์ ๊ฒ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
from itertools import permutations
def solution(n, weak, dist):
answer = []
dists = [list(x) for x in permutations(dist)]
weaks = [weak] + [weak[i+1:]+[x+n for x in weak[:i+1]] for i, _ in enumerate(weak[:-1])]
for weak in weaks :
for dist in dists :
check = weak[0]
for i, d in enumerate(dist) :
check += d
if check >= weak[-1] :
answer.append(i)
break
else :
check = [x for x in weak if x > check][0]
return min(answer)+1 if answer else -1
๋ฌธ์์ด ์์ถ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
def solution(s):
return min(press(s, index) for index in list(range(1, int(len(s)/2) + 1)) + [len(s)])
def press(s, index) :
result = ''
coef = 1
while(len(s) > index) :
piv, s = s[:index]
s = s[index:]
if piv != s[:index] :
result += str(coef) + piv if coef != 1 else piv
coef = 0
coef += 1
result += str(coef)+ s if coef != 1 else s
return len(result)
๋๋ค์ฌ์ , Yang Sang-Ho ๋์ ์ฝ๋๋ฅผ ์กฐ๊ธ ์ฐธ๊ณ ํ์์ต๋๋ค.
๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
๋๋ฌด ์ด๋ ค์์ 5๋ช ์ ๋ ๋ค๋ฅธ์ฌ๋์ด ํผ ์ฝ๋๋ฅผ ๋ณด๊ณ ๊ณต๋ถํด์ ๋ค์ ์งฐ์ต๋๋ค. ๋จธ๋ฆฌ๊ฐ ๋ฉ์ฒญํ๋ฉด ๋ชธ์ด ๊ณ ์ํ๋ค๋ ๋ง์ด ๊ดํ ์๋๊ฒ ์๋์์ต๋๋ค. ๋ฌธ์ ์์ ์๋ ค์ฃผ๋ ์ ํ์ฌํญ์ ๊ผญ ๊ผผ๊ผผํ๊ฒ 3~4๋ฒ ์ฝ๊ณ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋ค๋ ์ฌ์ค์ ๊นจ๋ฌ์์ต๋๋ค.
def solution(n, build_frame):
global N
N = n
answer = []
for frame in build_frame :
if frame[3] == 1 : # ์ค์น
answer.append(frame[:-1])
if not check_rule(answer) : answer.pop() # ์ค์น ๊ท์น์ ์๋ฐฐ๋๋ฉด ๋ฃ์๊ฑฐ pop
else : # ์ญ์
del answer[answer.index(frame[:-1])]
if not check_rule(answer) : answer.append(frame[:-1]) # ์ญ์ ๊ท์น์ ์๋ฐฐ๋๋ฉด ๋ค์ ์ฝ์
answer.sort()
return answer
def check_rule(answer) :
for frame in answer :
x, y, structure = frame
if x < 0 or y < 0 or x > N or y > N : return False
if structure == 0 :
if y == 0 or [x, y, 1] in answer or [x-1, y, 1] in answer or [x, y-1, 0] in answer : continue
else : return False
else :
if [x, y-1, 0] in answer or [x+1, y-1, 0] in answer or ([x-1, y, 1] in answer and [x+1, y, 1] in answer) : continue
else : return False
return True
๊ดํธ ๋ณํ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
def solution(p):
if p == '' : return ''
u, v = split(p)
return u + solution(v) if check_right(u) else '('+ solution(v) + ')' + reverse(u[1:-1])
def check_balance(p) :
return str.count('(') == str.count(')')
def check_right(p) :
count = 0
for i in p :
count += 1 if i == '(' else -1
if count < 0 : return False
return True
def split(p) :
for i in range(1, len(p)+1) :
if check_balance(p[:i]) :
return p[:i], p[i:]
def reverse(p) :
return ''.join(['(' if x==')' else ')' for x in p])
๊ฐ์ฌ ๊ฒ์ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
์ฒ์๋ณด๋ ํธ๋ผ์ด ๊ตฌ์กฐ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํด ๋ฌธ์ ๋ฅผ ํ์ด์ผํด์ ๋งค์ฐ ๊ฐ์ด ์์กํ์์ต๋๋ค. ์ญ์ ์ด๋ฐ๋ฌธ์ ๋ ํ๋ฒ ๋ง์๋ด์ผ ๋ค์๋ถํฐ ์ ๋๋ก ํ ์ ์์ต๋๋ค. ์ ๊ธฐ์ต์ผ๋ก 2021 ๊ณต์ฑ ์ํ์์๋ ํธ๋ผ์ด ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ ๋ฌธ์ ๊ฐ 3๋ฒ์ธ๊ฐ 4๋ฒ์ผ๋ก ๋์์๋๋ฐ ๊ผญ ์ดํดํ๊ณ ๋์ด ๊ฐ์๊ธธ ๋ฐ๋๋๋ค.
import re
def solution(words, queries):
answer = []
trees = [Trie() for _ in range(10000)]
inv_trees = [Trie() for _ in range(10000)]
for word in words :
trees[len(word)-1].insert(word)
inv_trees[len(word)-1].insert(word[::-1])
for query in queries :
if query[0] == '?' :
answer.append(inv_trees[len(query)-1].query(re.sub('[^a-z]', '', query[::-1])))
else :
answer.append(trees[len(query)-1].query(re.sub('[^a-z]', '', query)))
return answer
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 char in word :
if char not in node.children : return 0
node = node.children[char]
return node.counter
ํคํจ๋ ๋๋ฅด๊ธฐ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
def solution(numbers, hand):
results = []
left = 10
right = 12
for number in numbers :
if number in [1, 4, 7] :
results.append('L')
left = number
elif number in [3, 6, 9] :
results.append('R')
right = number
else : # 2, 5, 8, 0์ ๊ฒฝ์ฐ ๊ฑฐ๋ฆฌ ๊ณ์ฐ
if number == 0: number = 11
q_l, r_l = divmod(left-1, 3) # ์ผ์ ์ซ์์ ์ขํ
q_r, r_r = divmod(right-1, 3) # ์ค๋ฅธ์ ์ซ์์ ์ขํ
q_n, r_n = divmod(number-1, 3) # ํ์ฌ ์ซ์์ ์ขํ
if abs(q_n - q_r) + abs(r_n - r_r) > abs(q_n - q_l) + abs(r_n - r_l):
results.append('L')
left = number
elif abs(q_n - q_r) + abs(r_n - r_r) < abs(q_n - q_l) + abs(r_n - r_l) :
results.append('R')
right = number
else : # ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ
if hand == 'right' :
results.append('R')
right = number
else :
results.append('L')
left = number
return ''.join(results)
์์ ์ต๋ํ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
์ด ๋ฌธ์ ๋ ์ ๊ฐํผ ์ฝ๋๋ณด๋ค ์๊ณจ์ฐ์ , ์ ์ฒ ํธ ๋๊ป์ ํผ ์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํ๊ณ ์ธ๋ จ๋์ด์ ๊ทธ ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ํ์ด๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค.
import re
from itertools import permutations
def solution(expression):
#1
op = [x for x in ['*','+','-'] if x in expression]
op = [list(y) for y in permutations(op)]
ex = re.split(r'(\D)',expression)
#2
a = []
for x in op:
_ex = ex[:]
for y in x:
while y in _ex:
tmp = _ex.index(y)
_ex[tmp-1] = str(eval(_ex[tmp-1]+_ex[tmp]+_ex[tmp+1]))
_ex = _ex[:tmp]+_ex[tmp+2:]
a.append(_ex[-1])
#3
return max(abs(int(x)) for x in a)
๋ณด์ ์ผํ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ ์ฝ๋ฉ ํ ์คํธ์์ ์๊ฐ์ก์๋จน๋ ๋น์จ์ด ์ฝ๋ฉ 10%, ์๊ณ ๋ฆฌ์ฆ ๊ณ ์ 90% ์ธ๊ฒ๊ฐ๋ค. ํนํ๋ ํจ์จ์ฑ์ด ํฌํจ๋ ๋ฌธ์ ๋ผ๋ฉดโฆ
์ด๋ฌธ์ ์์ ํจ์จ์ฑ์ ํต์ฌ์ Dictionary
๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด๋ค.
์ปค์๊ฐ ์ด๋ํ๋ฉฐ ์ฅ๋ฐ๊ตฌ๋(Dictionary
)์์ ๋ชจ๋ ๋ณด์์ข
๋ฅ๊ฐ ๋ค์ด์ฌ๋ ๊น์ง R
์ปค์๊ฐ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ๊ณ , ๋ชจ๋ ๋ณด์ ์ข
๋ฅ๊ฐ ์ฅ๋ฐ๊ตฌ๋์์ ๋ค์ด์ค๋ฉด ์ผ์ชฝ์ปค์๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์ํค๋ฉฐ ๋ฒ์๋ฅผ ์ค์ฌ๋๊ฐ๋ค.
์ต์ข
์ ๋ต์ ์ ๋ต ํ๋ณด์ค ๊ฐ์ฅ ๊ธธ์ด๊ฐ ์งง์(R
- L
) [L, R]
์ return
ํ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ์ฅ๋ฐ๊ตฌ๋์ ๋ณด์
์ด 0๊ฐ๊ฐ ๋๋ฉด ์ญ์ ๋ฅผ ์์ผ์ฃผ์ด์ผํ๊ณ , ์ด๋ฏธ ์๋ ๋ณด์
์ด๋ฉด +1
ํด์ฃผ์ด์ผํ๋ค. ์๋ํ๋ฉด ์ด ๋ฌธ์ ์์ ํจ์จ์ฑ์ ํ๋จํ๋ ํต์ฌ์ด ๋ฐ๋ก ์ฅ๋ฐ๊ตฌ๋์ ์ฌ์ด์ฆ๋ก ๋ชจ๋ ์ข
๋ฅ์ ๋ณด์์ ์๋์ง๋ฅผ ํ๋จํด์ผํ๊ธฐ ๋๋ฌธ์ด๋ค.
**
def solution(gems):
products = list(set(gems))
l = 0
r = 0
bucket = {}
answer = [0, len(gems)]
while(l <= len(gems) and r <= len(gems)) :
if len(bucket) == len(products) :
if answer[1]-answer[0] > r-l:
answer = [l, r]
bucket[gems[l]] -= 1
if bucket[gems[l]] == 0 : del bucket[gems[l]]
l += 1
else :
try :
if gems[r] in bucket : bucket[gems[r]] += 1
else : bucket[gems[r]] = 1
except :
break
r += 1
answer[0] += 1
return answer
๋ณด์์ ๋ช์ข
๋ฅ ์ฌ์ผํ๋์ง๋ฅผ set
์ ์ด์ฉํ์ฌ ๊ตฌํด์ค๋๋ค.
products = list(set(gems))
๋๊ตด ํํ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ ์ด ๋ฌธ์ ๋ ์ด๋ป๊ฒ ํ์ด์ผํ ์ง ๊ฐ์ด ์์กํ์ ํ์ด ํด์ค์ ๋ดค์ง๋งโฆ ํด์ค๋๋ก ๊ตฌํ์ ํด๋ ํจ์จ์ฑ์์ ๊ณ์ ๋งํ์ ์ฑ๊ณตํ ์ฌ๋์ ์ฝ๋์ค ๊นํ์ฐ๋์ ์ฝ๋๊ฐ ๊ฐ์ฅ ์ ์ง ๊ฑฐ๊ฐ์์ ๊ทธ๋ถ์ ์ฝ๋๋ฅผ ์ดํดํ๊ณ ๊ณต๋ถํ์ต๋๋ค.
import sys
sys.setrecursionlimit(10**6)# ์ฌ๊ท ๊น์ด ์ค์
def haveCycle(node):
if visit[node]:
if visit[node] == -1:
return True
return False
visit[node] = -1
for _next in inv_graph[node]:
if haveCycle(_next):
return True
visit[node] = 1
return False
def make_inv_graph(node, parent):
for child in graph[node]:
if child != parent:
inv_graph[child].append(node)
make_inv_graph(child, node)
def solution(n, path, order):
global graph, inv_graph, visit
graph, inv_graph, visit = [[] for _ in range(n)], [[] for _ in range(n)], [0]*n
for parent, node in path:
graph[parent].append(node)
graph[node].append(parent)
make_inv_graph(0, -1)
for parent, node in order:
inv_graph[node].append(parent)
for node in range(n):
if haveCycle(node):
return False
return True