Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypeho committed Aug 1, 2024
2 parents 97f682d + 4cd7688 commit e0ac000
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 30 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
<td> Python : 27&nbsp&nbsp&nbsp&nbspJava : 0&nbsp&nbsp&nbsp&nbspC : 0&nbsp&nbsp&nbsp&nbsp&nbspC++ : 0&nbsp&nbsp&nbsp&nbsp&nbspC# : 0</td>
</tr> <tr>
<td> 남현호 </td>
<td> 7 </td>
<td> 7 </td>
<td> 93 </td>
<td> -93000 </td>
<td> Python : 7&nbsp&nbsp&nbsp&nbspJava : 0&nbsp&nbsp&nbsp&nbspC : 0&nbsp&nbsp&nbsp&nbsp&nbspC++ : 0&nbsp&nbsp&nbsp&nbsp&nbspC# : 0</td>
<td> 10 </td>
<td> 10 </td>
<td> 90 </td>
<td> -90000 </td>
<td> Python : 10&nbsp&nbsp&nbsp&nbspJava : 0&nbsp&nbsp&nbsp&nbspC : 0&nbsp&nbsp&nbsp&nbsp&nbspC++ : 0&nbsp&nbsp&nbsp&nbsp&nbspC# : 0</td>
</tr> <tr>
<td> 류정민 </td>
<td> 12 </td>
Expand All @@ -51,14 +51,14 @@
<td> Python : 5&nbsp&nbsp&nbsp&nbspJava : 0&nbsp&nbsp&nbsp&nbspC : 0&nbsp&nbsp&nbsp&nbsp&nbspC++ : 0&nbsp&nbsp&nbsp&nbsp&nbspC# : 0</td>
</tr> <tr>
<td> 최수연 </td>
<td> 26 </td>
<td> 28 </td>
<td> 74 </td>
<td> -74000 </td>
<td> Python : 28&nbsp&nbsp&nbsp&nbspJava : 0&nbsp&nbsp&nbsp&nbspC : 0&nbsp&nbsp&nbsp&nbsp&nbspC++ : 0&nbsp&nbsp&nbsp&nbsp&nbspC# : 0</td>
<td> 30 </td>
<td> 72 </td>
<td> -72000 </td>
<td> Python : 30&nbsp&nbsp&nbsp&nbspJava : 0&nbsp&nbsp&nbsp&nbspC : 0&nbsp&nbsp&nbsp&nbsp&nbspC++ : 0&nbsp&nbsp&nbsp&nbsp&nbspC# : 0</td>
</tr></table>
<br>
총 Push 횟수 : 126회
총 Push 횟수 : 130회

# 업로드 방법
### 1. 파일명
Expand Down
25 changes: 25 additions & 0 deletions SYCHOI/11to20/19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def polynomial_hash(str) :
p = 31
m = 1000000007
hash_value = 0

for char in str :
hash_value = (hash_value * p + ord(char)) % m # why???
return hash_value

def solution(string_list, query_list) :
# string_list에 있는 문자열들을 해싱해서 hash_list에 저장
hash_list = [polynomial_hash(str) for str in string_list]

result = []
for query in query_list :
query_hash = polynomial_hash(query) # query_list에 있는 문자열(query)들을 해싱해서 query_hash에 저장
if query_hash in hash_list :
result.append(True) # 해당 해싱값이 hash_list에 있으면 True 저장
else :
result.append(False) # 해당 해싱값이 hash_list에 없으면 False 저장
return result


#TEST 코드입니다. 주석을 풀어서 확인해보세요
print(solution(["apple", "banana", "cherry"], ["banana", "kiwi", "melon", "apple"]))
39 changes: 20 additions & 19 deletions SYCHOI/21to30/30.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,45 @@ def isValid(ny, nx, n, m, maps) :
# 방문할 좌표가 벽이 아니면 return

def append_to_Queue(ny, nx, k, time, visited, q) :
if not visited[ny][nx][k] :
visited[ny][nx][k] = True
q.append((ny, nx, k, time + 1))
if not visited[ny][nx][k] : # 방문하지 않았다면
visited[ny][nx][k] = True # 방문 표시
q.append((ny, nx, k, time + 1)) # time + 1 상태 q에 append

def solution(maps) :
n, m = len(maps), len(maps[0]) # n : maps의 행 개수, m : maps의 열 개수
visited = [[[False for _ in range(2)] for _ in range(m)] for _ in range(n)] # [False, False]를 m개의 열 개수만큼, n개의 행 개수만큼 생성
# Flase for _ in range(2) : [False, False] 생성

# dy, dx의 인덱스에 따라 상/하/좌/우 구별 가능
# False for _ in range(2) : [False, False] 생성
# dy, dx의 인덱스에 따라 상/하/좌/우 구별 가능 : dx[0] & dy[0] = 위로 이동
dy = [-1, 1, 0, 0]; dx = [0, 0, -1, 1]
q = deque() # q라는 변수에 덱 기능 선언
endY, endX = -1, -1 # 도착점의 x, y 좌표 넣는 변수. -1로 초기화한다
q = deque() # q라는 변수에 덱 기능 선언

for i in range(n) :
for j in range(m) :
if maps[i][j] == "S" :
q.append((i, j, 0, 0)) # 덱 q에 시작점 좌표 입력
visited[i][j][0] = True
q.append((i, j, 0, 0)) # 덱 q에 시작점 좌표 입력 (q의 오른쪽 끝에 삽입)
visited[i][j][0] = True # (i, j) 좌표 방문 표시
if maps[i][j] == "E" :
endY, endX = i, j
endY, endX = i, j # (i, j) 출구 좌표 입력

