Skip to content
31 changes: 31 additions & 0 deletions ggangchongs/Angora/7days/BFS&DFS/Feb_1st_shortestPath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from collections import deque

def solution(maps):
queue = deque()
step = [(-1,0), (1,0), (0,-1), (0,1)] # 상 하 좌 우
answer = bfs_maze(0,0, queue, step, maps)
if answer == 1: return -1
else: return answer

def bfs_maze(x,y, queue, step, maps):
n,m = len(maps), len(maps[0])
queue.append((x,y))

while queue:
x, y = queue.popleft()
for act in step:
nx, ny = x + act[0], y + act[1]

# 벗어나면 패스
if nx < 0 or nx >= n or ny < 0 or ny >= m: continue
# 벽이면 패스
if maps[nx][ny] == 0: continue
# 이동 가능할 경우
if maps[nx][ny] == 1:
maps[nx][ny] = maps[x][y] + 1
queue.append((nx,ny))

return maps[n-1][m-1]

maps = [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]]
print(solution(maps))
6 changes: 6 additions & 0 deletions ggangchongs/Angora/7days/Brute-Force/Feb_1st_Carpet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def solution(brown, yellow):

brown = (brown-4)/2
for i in range(1, int(brown**1/2)+1):
if i*(brown-i) == yellow:
return [brown-i+2, i+2]
12 changes: 12 additions & 0 deletions ggangchongs/Angora/7days/DP/Feb_1st_intTriangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def solution(triangle):
higher = triangle[0]

for i in range(1, len(triangle)):
now = triangle[i]
for j in range(len(now)):
if j == 0: now[j] += higher[0]
elif j == len(now)-1: now[j] += higher[j-1]
else: now[j] += max(higher[j-1], higher[j])
higher = now

return max(higher)
23 changes: 23 additions & 0 deletions ggangchongs/Angora/7days/Graph/Feb_1st_furthestNode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from collections import deque

def solution(n, edge):
connect = [[] for _ in range(n+1)]
for a,b in edge:
connect[a].append(b)
connect[b].append(a)

visited = [0]*(n+1)
visited[1] = 1
queue = deque([1])
while queue:
now = queue.popleft()
for next in connect[now]:
if not visited[next]:
visited[next] = visited[now] + 1
queue.append(next)

return visited.count(max(visited))

n = 6
vertex = [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]]
print(solution(n, vertex))
24 changes: 24 additions & 0 deletions ggangchongs/Angora/7days/Greedy/Feb_1st_lifeBoat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def solution(people, limit):
people = sorted(people, reverse=True)
i,j = 0, len(people)-1
answer = 0

while i < j:
tmp = people[i] + people[j]
if tmp <= limit:
while tmp + people[j-1] <= limit:
tmp += people[j-1]
j -= 1
i += 1
j -= 1
answer += 1
else:
i += 1
answer += 1
if i == j: answer += 1

return answer

people = [70, 50, 50, 10, 20]
limit = 100
print(solution(people, limit))
17 changes: 17 additions & 0 deletions ggangchongs/Angora/7days/Hash/Feb_1st_Camouflage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def solution(clothes):

type = {}
for cloth in clothes:
answer = 1
if cloth[1] not in type:
type[cloth[1]] = 1
else:
type[cloth[1]] += 1

for c in type.values():
answer *= c+1
return answer-1


clothes = [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]
print(solution(clothes))
24 changes: 24 additions & 0 deletions ggangchongs/Angora/7days/Stack&Queue/Feb_1st_Printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def solution(priorities, location):
answer = 0

while len(priorities) != 0:
tmp = priorities.pop(0)

if len(priorities) == 0:
return answer + 1

if tmp < max(priorities): # 출력 불가
priorities.append(tmp)
if location == 0: location = len(priorities)-1
else: location -= 1
else: # 출력 가능
answer += 1
if location == 0: return answer
else: location -= 1

priorities = [1, 1, 2, 3, 2, 1]
location = 0
print(solution(priorities, location))


tmp = []
5 changes: 2 additions & 3 deletions ggangchongs/Angora/FromRestaurant/DP/11055.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
sequence = list(map(int, sys.stdin.readline().split()))

dp = [0]*n
dp[0], maxSum = sequence[0], sequence[0]
dp[0] = sequence[0]

for i in range(1,n):
for j in range(i):
if sequence[j] < sequence[i]:
dp[i] = max(dp[i], dp[j] + sequence[i])
else:
dp[i] = max(dp[i], sequence[i])
maxSum = max(maxSum, dp[i])

