Programmers Level.2 메뉴 리뉴얼
from itertools import combinations
def solution(orders, course):
answer = []
cnt = [0] * len(course)
for num in course:
dicts = {}
for order in orders:
for i in combinations(order, num):
i = sorted(i)
i = ''.join(i)
if i not in dicts:
dicts[i] = 1
else:
dicts[i] += 1
for i, j in dicts.items():
if max(dicts.values()) == j and max(dicts.values()) != 1:
answer.append(i)
return sorted(answer)
- 어렵다.. 조합 개념을 사용했고 combination을 사용하니까 문자열이 쪼개져서 다시 합치기 위해 정렬한 후에 join함수를 사용했다. 그 후 최대값에 해당하는 key값들을 추려냈다.
Programmers Level.2 게임 맵 최단거리
from collections import deque
def solution(maps):
print(maps)
if len(maps) == 1 and len(maps[0]) == 1:
return 1
q = deque()
q.append((0, 0))
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
n = len(maps)
m = len(maps[0])
while q:
x, y = q.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if maps[nx][ny] == 1:
maps[nx][ny] = maps[x][y] + 1
q.append((nx,ny))
if maps[n-1][m-1] in (0,1):
return -1
return maps[n-1][m-1]
- 전형적인 bfs 문제.
* 내일 있을 부스트캠프 관련 공부하느라 문제를 많이 못 풀었다. 오늘은 여기까지...
'알고리즘 문제 풀이 > Programmers' 카테고리의 다른 글
[8회차] (0) | 2021.12.12 |
---|---|
[7회차] (0) | 2021.12.10 |
[5회차] (0) | 2021.12.08 |
[4회차] (0) | 2021.12.07 |
[3회차] (0) | 2021.12.06 |
댓글