목록전체 글 (772)
컴공생의 다이어리
[프로그래머스] 후보키 - 파이썬(Python) from itertools import combinations def solution(relation): len_key = len(relation[0]) candidate_key = [] for i in range(1, len_key + 1): for combi in combinations(range(len_key), i): # 후보키가 될수 있는 key들의 전체 조합 temp = list() for r in relation: curr = [r[c] for c in combi] # 현재 후보키 조합에 해당하는 튜플 데이터 if curr in temp: # 유일성을 만족하지 않는 경우 break else: temp.append(curr) else: for c..
[프로그래머스] 실패율 - 파이썬(Python) import collections def solution(N, stages): total_people = len(stages) stage_count = dict(collections.Counter(stages)) result = [] for i in range(1, N + 1): if i in stage_count.keys(): result.append((i, stage_count[i] / total_people)) total_people -= stage_count[i] else: result.append((i, 0)) result.sort(key=lambda x: (-x[1], x[0])) return [x for x, _ in result] https:..
[프로그래머스] 오픈채팅방 - 파이썬(Python) def solution(record): answer = [] id_dict = dict() # id에 해당하는 이름 정보 commands = [list(r.split()) for r in record] # record의 데이터들을 공백 기준으로 분리 for command in commands: if command[0] == 'Enter' or command[0] == 'Change': # id에 해당하는 닉네임 정보 생성 혹은 업데이트 id_dict[command[1]] = command[2] for command in commands: if command[0] == 'Enter': answer.append(id_dict[command[1]] + "님이..
ANY 함수 컬럼명 비교연산자 ANY(값들 혹은 서브쿼리) 여러개의 비교값 중 하나라도 만족하면 true 반환 IN과 다른점은 비교 연산자를 사용한다는 점 비교 연산자와 ANY > ANY : 최소값보다 크면 >= ANY : 최소값보다 크거나 같으면 ANY(20, 22, 31); ALL 함수 컬럼명 비교연산자 ALL(값들 혹은 서브쿼리) 전체 값을 비교해서 모두 만족해야 true 반환 비교 연산자와 ALL > ALL : 최대값보다 크면 >= ALL : 최대값보다 크거나 같으면 < ALL : 최소값보다 작으면..
[프로그래머스] 방문 길이 - 파이썬(Python) def solution(dirs): move = {'U': (0, 1), 'D': (0, -1), 'R': (1, 0), 'L': (-1, 0)} answer = set() pos_x, pos_y = 0, 0 for i in dirs: x, y = move[i][0], move[i][1] if -5
[프로그래머스] 더 맵게 - 파이썬(Python) import heapq def solution(scoville, K): heapq.heapify(scoville) count = 0 while len(scoville) >= 2 and scoville[0] = K else -1 혹은 import heapq def solution(scoville, K): heapq.heapify(scoville) count = 0 while sco..
백준 14502번 : 연구소 (문제 바로가기) 내 코드 import sys from collections import deque from itertools import combinations def bfs_and_count_zero(graph, virus_pos, n, m): # 너비 우선 탐색 & 빈칸 수 구하기 directions = [(1, 0), (-1, 0), (0, 1), (0, -1)] visited = [[False] * m for _ in range(n)] for i, j in virus_pos: visited[i][j] = True queue = deque(virus_pos) while queue: # 바이러스 전파하기! x, y = queue.popleft() for i in rang..
[프로그래머스] K번째수 - 파이썬(Python) def solution(array, commands): answer = [] for i, j, k in commands: answer.append(sorted(array[i - 1:j])[k - 1]) return answer https://programmers.co.kr/learn/courses/30/lessons/42748 코딩테스트 연습 - K번째수 [1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3] programmers.co.kr
백준 2225번 : 합분해 (문제 바로가기) 내 코드 n, k = map(int, input().split()) dp = [[0] * (k + 1) for _ in range(n + 1)] dp[0][0] = 1 for i in range(n + 1): for j in range(1, k + 1): dp[i][j] = dp[i - 1][j] + dp[i][j - 1] print(dp[n][k] % 1000000000)
백준 7569번 : 토마토 (문제 바로가기) 내 코드 import sys from collections import deque input = sys.stdin.readline # m : 상자 가로 칸 수, n : 상자 세로 칸 수, h : 쌓아올려지는 상자의 수 m, n, h = map(int, input().split()) arr = [] queue = deque() for i in range(h): temp = [] for j in range(n): temp.append(list(map(int, input().split()))) for z in range(m): if temp[j][z] == 1: queue.append((i, j, z)) arr.append(temp) # 6방향 : 위, 아래, 왼쪽..