코딩테스트 7

[프로그래머스] 더 맵게 - Python

섞은 음식의 스코빌 지수 = 가장 맵지 않은 음식의 스코빌 지수 + (두 번째로 맵지 않은 음식의 스코빌 지수 * 2) 처음 문제를 접근했을 때는 아래와 같이 deque으로 접근했다. deque으로 접근하게 되면, 매번 Sorted를 통해서 정렬을 해주어야 하기 때문에 시간 초과가 발생했다. from collections import deque def solution(scoville, K): scv = deque(sorted(scoville)) cnt = 0 while min(scv) < K: cnt += 1 x, y = scv.popleft(), scv.popleft() scv.append(x + y*2) scv = deque(sorted(scv)) if len(scv) == 1: return -1 r..

Python/Algorithm 2023.11.22

[프로그래머스] 피로도 - Python

해당 문제는 DFS로 해결이 가능하다. DFS 는 깊이 우선 탐색으로, 한번에 던전을 갈 수 있는 끝까지 탐색한 후 이전 단계로 돌아가는 작업을하면 된다. 이때, 방문 여부와 방문 횟수를 초기화하고 다시 탐색을 하면 끝이다. answer = 0 def solution(k, dungeons): global answer def DFS(k, cnt, dungeons, visited): global answer answer = max(answer, cnt) # 최대 방문 횟수 for i in range(len(dungeons)): if visited[i] == 0 and k >= dungeons[i][0]: visited[i] = 1 # 방문 DFS(k-dungeons[i][1], cnt+1, dungeons,..

Python/Algorithm 2023.11.22

[프로그래머스] n^2 배열 자르기

프로그래머스 월간 코드 챌린지 3에 출제된 Level 2 난이도의 문제다. 초기 풀이는 extend를 활용해서 아래와 같이 작성하였다. def solution(n, left, right): ans = [] for i in range(n): l = list(range(i+1, n+1)) ans.extend([l[0]]*(n-len(l)) + l) return ans[left:right+1] 일부 케이스에서는 통과가 되었으나, 시간 초과가 발생한다. list를 생성하고 extend하는 과정에서 오래 걸리는 것으로 확인된다. 시간 복잡도를 줄이기 위해 몫, 나머지 개념을 도입했다. (0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) (2, 0) (2, 1) (2, 2) 1 2 3 2 2..

Python/Algorithm 2023.10.23

[프로그래머스] 메뉴 리뉴얼 - Python

해당 문제는 2021 KAKAO BLIND RECRUITMENT에서 출제된 문제다. 프로그래머스 기준 Level 2에 해당한다. 이번 문제는 combinations 함수와 Counter 함수를 사용하면 쉽게 해결이 가능하다. from itertools import combinations from collections import Counter def solution(orders, course): answer = [] for n in course: candidates = [] for menu in orders: for l in combinations(menu, n): candidates.append(''.join(sorted(l))) candidates = Counter(candidates).most_co..

Python/Algorithm 2023.09.05

[프로그래머스] 비밀지도 - Python

해당 문제는 2018 KAKAO BLIND RECRUITMENT에서 출제된 문제다. 프로그래머스 기준 Lvel 1에 해당하는 문제로 10진법을 2진법으로 변환하여 해결할 수 있다. def GetMap(arr1, arr2): answer = '' for a1, a2 in zip(arr1, arr2): if a1 == '0' and a2 == '0': answer += ' ' else: answer += '#' return answer def solution(n, arr1, arr2): answer = [] array1, array2 = [], [] for a1, a2 in zip(arr1, arr2): a1 = bin(a1)[2:].rjust(n, '0') a2 = bin(a2)[2:].rjust(n, '..

Python/Algorithm 2023.09.03

[프로그래머스] 오픈채팅방 - Python

해당 문제는 2019 KAKAO BLIND RECRUITMENT에 출제된 문제다. 프로그래머스 기준 Level 2에 해당하며, 다음과 같이 풀 수 있다. def solution(record): answer = [] user_list = {} for rec in record: v = rec.split() if v[0] in ['Enter', 'Change']: user_list[v[1]] = v[2] for rec in record: v = rec.split() if v[0] == 'Enter': answer.append(f'{user_list[v[1]]}님이 들어왔습니다.') elif v[0] == 'Leave': answer.append(f'{user_list[v[1]]}님이 나갔습니다.') retur..

Python/Algorithm 2023.09.03

[프로그래머스] 달리기 경주 - Python

문제를 살펴보면 단순히 역전한 결과를 스왑(swap)하면 되지 않을까 라고 생각해서, list를 이용하여 아래와 같이 문제를 풀어보았다. # 시간초과 def solution(players, callings): for calls in callings: idx = players.index(calls) values = players.pop(idx) players.insert(idx-1, values) return players def solution(players, callings): for calls in callings: idx = players.index(calls) players[idx-1], players[idx] = players[idx], players[idx-1] return players Ru..

Python/Algorithm 2023.08.24
반응형