Skip to content

명환 프로그래머스 숙제(210608) #86

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
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions mhkim/dfsbfs-19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
@file dfsbfs-19.py
@brief 연산자 끼워넣기
@desc dfs
"""

INF = int(1e9)+1

n = int(input())
numbers = list(map(int, input().split()))
ops = list(map(int, input().split())) # +,-,*,/

maxVal = -INF
minVal = INF


def dfs(cnt, hap, ops):
global maxVal
global minVal

if cnt == n:
maxVal = max(hap, maxVal)
minVal = min(hap, minVal)
return

if ops[0] > 0:
ops[0] -= 1
dfs(cnt+1, hap+numbers[cnt], ops)
ops[0] += 1

if ops[1] > 0:
ops[1] -= 1
dfs(cnt+1, hap-numbers[cnt], ops)
ops[1] += 1

if ops[2] > 0:
ops[2] -= 1
dfs(cnt+1, hap*numbers[cnt], ops)
ops[2] += 1

if ops[3] > 0:
ops[3] -= 1
if hap < 0:
dfs(cnt+1, -(-hap//numbers[cnt]), ops)
else:
dfs(cnt+1, hap//numbers[cnt], ops)
ops[3] += 1


dfs(1, numbers[0], ops)

print(maxVal)
print(minVal)
66 changes: 66 additions & 0 deletions mhkim/dfsbfs-20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
@file dfsbfs-20.py
@brief 감시 피하기
@desc
선생과 학생을 제외한 빈 곳 중에서 3곳을 선택하여 벽을 세우고
각 선생이 학생을 감시할 수 있는지 체크 - 네 방향으로 탐색
"""

from itertools import combinations as cb

N = int(input())
graph = [input().split() for _ in range(N)]

dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]

# 빈 곳 좌표
empty = []
# 선생님 좌표
teachers = []

for i in range(N):
for j in range(N):
if graph[i][j] == 'X':
empty.append((i, j))
elif graph[i][j] == 'T':
teachers.append((i, j))


def search():
for t in teachers:
for i in range(4):
nx, ny = t
while 1:
nx += dx[i]
ny += dy[i]

if nx < 0 or nx > N-1 or ny < 0 or ny > N-1 or graph[nx][ny] == 'O':
break

if graph[nx][ny] == 'S':
return False

return True


answer = 0
# 벽 3개 뽑은 경우의 수
for walls in cb(empty, 3):

# 벽 만들기
for x, y in walls:
graph[x][y] = 'O'

# 탐색
if search():
answer = 1
break

for x, y in walls:
graph[x][y] = 'X'

if answer == 1:
print('YES')
else:
print('NO')
67 changes: 67 additions & 0 deletions mhkim/dfsbfs-21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
@file dfsbfs-21.py
@brief 인구 이동
@desc
연합 찾기 - 연결된 나라 (나라마다 체크)
인구 이동 없을 때까지 반복하고 / 연합 국가끼리 인구 분배

deque는 탐색을 위한 / yeonhap은 연합인 나라 저장 공간
"""

import sys
from collections import deque

N, L, R = map(int, sys.stdin.readline().split())
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]


def search(x, y, num):
# x, y와 연합인 나라
yeonhap = []
yeonhap.append((x, y))
total = A[x][y] # 연합의 전체 인구 수
union[x][y] = num # 연합 번호

# BFS
q = deque([(x, y)])
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 < N and union[nx][ny] == -1:
if L <= abs(A[nx][ny] - A[x][y]) <= R:
q.append((nx, ny))
yeonhap.append((nx, ny))
union[nx][ny] = num
total += A[nx][ny]

# 인구 분배
nCountry = len(yeonhap)
for a, b in yeonhap:
A[a][b] = total // nCountry


days = 0
while True:
# 연합 초기화
union = [[-1]*N for _ in range(N)]
num = 0 # 연합국 번호

# 각 나라마다 체크
for i in range(N):
for j in range(N):
if union[i][j] == -1: # 아직 처리되지 않은 나라
search(i, j, num)
num += 1

# 모든 인구 이동
if num == N * N:
break

days += 1

print(days)
13 changes: 13 additions & 0 deletions mhkim/programmers/K번째수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
@file K번째수.py
@brief 정렬 - Level 1
@desc
"""


