Skip to content

Commit e2af8df

Browse files
committed
코딩테스트 스터디 풀이 업로드
1 parent 3fda2d7 commit e2af8df

10 files changed

+248
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import deque
2+
3+
def solution(arr):
4+
q = deque(arr)
5+
p1 = q.popleft()
6+
ans = deque([])
7+
if not q:
8+
return arr
9+
while q:
10+
11+
p2 = q.popleft() # 하나 더뽑음
12+
13+
if p1 == p2:
14+
if not q:
15+
ans.append(p1)
16+
continue
17+
else:
18+
ans.append(p1)
19+
p1 = p2
20+
if not q:
21+
ans.append(p2)
22+
23+
return list(ans)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import deque
2+
3+
def solution(pro, spd):
4+
answer = []
5+
comp = deque([])
6+
qpro = deque(pro)
7+
qspd = deque(spd)
8+
p = qpro.popleft()
9+
s = qspd.popleft()
10+
total_date = (100 - p) // s # 며칠후에 배포가능
11+
if (100 - p) % s > 0:
12+
total_date += 1 # 안맞아떨어지면 하루 추가
13+
14+
comp.append(p)
15+
16+
while qpro:
17+
qp = qpro.popleft()
18+
qs = qspd.popleft()
19+
20+
due_date = (100 - qp) // qs
21+
if (100 - qp) % qs > 0:
22+
due_date += 1
23+
24+
if total_date >= due_date:
25+
# 다음 작업 기간이 총진행일보다 같거나 일찍끝남
26+
comp.append(qp)
27+
if not qpro:
28+
answer.append(len(comp))
29+
break
30+
else: # 다음작업이 더걸림 - 앞선것을 배포
31+
answer.append(len(comp)) # 배포
32+
comp = deque([])
33+
total_date = due_date
34+
if not qpro:
35+
answer.append(1)
36+
break
37+
else:
38+
qpro.appendleft(qp)
39+
qspd.appendleft(qs)
40+
41+
return answer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
3+
def solution(s):
4+
answer = True
5+
oc = 0
6+
qs = deque(s)
7+
while qs:
8+
q = qs.popleft()
9+
if q == '(':
10+
oc += 1
11+
else:
12+
oc -= 1
13+
if oc < 0:
14+
return False
15+
16+
17+
if oc != 0:
18+
return False
19+
20+
return answer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ~ 10:30
2+
from collections import deque
3+
4+
def solution(pri, loc):
5+
answer = 0
6+
q_pri = deque(tuple(zip(range(len(pri)), pri)))
7+
# (위치정보 0~n-1, 중요도)
8+
9+
count = 0 # 인쇄된 문서 카운트
10+
11+
while q_pri:
12+
# pri에서 하나빼고, 이게 뺀 큐의 max보다 큰지 판단
13+
position, priority = q_pri.popleft() # 위치정보, 중요도
14+
if not q_pri: # 마지막까지 다뺸경우
15+
return count+1
16+
if priority >= max(q_pri, key = lambda x: x[1])[1]:
17+
# 뺀다
18+
count += 1
19+
if position == loc:
20+
return count
21+
else:
22+
q_pri.append((position, priority))
23+
24+
return count
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ~ 11:30
2+
def solution(answers):
3+
p1 = [1, 2, 3, 4, 5]
4+
p2 = [2, 1, 2, 3, 2, 4, 2, 5]
5+
p3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
6+
7+
pe1 = p1*(len(answers)//len(p1) + 1)
8+
pe2 = p2*(len(answers)//len(p2) + 1)
9+
pe3 = p3*(len(answers)//len(p3) + 1)
10+
11+
pe1 = pe1[:len(answers)]
12+
pe2 = pe2[:len(answers)]
13+
pe3 = pe3[:len(answers)]
14+
15+
cor = [0, 0, 0, 0]
16+
for i in range(len(answers)):
17+
if pe1[i] == answers[i]:
18+
cor[1] += 1
19+
if pe2[i] == answers[i]:
20+
cor[2] += 1
21+
if pe3[i] == answers[i]:
22+
cor[3] += 1
23+
result = []
24+
max_c = max(cor)
25+
for i in range(1, 4):
26+
if cor[i] == max_c:
27+
result.append(i)
28+
29+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ~ 11:00
2+
from itertools import permutations
3+
import numpy as np
4+
5+
def solution(numbers):
6+
# 소수 판별? 2~sqrt(자기자신)까지 해서 나머지 계속 생기면 됨
7+
def is_sosu(number):
8+
for i in range(2, int(np.sqrt(number))+1):
9+
if number%i == 0:
10+
return False
11+
return True
12+
13+
answer = []
14+
15+
perm = []
16+
for l in range(1, len(numbers)+1):
17+
perm += list(set(permutations(numbers, l)))
18+
19+
20+
# print(perm)
21+
for i in perm:
22+
num = int(''.join(i))
23+
if num in [0, 1]: continue
24+
# print(is_sosu(num))
25+
if is_sosu(num):
26+
answer.append(num)
27+
28+
return len(set(answer))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def solution(sizes):
2+
max_h, max_v = 0, 0
3+
# 가로 < 세로로 맞춤
4+
for h, v in sizes:
5+
if h < v:
6+
h, v = v, h
7+
if max_h < h:
8+
max_h = h
9+
if max_v < v:
10+
max_v = v
11+
12+
return max_h*max_v
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from heapq import heapify, heappop, heappush
2+
3+
def solution(scoville, K):
4+
5+
count = 0
6+
# 불가능 경우 처리하기
7+
heapify(scoville) # 최소 - 최대
8+
# for _ in range(int(1e6)):
9+
while True:
10+
dish1 = heappop(scoville)
11+
# K 체크
12+
if dish1 >= K:
13+
return count
14+
# 큐 비어있으면 -1 리턴
15+
if not scoville: return -1
16+
dish2 = heappop(scoville)
17+
mix = dish1 + (dish2*2)
18+
heappush(scoville, mix)
19+
count += 1
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 최댓값, 최솟값을 한번에 O(logn)으로 빼는 방법?
2+
# 1. 그때그때 정렬
3+
def solution(operations):
4+
answer = []
5+
queue = []
6+
for op in operations:
7+
com, num = op.split()
8+
num = int(num)
9+
if com == 'I':
10+
queue.append(num)
11+
elif com == 'D' and len(queue) > 0:
12+
if num == 1:
13+
queue.sort()
14+
queue.pop() # 최댓값 삭제
15+
elif num == -1:
16+
queue.sort()
17+
queue.pop(0) # 최솟값 삭제
18+
19+
return [max(queue), min(queue)] if queue else [0, 0]

temp/baekjoon_숨바꼭질 3.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
sys.setrecursionlimit = 5000
3+
n, k = map(int, input().split())
4+
5+
# top-down
6+
def find(x, t, memo: dict):
7+
# memoization - 확인해서 값 있으면 갱신하고 리턴
8+
if x == k:
9+
return t
10+
# k가 아닐 경우 메모하고 나옴
11+
if x-1 >= 0:
12+
if x-1 in memo:
13+
memo[x] = min(memo[x], memo[x-1]+1)
14+
return
15+
else:
16+
find(x-1, t+1, memo)
17+
if x+1 <= 100000:
18+
if x+1 in memo:
19+
memo[x] = min(memo[x], memo[x+1]+1)
20+
return
21+
else:
22+
find(x+1, t+1, memo)
23+
if 2*x <= 100000:
24+
if 2*x in memo:
25+
memo[x] = min(memo[x], memo[2*x])
26+
else:
27+
find(2*x, t, memo)
28+
29+
print(find(n, 0, dict()))
30+
31+
32+

0 commit comments

Comments
 (0)