print(maxSum)
print(max(dp))
16 changes: 6 additions & 10 deletions ggangchongs/Angora/FromRestaurant/Greedy/1339.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
n = int(input())
voca = []
alpha = {}
for i in range(n):
tmp = list(input())
for t in tmp:
alpha[t] = 0
voca.append(tmp)

for v in voca:
length = len(v)
voca = list(input())
length = len(voca)
for i in range(length):
oldValue = alpha[v[i]]
alpha[v[i]] += 10**(length-(i+1))
if voca[i] not in alpha:
alpha[voca[i]] = 10**(length-(i+1))
else:
alpha[voca[i]] += 10**(length-(i+1))

sorted_alpha = sorted(alpha.items(), key = lambda item: -item[1])
start = 9
Expand Down
24 changes: 24 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/11866.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys

n, k = map(int, sys.stdin.readline().split())

queue = []
for i in range(n):
queue.append(i+1)

count = 1
answer = []
while len(queue) != 0:
if count != k:
tmp = queue.pop(0)
queue.append(tmp)
count += 1
else:
answer.append(queue.pop(0))
count = 1

print("<", end='')
for i,a in enumerate(answer):
if i == len(answer)-1: print(a, end='')
else: print(a, end=', ')
print(">", end='')
16 changes: 16 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/1715.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys
import heapq

n = int(sys.stdin.readline())
heap = []
for i in range(n): heapq.heappush(heap, int(sys.stdin.readline()))

answer = 0
while len(heap)>=2:
first = heapq.heappop(heap)
second = heapq.heappop(heap)
answer += first + second
if len(heap) == 0: break
else: heapq.heappush(heap, first + second)

print(answer)
13 changes: 13 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/1764.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys

n, m = map(int, sys.stdin.readline().split())

d = []
for i in range(n): d.append(sys.stdin.readline())
b = []
for i in range(m): b.append(sys.stdin.readline())

db = sorted(list(set(d)&set(b)))
print(len(db))
for name in db:
print(name, end='')
31 changes: 31 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/1874.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
1 2 3 4 -> 4
1 2 3 -> 4
1 2 -> 4 3
1 2 5 6 -> 4 3
1 2 5 -> 4 3 6
1 2 5 7 8 -> 4 3 6
-> 4 3 6 8 7 5 2 1
'''

import sys

n = int(sys.stdin.readline())

candi = 1
stack, answer = [], []
correct = True
for i in range(n):
target = int(sys.stdin.readline())
while candi <= target:
stack.append(candi)
answer.append("+")
candi += 1
if stack[-1] == target:
stack.pop()
answer.append("-")
else: correct = False; break

if correct == False: print("NO")
else:
for a in answer: print(a)
18 changes: 18 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/2493.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys

n = int(sys.stdin.readline())
topList = list(map(int, sys.stdin.readline().split()))

indexStack, answer = [1],[0]
for i, top in enumerate(topList):
if i == 0: continue
else:

while topList[indexStack[-1]-1] < top:
indexStack.pop()
if len(indexStack) == 0: break
if len(indexStack) == 0: answer.append(0)
else: answer.append(indexStack[-1])
indexStack.append(i+1)

for i in answer: print(i, end=' ')
15 changes: 15 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/27160.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys

n = int(sys.stdin.readline())

fruits = {}
for i in range(n):
tmp, num = sys.stdin.readline().split()
if tmp in fruits:
fruits[tmp] += int(num)
else:
fruits[tmp] = int(num)

if 5 in fruits.values(): print("YES")
else: print("NO")

30 changes: 30 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/2841.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys

n, p = map(int, sys.stdin.readline().split())
answer = 0

guitar = {}
line = 0
for i in range(n):
newLine, newP = map(int, sys.stdin.readline().split())


if line != newLine and newLine not in guitar:
tmp = [newP]
guitar[newLine] = tmp
answer += 1
elif (line != newLine and newLine in guitar) or line == newLine:
tmp = guitar[newLine]
while tmp[-1] > newP:
tmp.pop(); answer += 1
if len(tmp) == 0: break
if len(tmp) == 0:
tmp.append(newP)
answer += 1
elif tmp[-1] < newP:
tmp.append(newP)
answer += 1

line = newLine

print(answer)
17 changes: 17 additions & 0 deletions ggangchongs/Angora/FromRestaurant/dataStructure_tmp/9012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys

t = int(sys.stdin.readline())

testStack = []
answer = "YES"
for i in range(t):
tmpPS = list(sys.stdin.readline())

for p in tmpPS:
if p == '(': testStack.append(p)
if p == ')':
if testStack[-1:] == ['(']: testStack.pop()
else: answer = "NO"; break
if len(testStack) != 0: answer = "NO"
print(answer)
testStack.clear(); answer ="YES"