Skip to content

20210329 - 20210402 Algorithm Study #26

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

Open
wants to merge 6 commits into
base: master
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 59 additions & 0 deletions 2021_3_MAR/boj_12907_동물원.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 1번부터 N번 번호매겨진 동물
# 토끼 or 고양이
# 키는 모두 다르다
# 같은 동물 중 자신보다 키가 큰 동물의 수를 물을 때
# 각 대답을 어떤 동물이 했는지 알아내려 함

# 출력: 가능한 조합의 수

# 체크할 조건
# 1 <= N <= 40 (동물의 수)
# 0 <= 대답 <= 40
# 각 대답은 동물의 수보다 많을 수도 있고 0일 수도 있다.

# 대답 숫자가 적을수록 키가 크다.
# 0은 2개까지만 가능하다.
# 모든 수는 2개 초과해서 나올 수 없다. (토끼, 고양이 두 종류)
# 키가 모두 다르므로 0, 1, 2, 3... 순서로 와야한다.
# 0, 0, 1, 1 ... 짝 맞는 만큼 2의 n제곱 해서 개수를 구한다.
# 짝이 딱 떨어지는 경우 n제곱, 이외엔 n+1제곱
# (0, 0, 1, 1, 2, 2의 경우의 수 == 0, 0, 1, 1, 2)
# 짝 맞은 후에는 가능한 경우여야 한다.
# 0, 0, 1, 1, 2, 3, 4, 5 (O)
# 0, 0, 1, 1, 2, 3, 4, 6 (X)

def impossible():
print(0)
exit()

def solution(N, answers):
answers.sort()
check = [0] * 41
for answer in answers:
check[answer] += 1
if check[answer] > 2: impossible()

power = 0
for i in range(len(check)):
if check[i] == 2:
power += 1
else:
break
if check[i]:
while i < len(check):
if check[i] == 1: i += 1
elif check[i] == 2: impossible()
else: break
else:
i += 1
for j in range(i, len(check)):
if check[j]: impossible()
print(2**power)
return

for j in range(i, len(check)):
if check[j]: impossible()
print(2 ** (power + 1))

if __name__ == '__main__':
solution(int(input()), list(map(int, input().split())))
31 changes: 31 additions & 0 deletions 2021_3_MAR/boj_13458_시험감독.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# N개 시험장, i번 시험장 응시자수 Ai
# 총감독관 부감독관 각각 B명, C명 감시 가능
# 총감독관은 1명, 부감독관은 여러명 가능
# 응시생 모두 감시할 때 필요한 최소 감독관 수

# 출력: 필요한 감독관의 최소 수

# 체크할 조건
# 1 <= N <= 1000000 (시험장 개수)
# 1 <= Ai <= 1000000 (응시자 수)
# 1 <= B,C <= 1000000
# 예제를 보니 부감독관이 없어도 되는듯.

# 시험장마다 총감독관 하나 배치하고
# 남은 응시생 수를 커버할 수 있는 부감독관 수를 찾는다.

def solution(N, A, B, C):
cnt = 0
for num in A:
num -= B
cnt += 1
if num > 0:
if num % C: cnt += 1
cnt += num // C
print(cnt)

if __name__ == '__main__':
N = int(input())
A = list(map(int, input().split()))
B, C = map(int, input().split())
solution(N, A, B, C)
34 changes: 34 additions & 0 deletions 2021_3_MAR/programmers_42839_소수찾기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 한자리 숫자가 적힌 종이 조각
# '013' == 0, 1, 3

# 출력: 종이 조각으로 만들 수 있는 소수 개수

# 체크할 조건
# 1 <= len(numbers) <= 7

# 소수 판별 함수를 만든 후 완전탐색

from itertools import permutations

def is_prime(number):
if number <= 1: return False
i = 2
while i * i <= number:
if not number % i:
return False
i += 1
return True

def solution(numbers):
nums = set()
for i in range(1, len(numbers)+1):
for item in permutations(numbers, i):
num = int(''.join(item))
nums.add(num)

answer = 0
for num in nums:
if is_prime(num):
answer += 1

return answer
42 changes: 42 additions & 0 deletions 2021_4_APR/programmers_49189_가장먼노드.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 가장 멀리 떨어진 노드:
# 최단 경로로 이동했을 때 간선의 개수가 가장 많은 노드

# 출력: 1번 노드에서 가장 멀리 떨어진 노드 개수

# 체크할 조건
# 양방향 그래프
# 2 <= n <= 20000 (노드의 개수)

# 가중치가 없으므로 BFS한 가장 마지막 노드들의 개수를 출력

from collections import deque

def BFS(graph, n):
q = deque([1])
visited = [0] * (n+1)
visited[1] = 1
while q:
cnt = len(q)
for _ in range(len(q)):
node = q.popleft()
if node in graph:
for v in graph.get(node):
if not visited[v]:
visited[v] = 1
q.append(v)
return cnt

def solution(n, edge):
graph = dict()
for s, e in edge:
if s in graph:
graph[s].append(e)
else:
graph[s] = [e]
if e in graph:
graph[e].append(s)
else:
graph[e] = [s]

answer = BFS(graph, n)
return answer
29 changes: 29 additions & 0 deletions 2021_4_APR/programmers_60057_문자열압축.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 1개 이상의 단위로 잘라서 압축

# 출력: 압축한 문자열 중 가장 짧은 것의 길이

# 체크할 조건
# 문자열은 제일 앞에서 정해진 길이 만큼 잘라야 함.
# 앞에서부터 잘라서 순서대로 리스트에 넣는다 [문자열, 개수]
# 모두 리스트에 넣은 후 압축된 문자열의 길이를 구하여 갱신한다.

def solution(s):
answer = len(s)
for i in range(1, len(s)):
cnt = 0
temp = []
temp.append([s[:i], 1])
j = i
while j < len(s):
string = s[j:j+i]
if temp[-1][0] == string:
temp[-1][1] += 1
else:
temp.append([string, 1])
j += i
for word, num in temp:
cnt += len(word)
if num > 1:
cnt += len(str(num))
answer = min(answer, cnt)
return answer