while q :
y, x, k, time = q.popleft()
y, x, k, time = q.popleft() # (q의 왼쪽 끝 원소 pop)
# y좌표 , x좌표, 레버 동작 여부, 시작 지점부터 해당 좌표까지 가는 데 걸린 시간

if y== endY and x == endX and k == 1 :
if y== endY and x == endX and k == 1 : # 왜 k == 1이어야 할까..?
return time

for i in range(4) :
ny, nx = y + dy[i], x + dx[i]
for i in range(4) : # 현재 좌표(y, x)에서 상하좌우 1칸씩 이동
ny, nx = y + dy[i], x + dx[i] # 이동한 좌표 ny, nx에 저장

if not isValid(ny, nx, n, m, maps) :
continue
if not isValid(ny, nx, n, m, maps) :
continue # 이동 불가한 위치라면 continue

if maps[ny][nx] == "L" :
append_to_Queue(ny, nx, 1, time, visited, q)
if maps[ny][nx] == "L" :
append_to_Queue(ny, nx, 1, time, visited, q) # 이동한 곳이 레버라면 k = 1 적용
else :
append_to_Queue(ny, nx, k, time, visited, q)
append_to_Queue(ny, nx, k, time, visited, q) # 이동한 곳이 레버가 아니라면 k = 0(그대로)

return -1

Expand Down
88 changes: 88 additions & 0 deletions SYCHOI/31to40/32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from collections import deque

class Node :
def __init__(self, info, num, left=None, right=None) :
self.info = info # 노드의 좌표 정보
self.left = left # 노드의 왼쪽 자식 노드
self.right = right # 노드의 오른쪽 자식 노드
self.num = num # 노드의 번호

def has_left(self) :
return self.left is not None # 왼쪽 노드에 값이 있으면 리턴

def has_right(self) :
return self.right is not None # 오른쪽 노드에 값이 없으면 리턴


def make_BT(nodeinfo) :
# 노드의 개수만큼 1부터 노드길이 + 1까지의 리스트 생성
nodes = [i for i in range(1, len(nodeinfo) + 1)]

# y좌표 내림차순 정렬, y좌표가 같을 때 x좌표 오름차순 정렬 (y축은 크고 x축은 작은 노드의 인덱스값 정렬)
# nodes의 원소는 1부터, nodesinfo의 원소는 0부터 시작하므로 nodeinfo의 1차원 리스트에 대해서 x - 1 적용
nodes.sort(key = lambda x : (nodeinfo[x - 1][1], -nodeinfo[x - 1][0]), reverse=True)
root = None # 루트 노드 초기화(None으로)

# root를 기준으로 순회해서 트리에 노드 추가
for i in range(len(nodes)) :
if root is None :
root = Node(nodeinfo[nodes[0] - 1], nodes[0]) # 'root' 노드의 info = nodeinfo[nodes[0] - 1], num = nodes[0]
else :
parent = root
node = Node(nodeinfo[nodes[i] - 1], nodes[i]) # 자식 노드들의 info = nodeinfo[nodes[i] - 1], num = nodes[i]

while True :
# info[0] : 해당 노드(node, parent)의 x좌표
if node.info[0] < parent.info[0] : # parent의 x좌표가 node보다 크면
if parent.has_left() :
parent = parent.left # parent의 왼쪽 노드에 값이 있으면 현재 parent를 parent의 왼쪽 노드로
continue
parent.left = node # parent의 왼쪽 노드에 값이 없으면 node 삽입
break
else : # node의 x좌표가 parent보다 크면
if parent.has_right() :
parent = parent.right # parent의 오른쪽 노드에 값이 있으면 현재 parent를 parent의 오른쪽 노드로
continue
parent.right = node # parent의 오른쪽 노드에 값이 없으면 node 삽입
break
return root


def pre_order(root, answer) : # 생성한 이진트리의 root, 전위순회를 담을 answer 리스트(answer[0])
stack = [root] # stack에 root 노드 삽입
while stack :
node = stack.pop() # stack의 최상단 노드 방문
if node is None :
continue # 부모 노드의 왼쪽or오른쪽 노드가 없을 경우
answer[0].append(node.num) # 방문한 노드의 값 answer[0]에 append
stack.append(node.right)
stack.append(node.left) # stack은 LIFO이기 때문에 먼저 방문할 왼쪽 노드를 나중에 삽입


def post_order(root, answer) : # 생성한 이진트리의 root, 후위순회를 담을 answer 리스트(answer[1])
stack = [(root, False)] # stack에 root 노드 & 방문 여부 삽입
while stack :
node, visited = stack.pop() # stack의 최상단 노드 방문
if node is None :
continue # 부모 노드의 왼쪽or오른쪽 노드가 없을 경우
if visited :
answer[1].append(node.num) # 방문
else :
stack.append((node, True))
stack.append((node.right, False))
stack.append((node.left, False))




def solution(nodeinfo) :
answer = [[], []] # 이진 트리의 각각 [전위 순회], [후위 순회] 결과를 담을 변수
root = make_BT(nodeinfo) # 입력받은 노드의 좌표(nodeinfo)를 통해 이진 트리 생성
pre_order(root, answer)
post_order(root, answer)

return answer


#TEST 코드입니다. 주석을 풀어서 확인해보세요
print(solution([[5, 3], [11, 5], [13, 3], [3, 5], [6, 1], [1, 3], [8, 6], [7, 2], [2, 2]]))
2 changes: 1 addition & 1 deletion total_push_cnt.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126
130

0 comments on commit e0ac000

Please sign in to comment.