목록파이썬 (170)
컴공생의 다이어리
[프로그래머스] 전화번호 목록 - 파이썬(Python) def solution(phone_book): phone_book.sort() for p1, p2 in zip(phone_book, phone_book[1:]): if p2.startswith(p1): return False return True https://programmers.co.kr/learn/courses/30/lessons/42577 코딩테스트 연습 - 전화번호 목록 전화번호부에 적힌 전화번호 중, 한 번호가 다른 번호의 접두어인 경우가 있는지 확인하려 합니다. 전화번호가 다음과 같을 경우, 구조대 전화번호는 영석이의 전화번호의 접두사입니다. 구조 programmers.co.kr
[프로그래머스] 완주하지 못한 선수 - 파이썬(Python) import collections def solution(participant, completion): answer = collections.Counter(participant) - collections.Counter(completion) return list(answer.keys())[0] https://programmers.co.kr/learn/courses/30/lessons/42576 코딩테스트 연습 - 완주하지 못한 선수 수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수 programmers...
[프로그래머스] 카펫 - 파이썬(Python) def solution(brown, yellow): size = brown + yellow for i in range(3, brown): if size % i == 0: j = size // i if (i - 2) * (j - 2) == yellow: return sorted([i, j], reverse=True) 혹은 def solution(brown, yellow): # 둘레 길이 활용 for i in range(1, int(yellow ** (1 / 2)) + 1): if yellow % i == 0: if 2 * (i + yellow // i) == brown - 4: return [yellow // i + 2, i + 2] 아래는 근의 공식을 활용한..
백준 2075번 : N번째 큰 수 (문제 바로가기) 내 코드 import sys, heapq input = sys.stdin.readline heap = [] for _ in range(int(input())): arr = list(map(int, input().split())) if not heap: for a in arr: heapq.heappush(heap, a) else: for a in arr: if heap[0] < a: heapq.heappush(heap, a) heapq.heappop(heap) print(heap[0])
백준 1012번 : 유기농 배추 (문제 바로가기) 내 코드 import sys sys.setrecursionlimit(10**6) def dfs(x, y): visited[x][y] = True directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] for dx, dy in directions: nx, ny = x + dx, y + dy if nx = n or ny = m: continue if array[nx][ny] and not visited[nx][ny]: dfs(nx, ny) for _ in range(int(input())): m, n, k = map(int, input().split()) array = [[0] * m fo..
백준 2014번 : 소수의 곱 (문제 바로가기) 내 코드 import heapq k, n = map(int, input().split()) prime = list(map(int, input().split())) h = prime[:] heapq.heapify(h) for i in range(n): num = heapq.heappop(h) for j in prime: heapq.heappush(h, num * j) if num % j == 0: break print(num)
계수 정렬(Counting Sort) 계수 정렬은 특정한 조건이 부합될 때만 사용할 수 있지만 데이터 수가 많더라도 중복된 값이 많이 분포돼있는 배열을 정렬할 때 효과적이고 빠른 정렬 알고리즘이다. 최대, 최소 값 차이가 100만 이하일 경우 효과적이다. 카운팅 정렬이라고 하기도 한다. 선택, 삽입, 퀵 정렬처럼 데이터를 비교하며 위치를 변경하는 비교 기반의 정렬 알고리즘이 아니다. 계수 정렬의 조건은 아래와 같다. 데이터의 크기 범위가 제한된 경우 ex) 0 ~ 100 까지의 점수를 정렬하는 경우 데이터가 양의 정수인 경우 데이터가 실수인 경우 무한의 범위를 가질 수 있으므로 1번 조건에 부합하지 못함 가장 큰 데이터와 가장 작은 데이터의 차이가 1,000,000(백만)을 넘지 않는 경우 필수적인 조건..
백준 4948번 : 베르트랑 공준 (문제 바로가기) 내 코드 import sys n_max = 123456 is_prime = [True] * (2 * n_max + 1) is_prime[0], is_prime[1] = False, False for i in range(2, int((2 * n_max) ** 0.5) + 1): if is_prime[i]: j = 2 while (i * j)
최소공배수(Lowest Common Multiple, LCM) 공배수(common multiple)란 두 수 이상의 여러 수의 공통된 배수를 의미 최소공배수(LCM)란 두 수 이상의 여러 수의 공배수 중 최소인 수를 가리킴 기본적인 방법 def lcm(a, b): for i in range(max(a, b), (a * b) + 1): if i % a == 0 and i % b == 0: return i 최대공약수 활용 def gcd(a, b): # 최대공약수 while b > 0: a, b = b, a % b return a def lcm(a, b): return a * b / gcd(a, b) 혹은 import math def lcm(a, b): return a * b / math.gcd(a, b) 파..
최대공약수(Greatest Common Divisor, GCD) 공약수(common divisor)란 두 수 이상의 여러 수의 공통된 약수를 의미 최대공약수(GCD)란 두 수 이상의 여러 수의 공약수 중 최대인 수를 가리킴 최대공약수가 1이면 두 수는 서로소(coprime) 관계 기본적인 방법 def gcd(a, b): for i in range(min(a, b), 0, -1): if a % i == 0 and b % i == 0: return i 유클리드 호제법 사용 def gcd(a, b): while b > 0: a, b = b, a % b return a # or def gcd(a, b): if a % b == 0: return b elif b == 0: return a else: return g..