8000 [23-03-02] dain.py by da-in · Pull Request #141 · da-in/algorithm-study · GitHub
[go: up one dir, main page]

Skip to content

[23-03-02] dain.py #141

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

Merged
merged 5 commits into from
Mar 5, 2023
Merged
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
19 changes: 19 additions & 0 deletions Programmers - 고득점 Kit/[DFS-BFS] 네트워크/dain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from collections import deque

def solution(n, computers):
answer = set()
for i in range(n):
visit = [0 for _ in range(n)]
q = deque([i])
visit[i] = 1
root = i
while q:
cur = q.popleft()
root = min(cur, root)
for j in range(n):
if computers[cur][j] == 1 and visit[j] == 0:
q.append(j)
visit[j] = 1
answer.add(root)

return len(answer)
55 changes: 55 additions & 0 deletions Programmers - 고득점 Kit/[그래프] 방의 개수/dain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
def solution(arrows):
answer = 0
graph = {(0, 0): set()}
dx = [0, 1, 1, 1, 0, -1, -1, -1]
dy = [1, 1, 0, -1, -1, -1, 0, 1]
x = y = nx = ny = 0

def isCross(a, x, y):
if a == 1 and (x, y + 1) in graph.keys() and (x + 1, y) in graph[(x, y + 1)]:
return True
elif a == 3 and (x, y - 1) in graph.keys() and (x + 1, y) in graph[(x, y - 1)]:
return True
elif a == 5 and (x, y - 1) in graph.keys() and (x - 1, y) in graph[(x, y - 1)]:
return True
elif a == 7 and (x, y + 1) in graph.keys() and (x - 1, y) in graph[(x, y + 1)]:
return True
return False

for a in arrows:
nx = x + dx[a]
ny = y + dy[a]
graph[(x, y)].add((nx, ny))
if (nx, ny) in graph.keys():
if (x, y) not in graph[(nx, ny)]:
graph[(nx, ny)].add((x, y))
answer += 1
if isCross(a, x, y):
answer += 1
else:
graph[(nx, ny)] = {(x, y)}
if isCross(a, x, y):
answer += 1
x, y = nx, ny
return answer


# 오일러 정리를 이용한 코드
# v-e+f = 1
# v: 꼭짓점, e:모서리, f:면

def solution(arrows):
# f = 1 + e - v
e = set()
v = {(0, 0)}
dx = [0, 1, 1, 1, 0, -1, -1, -1]
dy = [1, 1, 0, -1, -1, -1, 0, 1]
x = y = nx = ny = 0
for a in arrows:
for _ in range(2):
nx = x + dx[a]
ny = y + dy[a]
v.add((nx, ny))
e.add(frozenset({(nx, ny), (x, y)}))
x, y = nx, ny
return 1 + len(e) - len(v)
20 changes: 20 additions & 0 deletions Programmers - 고득점 Kit/[해시] 베스트앨범/dain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from collections import defaultdict


def solution(genres, plays):
genreCount = defaultdict(int)
genrePlay = defaultdict(list)
answer = []
for i in range(len(genres)):
genreCount[genres[i]] += plays[i]
genrePlay[genres[i]].append((plays[i], i))

genreCount = sorted(genreCount.items(), key=lambda item: item[1], reverse=True)

for genre, _ in genreCount:
genrePlay[genre].sort(key=lambda x: (-x[0], x[1]))
top2 = genrePlay[genre][0:2]
for p in top2:
answer.append(p[1])

return answer
0