목록분류 전체보기 (52)
JH 개발 블로그
1. 정렬 후 이분탐색 ex) [probb ,proaa, prozz,procc]는 정렬 후 [proaa,probb,procc,prozz]가 된다. probb,procc는 proaa와 prozz 사이에 있음에 착안해서, ?를 a와 z로 바꾼후 그 사이에 있는 단어의 개수를 센다. from collections import defaultdict import bisect def find_result(word,left,right): left_idx = bisect.bisect_left(word,left) right_idx = bisect.bisect_right(word,right) return right_idx-left_idx def solution(words, queries): answer = [] word_..
def solution(key, lock): n,m = len(lock),len(key) new_map = [[0]*(2*(m-1)+n) for _ in range(2*(m-1)+n)] for i in range(n): for j in range(n): new_map[m-1+i][m-1+j] = lock[i][j] for _ in range(4): for i in range(m-1+n): for j in range(m-1+n): for k in range(m): for l in range(m): new_map[i+k][j+l] += key[k][l] flag = True for k in range(n): for l in range(n): if new_map[m-1+k][m-1+l] != 1: flag =..
def make_u_v(s): cnt1 = cnt2 = 0 for i in s: if i == '(': cnt1 += 1 else: cnt2 += 1 if cnt1 == cnt2: break return s[:cnt1 + cnt2], s[cnt1 + cnt2:] def is_right(s): cnt1 = cnt2 = 0 for i in s: if i == '(': cnt1 += 1 else: cnt2 += 1 if cnt2 > cnt1: return False return True def solution(p): if p == '': return p answer = '' u, v = make_u_v(p) if is_right(u): answer += u answer += solution(v) else: a..
def solution(s): answer = len(s) for step in range(1,len(s)//2+1): temp = [s[i:i+step] for i in range(0,len(s),step)] ans = 0 cnt = 0 for i in range(len(temp)-1): cnt += 1 if temp[i] == temp[i+1]: continue else: ans += len(temp[i]) + (len(str(cnt)) if cnt > 1 else 0) cnt = 0 cnt += 1 ans += len(temp[-1]) + (len(str(cnt)) if cnt > 1 else 0) answer = min(answer,ans) return answer
dir = ((-1,0),(0,1),(1,0),(0,-1)) def A_turn(ar,ac,br,bc,cnt,board): if board[ar][ac] == 0: return (1,cnt) winner = [] loser = [] flag = False for dr,dc in dir: nr,nc = ar+dr,ac+dc if 0
def solution(board, skill): R,C = len(board),len(board[0]) imos = [[0]*(R+1) for _ in range(C+1)] for type,r1,c1,r2,c2,degree in skill: if type == 1: imos[r1][c1] -= degree imos[r1][c2+1] += degree imos[r2+1][c1] += degree imos[r2+1][c2+1] -= degree if type == 2: imos[r1][c1] += degree imos[r1][c2+1] -= degree imos[r2+1][c1] -= degree imos[r2+1][c2+1] += degree for i in range(len(imos)): for j i..
from collections import deque def solution(info, edges): tree = [[] for _ in range(len(info))] for a,b in edges: tree[a].append(b) q = deque([(1,0,tuple([1]+[0]*(len(info)-1)))]) visited = set() while q: s,w,visit = q.popleft() if s == w: continue if (s,w,visit) in visited: continue visited.add((s,w,visit)) for i in range(len(info)): if visit[i] == 1: for j in tree[i]: if not visit[j]: temp = li..
def dfs(n, info, k, apeach, lion, temp): if n == 0: global max_val, answer if lion - apeach > max_val: max_val = lion - apeach answer = temp[:] elif lion - apeach == max_val and max_val != 0: for i in range(10, -1, -1): if temp[i] > answer[i]: answer = temp[:] break elif temp[i] info[i]: temp[i] = info[i] + 1 if info[i] > 0: dfs(n - temp[i]..