-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from codesquad-backend-study/ayaan
Ayaan : 1월 셋째 주
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def solution(word): | ||
alphabets = ['A', 'E', 'I', 'O', 'U'] | ||
answer = [] | ||
order = 0 | ||
|
||
def dfs(target): | ||
nonlocal order | ||
if target == word: | ||
answer.append(order) | ||
if len(target) == 5: | ||
return | ||
|
||
for alphabet in alphabets: | ||
order += 1 | ||
dfs(target + alphabet) | ||
|
||
dfs('') | ||
return answer[0] | ||
|
||
|
||
solution('I') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import collections | ||
|
||
|
||
def bfs(graph, start, visited): | ||
q = collections.deque([start]) | ||
visited[start] = True | ||
cnt = 1 | ||
|
||
while q: | ||
v = q.popleft() | ||
for n in graph[v]: | ||
if not visited[n]: | ||
q.append(n) | ||
visited[n] = True | ||
cnt += 1 | ||
return cnt | ||
|
||
|
||
def solution(n, wires): | ||
answer = n | ||
for i in range(len(wires)): | ||
temp = wires[:] | ||
graph = collections.defaultdict(list) | ||
# i번째 연결을 뺀 graph를 만든다. | ||
for idx, node in enumerate(temp): | ||
if i == idx: | ||
continue | ||
graph[node[0]].append(node[1]) | ||
graph[node[1]].append(node[0]) | ||
# 한쪽의 개수를 구해서 차이를 계산한다. | ||
cnt = bfs(graph, 1, [False] * (n + 1)) | ||
other = n - cnt | ||
answer = min(answer, abs(cnt - other)) | ||
|
||
return answer | ||
|
||
|
||
solution(4, [[1, 2], [2, 3], [3, 4]]) |