def solution(array, commands):
answer = []
for i in commands:
arr = sorted(array[i[0]-1:i[1]])
answer.append(arr[i[2]-1])
return answer
63 changes: 63 additions & 0 deletions mhkim/programmers/N으로표현.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
@file N으로표현.py
@brief 동적계획법 - Level 3
@desc
1번 사용한 케이스, 2번 사용한 케이스
3번 사용한 케이스 -> 1번과 2번 연산 / 2번과 1번 연산
4번 -> 1번과 3번, 2번과 2번, 3번과 1번 ... 8번까지

구할 수 있는 케이스를 구하고 찾고자 하는 값이 그 케이스들에 존재하는지 확인
"""

# Ver1. 연산 전체
def solution(N, number):
dp = [[]]

x = 0
for i in range(1, 9):
dp.append(set())
x = 10*x + N
dp[i].add(x) # N, NN, NNN...

for j in range(i):
# 연산자 케이스
for op1 in dp[j]:
for op2 in dp[i-j]:
dp[i].add(op1 + op2)
dp[i].add(op1 - op2)
dp[i].add(op1 * op2)
if op2 != 0:
dp[i].add(op1 // op2)

if number in dp[i]:
return i

return -1

# Ver2. 반만 연산
def solution(N, number):
dp = [[]]

x = 0
for i in range(1, 9):
dp.append(set())
x = 10 * x + N
dp[i].add(x) # N, NN, NNN...

for j in range((i//2)+1):
# 연산자 케이스
for op1 in dp[j]:
for op2 in dp[i-j]:
dp[i].add(op1 + op2)
dp[i].add(op1 - op2)
dp[i].add(op2 - op1)
dp[i].add(op1 * op2)
if op1 != 0:
dp[i].add(op2 // op1)
if op2 != 0:
dp[i].add(op1 // op2)

if number in dp[i]:
return i

return -1
46 changes: 46 additions & 0 deletions mhkim/programmers/가장먼노드.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import heapq

INF = int(1e9)

graph = []
distance = []


def dijkstra(start):
global graph
global distance

q = []
heapq.heappush(q, (0, start))
distance[start] = 0

while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue

for i in graph[now]:
cost = dist + i[1]
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))


def solution(n, edge):
answer = 0
global graph
global distance

graph = [[] for i in range(n+1)]
distance = [INF]*(n+1)

for a, b in edge:
graph[a].append((b, 1))
graph[b].append((a, 1))

dijkstra(1)

maxDist = max(distance[1:])
answer = distance[1:].count(maxDist)

return answer
24 changes: 24 additions & 0 deletions mhkim/programmers/기능개발.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from collections import deque
import math

def solution(progresses, speeds):
answer = []
q = deque()

length = len(progresses)
for i in range(length):
q.append(math.ceil((100-progresses[i])/speeds[i]))

tmp = q.popleft()
cnt = 1
while q:
if tmp < q[0]:
answer.append(cnt)
tmp = q.popleft()
cnt = 1
else:
q.popleft()
cnt += 1
answer.append(cnt)

return answer
22 changes: 22 additions & 0 deletions mhkim/programmers/더맵게.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
@file 더맵게.py
@brief 힙 - Level 2
@desc
"""
import heapq


def solution(scoville, K):
answer = 0

heapq.heapify(scoville)
while scoville[0] < K:
if len(scoville) < 2:
return -1

first = heapq.heappop(scoville)
second = heapq.heappop(scoville)
heapq.heappush(scoville, first+second*2)
answer += 1

return answer
29 changes: 29 additions & 0 deletions mhkim/programmers/모의고사.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
@file 모의고사.py
@brief 완전탐색 - Level 1
@desc
"""


def solution(answers):
answer = []

supoOne = [1, 2, 3, 4, 5]
supoTwo = [2, 1, 2, 3, 2, 4, 2, 5]
supoThree = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
correct = [0, 0, 0]

for i in range(len(answers)):
if answers[i] == supoOne[i % len(supoOne)]:
correct[0] += 1
if answers[i] == supoTwo[i % len(supoTwo)]:
correct[1] += 1
if answers[i] == supoThree[i % len(supoThree)]:
correct[2] += 1

for i in range(len(correct)):
if correct[i] == max(correct):
answer.append(i+1)

answer.sort()
return answer
Loading