Skip to content

Instantly share code, notes, and snippets.

View inspirit941's full-sized avatar

Donggeon Lee inspirit941

View GitHub Profile
from itertools import combinations
from collections import defaultdict
def solution(orders, course):
orders = [sorted(i) for i in orders]
counter = defaultdict(int)
max_count = defaultdict(int)
for order in orders:
for c in course:
if len(order) < c:
continue
import re
def solution(new_id):
# 1. 소문자 치환
new_id = new_id.lower()
# 2. 알파벳 소문자, 숫자, -, _, . 빼고 제거
new_id = re.sub(r"[^a-z0-9\-\_\.]","",new_id)
# 3. . 두 개 이상이면 하나로 치환
new_id = re.sub(r"[\.]+", ".", new_id)
# 4. 마침표가 처음이나 끝에 있으면 제거
if new_id.startswith("."): new_id = new_id[1:]
from collections import Counter
def solution(a):
# a 배열 element 각 원소의 등장횟수를 센다.
elements = Counter(a)
answer = -1
# a에 있는 각 원소 k를 기준으로
# k값을 기준으로 스타수열을 만들 수 있는지 검증한다.
for k in elements.keys():
# k의 등장횟수가 스타수열에 사용된 공통인자 횟수보다 작거나 같으면
from collections import Counter
def solution(s):
# 0 개수, 반복 횟수를 저장
zeros, cnt = 0, 0
while s != "1":
# 횟수 카운트
cnt += 1
# 제거한 0의 개수 저장
zeros += Counter(s)['0']
# 1의 개수를 정수로 변환 -> bin()으로 이진수 변환 -> str로 문자열 변환.
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# 겹치는 구간파악을 위해, 먼저 정렬
merged = []
for arr in sorted(intervals, key = lambda x: x[0]):
# arr의 시작점이 구간 안에 있는 경우
if merged and arr[0] <= merged[-1][1]:
# arr의 끝점과 merged 끝점 중 더 긴 것을 merged 끝점에 업데이트
merged[-1][1] = max(merged[-1][1], arr[1])
else:
from collections import defaultdict
import heapq
class Solution:
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
# 1. 모든 노드가 신호를 받을 수 있는가?
# 2. 모든 노드에 도달할 때까지의 시간은?
# = 가장 오래 걸리는 노드까지의 최단거리 = 다엑스트라 알고리즘으로 해결 가능
# 단방향 = 인접연결리스트로 그래프 정보 구현
graph = defaultdict(list)
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
# 트리 구조의 dfs로 해결하기
result = []
def dfs(idx, path):
result.append(path)
# idx 순서대로 경로를 만들면서 dfs 연산
for next_idx in range(idx, len(nums)):
dfs(next_idx + 1, path + [nums[next_idx]])
def solution(n,a,b):
# a < b 이도록 변환.
a, b = min(a,b), max(a,b)
count = 1
# b-a == 1 이면 a와 b가 만나는 시점.
# 더 큰 값인 b가 홀수면, b는 a가 아니라 b 오른쪽 사람과 겨뤄야 한다.
# ex) a, b = 2, 3일 경우
# 1-2, 3-4 형태로 먼저 대전한 다음에야 만날 수 있음.
while b - a != 1 or b % 2 != 0:
o_a, r_a = divmod(a, 2)
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# 부분 문자열. 연속된 문자열이어야 한다.
# 슬라이딩 윈도우 + 투 포인터로 사이즈 조절하기
start, end, max_len = 0, 0, 0
# 이미 존재하는 String -> index 기록
exists = dict()
for idx, char in enumerate(s):
# 이미 있는 문자열이고, 현재 윈도우 시작위치가 해당 문자열 등장위치보다 이전일 경우
# window의 시작지점을 해당 문자열 위치 다음으로 옮긴다.
class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
# 현재 온도를 stack에 저장.
# 다음날 온도가 현재보다 높으면 stack 값을 꺼내서, 날씨차이 저장.
# 낮으면 스택 값은 계속 유지됨.
stack = []
answer = [0 for _ in range(len(T))]
for idx, value in enumerate(T):
while stack and value > T[stack[-1]]:
last = stack.pop()