본문 바로가기

분류 전체보기

(49)
RuntimeError: Set changed size during iteration ✅ 파이썬(Python) 집합(Set) 자료구조와 관련된 에러( 딕셔너리와 deque 자료구조에서도 해당된다. ) # wrongs = {(0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 0, -1), (0, -1, 0, 0), (0, 0, -1, 0), (0, 1, 0, 0)}for i in s: re_r = list(i[2:])+(list(i[:2])) if tuple(re_r) in routes: s.remove(i) 👉 집합(또는 딕셔너리, deque)을 순회할 때, 집합(또는 딕셔너리, deque) 크기가 변경되면 발생한다.👉 파이썬 3.x 에서는 리스트를 list(a) 형태로 복사하여 해결한다.# corrects = {(0, 0, 0, 1), (0, 0, 1, ..
[Lv2.] 해시 - 완주하지 못한 선수 👉 사용 언어 : PYTHON3 완주하지 못한 선수 ✅ Solution def solution(participant, completion): res = {name:participant.count(name) for name in participant} for comp_name in completion: res[comp_name] -= 1 return [name for name in res.keys() if res[name] > 0][0] : 효율성 테스트에서 시간초과 발생 def solution(participant, completion): res = {name:0 for name in participant} for name in participant: res[name] += 1 for comp_name ..
[Lv2.] 스택/큐 - 올바른 괄호 👉 사용 언어 : PYTHON3 올바른 괄호 ✅ Solution def solution(s): stack = [] for i in s: if i == '(': stack.append(i) else: if len(stack) == 0 or stack[-1] == ')': return False else: stack.pop() return len(stack) == 0 👉 문제 설명 및 제한 사항 괄호가 바르게 짝지어졌다 ➡️ '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다 "()()" 또는 "(())()" : 올바른 괄호 ")()(" 또는 "(()(" : 올바르지 않은 괄호 매개변수 : '(' 또는 ')' 로만 이루어진 문자열 s 길이
[3/29] Kakao Internship 기출문제 풀이 👉 사용 언어 : PYTHON3 가장 많이 받은 선물 ✅ Solution def gift_idx(idx_table, name1, name2): if idx_table[name1] idx_table[name2]: return name1 else: return ' ' def solution(friends, gifts): max = -1 idx_name = {i:name for i, name in enumerate(friends)} name_idx = {name:i for i, name in enumerate(friends)} idx_table = {name:0 for name in friends} ans =..
[Lv2.] 힙 - 더 맵게 👉 사용 언어 : PYTHON3 타겟넘버 ✅ Solution import heapq def solution(scoville, K): cnt = 0 heapq.heapify(scoville) while scoville[0] 1: new = heapq.heappop(scoville) + heapq.heappop(scoville)*2 heapq.heappush(scoville, new) cnt += 1 if scoville[0] < K: return -1 return cnt 👉 문제 설명 및 제한 사항 모든 음식의 스코빌 지수를 K 이상으로 만들기 모든 음식의 스코빌 지수가 K 이상이 될 때까지 반복하여 섞는다❗️ 섞은 음식의 스코빌 지수 = 가장 맵지 않은 음식의 ..
[Lv2.] 덧칠하기 👉 사용 언어 : PYTHON3 덧칠하기 ✅ Solution import math def solution(n, m, section): cnt = 0 for i in range(len(section)): if section[-i] - section[-i-1] > m: cnt += 1 return math.ceil((section[-1] - section[0] +1) / m) - cnt : 실패 케이스 발생 (54점) : 문제 예시에 끼워맞추는 풀이이기 때문에 잘못된 접근..! def solution(n, m, section): cnt = 0 i = 1 length = len(section) while i
[Lv2.] 정렬 - H-Index 👉 사용 언어 : PYTHON3 H-Index ✅ Solution def solution(citations): citations = sorted(citations, reverse=True) for i in range(citations[0], 0, -1): cnt = 0 for j in citations: if j = i and len(citations)-cnt
[Lv2.] 정렬 - 가장 큰 수 ✅ Solution from itertools import permutations def solution(numbers): ans = [] numbers = list(map(str, numbers)) ans = sorted(list(map(''.join, permutations(numbers, len(numbers))))) return ans[-1] 👉 시간 초과 발생❗️ : 굳이 순열이 아니더라도 문제의 예시들을 잘 보고 규칙을 확인한 뒤, 그에 맞게 정렬만 잘 수행해주고 이어 붙여도 풀이 가능❗️ def solution(numbers): numbers = list(map(str, numbers)) numbers.sort(key=lambda x:x*3, reverse=True) if numbers.co..