Skip to content

Commit b7eb73f

Browse files
authored
Merge pull request #119 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 1์›” 2์ฃผ์ฐจ / 5๋ฌธ์ œ
2 parents e736e3a + 7ca4ff8 commit b7eb73f

5 files changed

+135
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def solution(strs, t):
2+
n = len(t)
3+
dp = [1e9] * (n + 1) # 1e9๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธ
4+
dp[0] = 0 # ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •: ์‹œ์ž‘์ ์€ 0
5+
6+
# strs๋ฅผ set์œผ๋กœ ๋ณ€ํ™˜ํ•ด ๋น ๋ฅด๊ฒŒ ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ
7+
str_set = set(strs)
8+
9+
# DP ์ง„ํ–‰
10+
for i in range(1, n + 1):
11+
# ์ตœ๋Œ€ 5๊ธ€์ž๊นŒ์ง€ ํ™•์ธ
12+
for j in range(1, 6):
13+
if i - j >= 0 and t[i - j:i] in str_set:
14+
# ํ˜„์žฌ ์œ„์น˜๋ฅผ j ๊ธธ์ด๋งŒํผ ๋’ค๋กœ ๊ฐ€์„œ ๋น„๊ต
15+
dp[i] = min(dp[i], dp[i - j] + 1)
16+
17+
# ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜: ์™„์„ฑ ๊ฐ€๋Šฅํ•˜๋ฉด dp[n], ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด -1
18+
return dp[n] if dp[n] != 1e9 else -1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def solution(n):
2+
# DP ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
3+
dp = [0] * (n + 1)
4+
dp[0] = 1 # ์ดˆ๊ธฐ๊ฐ’ ์„ค์ • (๊ด„ํ˜ธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ)
5+
6+
# ์นดํƒˆ๋ž€ ์ˆ˜ ์ ํ™”์‹ ์ ์šฉ
7+
for i in range(1, n + 1):
8+
for j in range(i):
9+
dp[i] += dp[j] * dp[i - j - 1]
10+
11+
return dp[n] # n๊ฐœ์˜ ๊ด„ํ˜ธ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋ฐ˜ํ™˜
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# ์ž…๋ ฅ ๋ฐ›๊ธฐ
6+
A, B = map(int, input().split())
7+
8+
# ํ ์ดˆ๊ธฐํ™”
9+
queue = deque([A])
10+
cnt = 1 # ์—ฐ์‚ฐ ํšŸ์ˆ˜
11+
12+
while queue:
13+
# ํ˜„์žฌ ํ์—์„œ ๋ชจ๋“  ๊ฐ’์„ ํƒ์ƒ‰
14+
for _ in range(len(queue)):
15+
current = queue.popleft()
16+
17+
# ํ˜„์žฌ ๊ฐ’์ด B์™€ ๊ฐ™์œผ๋ฉด ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ›„ ์ข…๋ฃŒ
18+
if current == B:
19+
print(cnt)
20+
exit()
21+
22+
# ๋‹ค์Œ ๊ฐ’์„ ํ์— ์ถ”๊ฐ€ (B๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋งŒ)
23+
if current * 2 <= B:
24+
queue.append(current * 2)
25+
if int(str(current)+"1") <= B:
26+
queue.append(int(str(current)+"1"))
27+
28+
# ์—ฐ์‚ฐ ํšŸ์ˆ˜ ์ฆ๊ฐ€
29+
cnt += 1
30+
31+
# ํ๊ฐ€ ๋น„์—ˆ๋‹ค๋ฉด -1 ์ถœ๋ ฅ
32+
print(-1)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
# ์ž…๋ ฅ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ
7+
S = deque(input().strip())
8+
temp = deque([]) # ๋‹จ์–ด๋ฅผ ์ €์žฅํ•˜๋Š” ์ž„์‹œ ๋ฆฌ์ŠคํŠธ
9+
answer = deque([]) # ๊ฒฐ๊ณผ ์ €์žฅ ๋ฆฌ์ŠคํŠธ
10+
11+
tag = False # ํƒœ๊ทธ ์ƒํƒœ ํ™•์ธ
12+
13+
while S:
14+
# ํƒœ๊ทธ ์‹œ์ž‘
15+
if S[0] == "<":
16+
tag = True
17+
18+
# ํƒœ๊ทธ ์‹œ์ž‘ ๋˜๋Š” ๊ณต๋ฐฑ ์ฒ˜๋ฆฌ
19+
if S[0] == " " or S[0] == "<":
20+
# temp์— ์ €์žฅ๋œ ๋‹จ์–ด ๋’ค์ง‘๊ธฐ
21+
while temp:
22+
answer.append(temp.pop())
23+
# ํƒœ๊ทธ ๋‚ด์šฉ ์ถ”๊ฐ€
24+
if tag:
25+
while S[0] != ">":
26+
answer.append(S.popleft())
27+
tag = False
28+
# ๊ณต๋ฐฑ ๋˜๋Š” ๋‹ซ๋Š” ํƒœ๊ทธ ์ถ”๊ฐ€
29+
answer.append(S.popleft())
30+
else:
31+
# ๋‹จ์–ด๋ฅผ temp์— ์ €์žฅ
32+
temp.append(S.popleft())
33+
34+
# ๋‚จ์•„ ์žˆ๋Š” ๋‹จ์–ด ์ฒ˜๋ฆฌ
35+
while temp:
36+
answer.append(temp.pop())
37+
38+
# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
39+
print(''.join(answer))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
n = int(input()) # ์ˆ˜์—ด์˜ ๊ธธ์ด
7+
stack = deque([]) # ์Šคํƒ ์ดˆ๊ธฐํ™”
8+
answer = deque([]) # ์—ฐ์‚ฐ ๊ธฐ๋ก
9+
flag = True # ์ˆ˜์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ”Œ๋ž˜๊ทธ
10+
current = 1 # ํ˜„์žฌ pushํ•  ์ˆซ์ž
11+
12+
# n๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ˆ˜์—ด ์ž…๋ ฅ
13+
for _ in range(n):
14+
num = int(input())
15+
16+
# ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ์ˆ˜์—ด์˜ ๊ฐ’์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ push
17+
while current <= num:
18+
stack.append(current)
19+
answer.append("+")
20+
current += 1
21+
22+
# ์Šคํƒ ์ตœ์ƒ๋‹จ์ด ์ˆ˜์—ด์˜ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋ฉด pop
23+
if stack[-1] == num:
24+
stack.pop()
25+
answer.append("-")
26+
else:
27+
# ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
28+
flag = False
29+
30+
# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
31+
if flag:
32+
for a in answer:
33+
print(a)
34+
else:
35+
print("NO")

0 commit comments

Comments
ย (0)