Skip to content

Instantly share code, notes, and snippets.

@erdoslibrary
erdoslibrary / solution.py
Created June 2, 2025 12:06
[코테합][시뮬레이션]이진변환
from typing import List
def decimal_to_binary(n: int) -> str:
if n == 0:
return '0'
result: str = ""
while (n > 0):
result = str(n % 2) + result
n //= 2
return result
@erdoslibrary
erdoslibrary / solution.py
Created May 17, 2025 04:13
[코테합][정렬]지형 이동
from heapq import heappush, heappop
from typing import List, Tuple
def solution(land: List[List[int]], height: int) -> int:
n = len(land)
visited: List[List[bool]] = [[False] * n for _ in range(n)]
total_cost = 0
# 상, 우, 하, 좌
directions: List[Tuple[int, int]] = [(-1, 0), (0, 1), (1, 0), (0, -1)]
@erdoslibrary
erdoslibrary / solution.py
Created May 16, 2025 09:03
[코테합][정렬]튜플
from typing import List
def solution(s: str) -> List[int]:
inner = s[2:-2]
subset_strings = inner.split("},{")
subsets: List[List[int]] = []
for subset_str in subset_strings:
number_strings = subset_str.split(",")
numbers = [int(num) for num in number_strings]
@erdoslibrary
erdoslibrary / cmp_to_key.py
Created May 15, 2025 05:41
[코테합][정렬]가장 큰 수
from functools import cmp_to_key
from typing import List
def solution(numbers:List[int]) -> str:
strs: List[str] = list(map(str, numbers))
def compare(a: str, b: str) -> int:
if a + b > b + a:
return -1
if a + b < b + a:
@erdoslibrary
erdoslibrary / solution.py
Last active May 14, 2025 02:40
[코테합][정렬]k번째 수
from typing import List, Tuple
def solution(array: List[int], commands: List[Tuple[int, int, int]]) -> List[int]:
answer: List[int] = []
for i, j, k in commands:
answer.append(sorted(array[i-1:j])[k-1])
return answer
@erdoslibrary
erdoslibrary / type_hint.py
Created May 13, 2025 01:44
[코테합][정렬]문자열 내림차순 배치
def solution(s:str) -> str:
return "".join(sorted(s, reverse=True))
@erdoslibrary
erdoslibrary / without_lambda.py
Created May 12, 2025 15:12
[코테합][정렬]문자열 내 마음대로 정렬하기
from operator import itemgetter
def solution(strings, n):
alphabet_sorted = sorted(strings)
return sorted(alphabet_sorted, key = itemgetter(n))
@erdoslibrary
erdoslibrary / solution.py
Last active May 9, 2025 11:21
[코테합][백트래킹]양궁문제(python)
from itertools import combinations_with_replacement
from collections import Counter
def solution(n, info):
maxdiff, max_comb = 0, {}
# 주어진 조합에서 각각의 점수 계산
def calculate_score(combi):
ryon, apeach = 0, 0
for score in range(1, 11):
@erdoslibrary
erdoslibrary / solution.py
Created May 6, 2025 11:15
[코테합][백트래킹]사라지는 발판(python)
def solution(board, aloc, bloc):
ROW, COL = len(board), len(board[0]) # 게임판의 행, 열 저장
DR, DC = [-1, 0, 1, 0], [0, 1, 0, -1]
# 주어진 위치가 유효한 위치인가?
def is_valid_pos(r, c):
return 0 <= r < ROW and 0 <= c < COL
# 재귀적 호출
def recursive_func(alpha_pos, beta_pos, visited, step):
@erdoslibrary
erdoslibrary / dfs_solution.py
Last active April 30, 2025 14:35
[코테합][그래프]n-퀸
def backtrack(col_positions, current_row, board_size):
total_solutions = 0
if current_row == board_size:
return 1
for col in range(board_size):
col_positions[current_row] = col
for prev_row in range(current_row):
same_col = (col_positions[prev_row] == col)
same_diag = abs(col_positions[prev_row] - col) == current_row - prev_row
if same_col or same_diag: