Skip to content
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
15 changes: 15 additions & 0 deletions Baekjoon/hyeon-gyu/회의실배정/1931.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys
input = sys.stdin.readline
N = int(input().strip())
arr = []
for i in range(N):
arr.append(tuple(map(int,(input().split()))))
arr.sort(key = lambda x : (x[1], x[0]))

count = 0
prev_time = 0
for (x, y) in arr:
if x >= prev_time:
prev_time = y
count += 1
print(count)
2 changes: 2 additions & 0 deletions Baekjoon/hyeon-gyu/회의실배정/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

종료시간을 기준으로 정렬해서 문제를 해결
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# def solution(board, skill):
# answer = len(board) * len(board[0])
# chk = [[0] * len(board[0]) for _ in range(len(board))]
def solution(board, skill):
answer = len(board) * len(board[0])
chk = [[0] * len(board[0]) for _ in range(len(board))]

# #2중루프 안돌고 좌표들에 값을 바꾸는 방법 찾기
# for type,r1,c1,r2,c2,degree in skill:
# if type == 1:
# for i in range(r1,r2+1):
# for j in range(c1,c2+1):
# board[i][j] -= degree
# if board[i][j] <= 0 and chk[i][j] == 0:
# chk[i][j] = 1
# answer -= 1
# else :
# for i in range(r1,r2+1):
# for j in range(c1,c2+1):
# board[i][j] += degree
# if board[i][j] > 0 and chk[i][j] == 1:
# chk[i][j] = 0
# answer += 1
# return answer
#2중루프 안돌고 좌표들에 값을 바꾸는 방법 찾기
for type,r1,c1,r2,c2,degree in skill:
if type == 1:
for i in range(r1,r2+1):
for j in range(c1,c2+1):
board[i][j] -= degree
if board[i][j] <= 0 and chk[i][j] == 0:
chk[i][j] = 1
answer -= 1
else :
for i in range(r1,r2+1):
for j in range(c1,c2+1):
board[i][j] += degree
if board[i][j] > 0 and chk[i][j] == 1:
chk[i][j] = 0
answer += 1
return answer

def solution(board, skill):
visited = [[0] * (len(board[0]) + 1) for _ in range(len(board) + 1)]
Expand Down
34 changes: 34 additions & 0 deletions Programmers/hyeon-gyu/DynamicProgramming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## 레벨 1
### 푸드파이터 대회
- 좌우에 값을 대입해야해서 deque 자료구조를 활용하여 문제 해결

### 가장가까운 같은 글자
- 딕셔너리의 get함수의 기능를 활용하여 문제 해결


## 레벨 2
### 방문 길이
- 딕셔너리에 value로 튜플을 넣어 이동 방향 길이 설정
- (0,0)에서 (1,0)으로 아래로 한칸 내려가면 (0,0)->(1,0)과 (1,0)->(0,0) 양쪽에서 이동했을 때의 기록을 저장해야한다.

### 게임 맵 최단거리
- 전형적인 bfs + 최단경로 추적 문제
- 방문기록을 체크하는 리스트의 value 값을 이동비용을 저장하여 문제 해결

## 레벨 3
### 미로탈출명령어
문제의 핵심 : 도달할 수 있는지 여부부터 판단해야함
1. 남은 거리가 k보다 크면 impossible : 도달이 안됨.
2. k-직선거리 값이 홀수라면 impossible : 짝수인 경우엔 상하좌우 움직여서 목적지로 돌아올 수 있지만, 홀수는 목적지로 되돌아올 수 없음

### 표현가능한 이진트리
https://velog.io/@error_io/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%ED%91%9C%ED%98%84-%EA%B0%80%EB%8A%A5%ED%95%9C-%EC%9D%B4%EC%A7%84%ED%8A%B8%EB%A6%AC-Lv.-3-Python

1. numbers 값을 이진수로 변환
2. 이진수 길이 : 노드 개수, 포화이진트리 노드 개수 : 2**n -1

먼저 포화이진트리로 만들기전 이진트리가 유효한지부터 검증해야한다. 가운데 노드가 0인데 왼쪽이나 오른쪽 자식 노드에 1이 있으면 유효하지 않다.

63 -> 111111 자릿수가 (2**n)-1 이 아니라면 앞에 0을 추가하여 0111111로 만든다.
루트노드부터 시작해서 왼쪽 자식, 오른쪽 자식 검사
0111111의 루트는 len(arr)//2로 idx =3이 루트.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def solution(s):
answer = []
dic = {} # 단어 : 최근에 기록한 위치 인덱스
for i in range(len(s)):
res = dic.get(s[i],-1) # 두번째 인자에 -1 넣으면 에러 대신 -1을 리턴
if res != -1:
answer.append(i - dic[s[i]]) # dictionary에 존재한다면 현재 인덱스에서 최근 기록한 인덱스 차이값 기록
dic[s[i]] = i
else:
answer.append(-1)
dic[s[i]] = i
return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from collections import deque

def solution(maps):
n = len(maps) # 맵의 행 크기
m = len(maps[0]) # 맵의 열 크기

