코딩/알고리즘

import heapq def solution(operations): min_heap, max_heap = [], [] for oper in operations: o = oper.split(" ") if o[0] == "I": heapq.heappush(min_heap, int(o[1])) heapq.heappush(max_heap, -int(o[1])) elif o[0] == "D": if not min_heap and not max_heap: continue if o[1] == "-1": max_heap.remove(-heapq.heappop(min_heap)) elif o[1] == "1": min_heap.remove(-heapq.heappop(max_heap)) answer = [] if min..
def solution(coin, cards): answer = 0 n = len(cards) target = n + 1 myCard = {} for i in range(n // 3): myCard[cards.pop(0)] = 1 all_round = len(cards) // 2 temp = {} nowRound = 1 for i in range(all_round): temp[cards.pop(0)] = 1 temp[cards.pop(0)] = 1 check = False for key in myCard.copy(): reverse = target - key if myCard.get(reverse) != None: del myCard[reverse] del myCard[key] check = True n..
2024 KAKAO WINTER INTERNSHIP combi = [] perm = [] def solution(dice): leng = len(dice) generate(leng, [], 0, 0) permu(leng / 2, [], 0) max_winrate = 0 max_dice = [] count = 0 for arr in combi: b_arr = [] for i in range(leng): if i not in arr: b_arr.append(i) red = {} blue = {} for p in perm: a_val = 0 for i in range(len(arr)): a_val += dice[arr[i]][p[i]] red[a_val] = red.get(a_val, 0) + 1 b_val ..
def solution(n, k): answer = 0 for str in convert_notation(n, k).split("0"): if len(str) > 0 and int(str) > 1 and check_prime(int(str)): answer += 1 return answer def convert_notation(n, base): T = "0123456789ABCDEF" q, r = divmod(n, base) return convert_notation(q, base) + T[r] if q else T[r] def check_prime(num): valid = True for i in range(2, int(num ** 0.5) + 1): if (num % i == 0): valid = F..
def solution(n, tops): answer = 0 div = 10007 # 우하단 마름모를 제외한 경우의 수 배열 a = [0] * (n + 1) # 우하단 마름모만을 포함하는 경우의 수 배열 b = [0] * (n + 1) a[0] = 1 for i in range(0, n): # 현재 순회 중 top 존재 시 발생 경우의 수 확인 if (tops[i] == 1): a[i + 1] = a[i] * 3 + b[i] * 2 else: a[i + 1] = a[i] * 2 + b[i] * 1 # 현재 정삼각형에서 우하단 마름모인 경우는 앞선 경우의 수와 무관 b[i + 1] = a[i] + b[i] a[i + 1] = a[i + 1] % div; b[i + 1] = b[i + 1] % div; re..
from collections import defaultdict def solution(edges): answer = [0, 0, 0, 0] graph = defaultdict(lambda: [0, 0]) for s, e in edges: graph[s][1] += 1 graph[e][0] += 1 for items in graph.items(): node, num = items if num[0] == 0 and num[1] >= 2: answer[0] = node elif num[0] >= 2 and num[1] >= 2: answer[3] += 1 elif num[0] > 0 and num[1] == 0: answer[2] += 1 # 시작정점에서 뻗어나간 간선의 개수 = 총 그래프 개수 donut ..
def solution(h1, m1, s1, h2, m2, s2): answer = 0 s_total = getTotalTime(h1, m1, s1) e_total = getTotalTime(h2, m2, s2) if (s_total == 0 or s_total == 12 * 3600): answer += 1 for time in range(s_total, e_total): # 현재 위치 s_sec = time * 6 % 360 s_min = time * (6 / 60) % 360 s_hour = time * (30 / 3600) % 360 # 1초 뒤 위치 e_sec = (time + 1) * 6 % 360 e_min = (time + 1) * (6 / 60) % 360 e_hour = (time + ..
import java.util.*; class Solution { class Node implements Comparable { int index; int cost; Node(int index, int cost) { this.index = index; this.cost = cost; } @Override public int compareTo(Node o) { return Integer.compare(this.cost, o.cost); } } List[] graph; public int[] solution(int n, int[][] paths, int[] gates, int[] summits) { graph = new ArrayList[n + 1]; for (int i = 0; i < n + 1; i++)..
thisisdj
'코딩/알고리즘' 카테고리의 글 목록 (7 Page)