본문 바로가기

백준

[코딩인터뷰] 백준 15684번 def is_completed(): global n global h global ladder for b in range(n): s = b for a in range(h): if 0 < s and ladder[a][s-1]: s -= 1 continue if ladder[a][s]: s += 1 continue if s != b: return False return True def dfs(count, a, b): global n global h global ladder global answer if is_completed(): if count < answer: answer = count return if count == 3: return # 현재 b = 세로선부터 마지막 바로 전 세로선까지만 # i = 세..
[코딩인터뷰] 백준 14888번 Python3 는 시간초과 가 발생합니다. itertools.permutations 함수가 시간이 오래 걸리기 때문입니다. PyPy3 로 변경하면 통과됩니다. 저는 개인적으로 알고리즘 효율성에 큰 의미를 두지는 않습니다. 효율성이 중요하지 않다는건 아닌데, 실무를 하다보니 주어진 개발 기간을 코드의 효율을 개선하는데 투자하기 보다는 전체 설계를 개선할 수 있는 코드 또는 가독성이 좋아지는 코드를 짜는데 시간을 투자하는게 생산성이 더 높다고 생각합니다. 기간 내에 정확하게 개발해서 출시한 뒤에 효율이 크게 뒤떨어지는 부분부터 제품을 개선해 나가는 재미도 있으니까요. 당연히 효율 높은 코드를 바로바로 구현할 수 있도록 훈련을 지속적으로 하면 생산성과 효율 모두 달성할 수 있으니, 코딩 테스트로 연습할 때는 ..
[코딩인터뷰] 백준 14889번 import itertools n = int(input()) score_board = [] for _ in range(n): score_board.append(list(map(lambda i:int(i), input().split()))) team = range(n) min_dist = 10**8 for selected in itertools.combinations(team, n//2): start_team = selected link_team = [] for k in range(n): if k not in selected: link_team.append(k) start_team_score = 0 link_team_score = 0 for m in itertools.combinations(start_te..
[코딩인터뷰] 백준 9663번 def solve(n, location, column): c = 0 if column == n: return 1 for row in range(n): if is_possible(n, location, column, row): location.append((column, row)) c += solve(n, location, column + 1) location.pop() return c def is_possible(n, location, column, row): for loc in location: if loc[0] == column: return False if loc[1] == row: return False for k in range(1, min(n - loc[0], n - loc[1])): if c..
[코딩인터뷰] 백준 2661번 def is_possible(l): for d in range(1, len(l) // 2 + 1): for s in range(len(l) - (d*2) + 1): a_start = s a_end = s + d a = l[a_start:a_end] b_start = a_end b_end = a_end + d if b_end > len(l): break b = l[b_start:b_end] if a == b: return False return True n = int(input()) l = [] value = (1, 2, 3) pos = [[True, True, True] for _ in range(n)] while len(l) < n: i = len(l) is_appended = False for v i..