visited = [[-1 for _ in range(m)] for _ in range(n)] # value에 이동비용 저장
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
queue = deque()
queue.append((0, 0))
visited[0][0] = 1

while queue:
cx, cy = queue.popleft()
for i in range(4):
tx = cx + dx[i]
ty = cy + dy[i]
if 0 <= tx < n and 0 <= ty < m and maps[tx][ty] == 1:
if visited[tx][ty] == -1:
queue.append((tx, ty))
visited[tx][ty] = visited[cx][cy] + 1
if tx == n - 1 and ty == m - 1:
return visited[tx][ty]
return -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
sys.setrecursionlimit(10**5)
# bfs는 시간초과 -> 이동 경로를 전부 고려하는게 아니라 처음부터 가장 정확하고 사전순으로 빠른 경로 하나만을 탐색.
dirs = [["d", 1, 0], ["l", 0, -1], ["r", 0, 1], ["u", -1, 0]]
# dfs = 한방향으로 가능한 깊이 -> 가장 먼저 발견되는 유효한 경로가 사전순으로 빠른 경로
def solution(n, m, x, y, r, c, k):
dist = abs(x - r) + abs(y - c)
if dist > k:
return "impossible"
if dist % 2 != k % 2:
return "impossible"

def dfs(cy, cx, path, cnt):
if cnt == k:
if cy == r and cx == c:
return path
else:
for prc in dirs:
ny = cy + prc[1]
nx = cx + prc[2]
if 1 <= ny <= n and 1 <= nx <= m:
dist = abs(ny - r) + abs(nx - c)
# 다음 위치에서 목표 위치까지의 맨해튼 거리가 남은 이동 횟수보다 크면 그 방향으로 이동x
if dist > k - (cnt + 1):
continue
rtn = dfs(ny, nx, path + prc[0], cnt + 1)
if rtn is not None:
return rtn

answer = dfs(x, y, "", 0)
if answer is None:
return "impossible"
return answer
15 changes: 15 additions & 0 deletions Programmers/hyeon-gyu/DynamicProgramming/방문길이(49994).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def solution(dirs):
answer = 0
dic = {"U" : (-1,0), "L" : (0,-1), "R" : (0,1), "D" : (1,0)}
visited = []
cx, cy = 0, 0
for i in dirs:
# 방문 여부 확인법 : [기존x,기존y,도착x,도착y] 기록
nx, ny = cx + dic[i][0], cy + dic[i][1]
if -5 <= nx <= 5 and -5 <= ny <= 5:
if (cx, cy, nx, ny) not in visited and (nx, ny, cx, cy) not in visited:
visited.append((cx, cy, nx, ny))
visited.append((nx, ny, cx, cy)) # 왕복 둘다 적용해야함
answer += 1
cx, cy = nx, ny
return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 참고블로그 : https://velog.io/@ggb05224/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%ED%91%9C%ED%98%84-%EA%B0%80%EB%8A%A5%ED%95%9C-%EC%9D%B4%EC%A7%84%ED%8A%B8%EB%A6%AC-python
# 1. numbers 값을 이진수로 변환
# 2. 이진수 길이 : 노드 개수, 포화이진트리 노드 개수 : 2**n -1

# 먼저 포화이진트리로 만들기전 이진트리가 유효한지부터 검증해야한다. 가운데 노드가 0인데 왼쪽이나 오른쪽 자식 노드에 1이 있으면 유효하지 않다.

# 63 -> 111111 자릿수가 (2**n)-1 이 아니라면 앞에 0을 추가하여 0111111로 만든다.
# 루트노드부터 시작해서 왼쪽 자식, 오른쪽 자식 검사
# 0111111의 루트는 len(arr)//2로 idx =3이 루트.

def dfs(b, i, depth):
if depth == 0: # 리프 노드에 도달했다면
return True # 포화이진트리

# 부모노드가 '0' 일때
# 왼쪽 자식 노드가 '1' 이거나 오른쪽 자식 노드가 '1' 이라면 포화 이진트리가 될 수 없음
elif b[i] == '0':
if b[i - depth] == '1' or b[i + depth] == '1': return False

# 왼쪽 서브 트리 탐색
left = dfs(b, i - depth, depth // 2)
# 오른쪽 서브 트리 탐색
right = dfs(b, i + depth, depth // 2)
return left and right


def solution(numbers):
answer = []
for num in numbers: # num = 42
b = bin(num)[2:] # b = 101010 / len(b) = 6
nodes = bin(len(b) + 1)[2:] # nodes = 7 = 111

# 포화이진트리가 아닌 경우 더미노드(0추가)
if '1' in nodes[1:]:
dummies = (1 << len(nodes)) - int(nodes, 2)
b = '0' * dummies + b

# 이미 포화이진트리일 경우
result = dfs(b, len(b)//2, (len(b)+1)//4)
answer.append(1 if result else 0)

return answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from collections import deque

def solution(food):
answer = ""
queue = deque([0])
for i in range(len(food)-1, -1, -1):
k = food[i] // 2
for _ in range(k):
queue.appendleft(i)
queue.append(i)

for i in queue:
answer += str(i)
return answer