Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Programmers] 23-08-28 #1049

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from collections import deque

def solution(maps):
bfs((0, 0), maps)
if maps[len(maps)-1][len(maps[0])-1] == 1:
return -1
return maps[len(maps)-1][len(maps[0])-1]

def bfs(start, maps):
q = deque([start])
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
while q:
x, y = q.popleft()
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < len(maps) and 0 <= ny < len(maps[0]) and maps[nx][ny] == 1:
maps[nx][ny] = maps[x][y] + 1
q.append((nx, ny))

print(solution([[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]]))
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from collections import deque

def solution(n, computers):
answer = 0
global visited
visited = [False] * n
for i in range(n):
if not visited[i]:
bfs(i, n, computers)
answer += 1
return answer

def bfs(start, n, computers):
q = deque([start])
while q:
com1 = q.popleft()
for com2 in range(n):
if computers[com1][com2] == 1 and not visited[com2]:
visited[com2] = True
q.append(com2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from collections import deque


def solution(begin, target, words):
if target not in words:
return 0

words.append(begin)
words_dict = {word: [] for word in words}

for i in range(len(words)):
for j in range(i + 1, len(words)):
cnt = 0
for idx in range(len(words[i])):
if words[i][idx] != words[j][idx]:
cnt += 1
if cnt == 1:
words_dict[words[i]].append(words[j])
words_dict[words[j]].append(words[i])


q = deque([(begin, 0)])
visited = []
while q:
now, cnt = q.popleft()
if target in words_dict[now]:
return cnt + 1
elif words_dict[now]:
for w in words_dict[now]:
if w not in visited:
q.append((w, cnt + 1))
visited.append(w)
return 0

print(solution("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"]))
print(solution("hit", "cog", ["hot", "dot", "dog", "lot", "log"]))
print(solution("aab", "aba", ["abb", "aba"]))
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def solution(tickets):
global answer
answer = []
visited = [False] * len(tickets)

dfs("ICN", ["ICN"], visited, tickets)
answer.sort()
return answer[0]


def dfs(dept, arr, visited, tickets):
if len(arr) == len(tickets)+1:
answer.append(arr)

for i in range(len(tickets)):
if tickets[i][0] == dept and not visited[i]:
visited[i] = True
dfs(tickets[i][1], arr+[tickets[i][1]], visited, tickets)
visited[i] = False

print(solution([["ICN", "JFK"], ["HND", "IAD"], ["JFK", "HND"]]))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def solution(numbers, target):
global answer
answer = 0
dfs(0, numbers, 0, target)
return answer

def dfs(depth, numbers, tmp, target):
global answer

if depth == len(numbers):
if tmp == target:
answer += 1
return

dfs(depth+1, numbers, tmp+numbers[depth], target)
dfs(depth+1, numbers, tmp-numbers[depth], target)