practivceAlgorithm 570

[Programmers][Python][2021 카카오 인턴십] 표 편집

우선순위 큐 두개를 이용해 가운데값을 k로 운용했습니다. 마지막에 left를 right로 굳이 안옮겨도됐을텐데 조금 아쉽습니다. 또 set을 썼으면 좀 더 출력부분이 깔끔한 반복문으로 처리됐을 것 같습니다. 다른 분들은 연결리스트를 구현하신분들이 많았습니다. from heapq import heappop, heappush def solution(n, k, cmd): left, right, delete = [-i for i in range(k-1, -1, -1)], [i for i in range(k, n)], [] for command in cmd: c, *num = command.split() if c == 'U': cnt = int(num.pop()) for _ in range(cnt): heappu..

[Programmers][Python][2021 카카오 인턴십] 거리두기 확인하기

각 지점에서 거리 2까지만 확인해주면 됩니다. 거리두기 안지켰으면 바로 return False 해줍니다. delta = ((0, 1), (1, 0), (0, -1), (-1, 0)) def is_check(place): q = [] for i in range(5): for j in range(5): if place[i][j] == 'P': q.append((i, j, 0, set())) while q: x, y, dist, visited = q.pop() visited.add((x, y)) for dx, dy in delta: nx, ny = x + dx, y + dy if 0

[Codeforce][Python] #702 G. Old Floppy Drive

이거 cycle을 산출하는 공식이 이해가 안됨. 이번주 내로 해결할 예정 import sys input = sys.stdin.readline from bisect import bisect_left # 풀이 참고했는데 for test in range(int(input())): n, m = map(int, input().split()) arr = list(map(int, input().split())) prefix_sum, idxes, idx, total = [], [], 0, 0 for num in arr: total += num if not prefix_sum or prefix_sum[-1] < total: prefix_sum.append(total) idxes.append(idx) idx += 1 x ..

[Codeforce][Python] #702 F. Equalize the Array

Counter map을 만든 후 set에 조사할 count들을 모은 후 순회하며 그 cnt보다 크면 cnt에 맞춰주고 작으면 0에 맞춰주는 식으로 풀었습니다. import sys input = sys.stdin.readline from collections import defaultdict for test in range(int(input())): n = int(input()) arr = list(map(int, input().split())) counter = defaultdict(int) for num in arr: counter[num] += 1 candidate = set() for target in counter.values(): candidate.add(target) answer = float..

[Codeforce][Python] #702 D. Permutation Transformation

분할시키면서 실제 subtree도 분할시켰습니다. import sys input = sys.stdin.readline def make_tree(depth, a): if not a: return a max_num = 0 for idx, num in enumerate(a): if max_num < num: max_idx = idx max_num = num answer[dic[max_num]] = depth make_tree(depth+1, a[:max_idx]) make_tree(depth+1, a[max_idx+1:]) for test in range(int(input())): n = int(input()) arr = list(map(int, input().split())) dic = {num: idx f..