Skip to content

Commit 4e18114

Browse files
authored
Merge pull request #123 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 1월 3주차 / 5문제
2 parents c549cc7 + c50a45b commit 4e18114

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력
5+
S1 = input().strip()
6+
S2 = input().strip()
7+
8+
# DP 초기화
9+
dp = [[0] * (len(S1) + 1) for _ in range(len(S2) + 1)]
10+
11+
# DP 채우기
12+
for i in range(1, len(S2)+1):
13+
for j in range(1, len(S1)+1):
14+
if S2[i-1] == S1[j-1]:
15+
# 이전 값을 그대로 가져오거나 직전 문자열이 이어지고 있다면 +1 하거나
16+
dp[i][j] = dp[i-1][j-1] + 1
17+
18+
# 정답 출력
19+
print(max(max(row) for row in dp))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력
5+
n, m = map(int, input().split())
6+
write_str = " " + input().strip()
7+
answer_str = " " + input().strip()
8+
9+
# DP
10+
dp = [[0] * (n + 1) for _ in range(m + 1)]
11+
12+
# 공집합 채우기
13+
for i in range(1, m + 1):
14+
dp[i][0] = i
15+
for j in range(1, n + 1):
16+
dp[0][j] = j
17+
18+
# 최소 편집으로 DP 채우기
19+
for i in range(1, m + 1):
20+
for j in range(1, n + 1):
21+
if write_str[j] == answer_str[i] or \
22+
(write_str[j] == 'i' and answer_str[i] == 'j') or \
23+
(write_str[j] == 'i' and answer_str[i] == 'l') or \
24+
(write_str[j] == 'v' and answer_str[i] == 'w'):
25+
dp[i][j] = dp[i - 1][j - 1]
26+
else:
27+
dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1
28+
29+
# 정답 출력
30+
print(dp[m][n])
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력 및 초기화
5+
S1 = input().strip()
6+
S2 = input().strip()
7+
n, m = len(S1), len(S2)
8+
dp = [[""] * (m+1) for _ in range(n+1)]
9+
10+
# DP 채우기
11+
for i in range(1, n+1):
12+
for j in range(1, m+1):
13+
# 문자가 같으면 이전 값에 현재 문자 더하기
14+
if S1[i-1] == S2[j-1]:
15+
dp[i][j] = dp[i-1][j-1] + S1[i-1]
16+
# 문자가 다르면 더 긴 걸로 선택
17+
else:
18+
if len(dp[i-1][j]) > len(dp[i][j-1]):
19+
dp[i][j] = dp[i-1][j]
20+
else:
21+
dp[i][j] = dp[i][j-1]
22+
23+
# 정답 출력
24+
print(dp[n][m])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import heapq
2+
3+
4+
def solution(scoville, K):
5+
answer = 0
6+
heapq.heapify(scoville)
7+
8+
while len(scoville) > 1:
9+
10+
if scoville[0] >= K:
11+
return answer
12+
mix_scoville = heapq.heappop(scoville) + (heapq.heappop(scoville) * 2)
13+
answer += 1
14+
heapq.heappush(scoville, mix_scoville)
15+
16+
if scoville[0] >= K:
17+
return answer
18+
else:
19+
return -1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def solution(prices):
2+
answer = [0] * len(prices)
3+
stack = []
4+
5+
for idx, p in enumerate(prices):
6+
while stack and (p < prices[stack[-1]] or idx == len(prices) - 1):
7+
last = stack.pop()
8+
answer[last] = idx - last
9+
stack.append(idx)
10+
11+
return answer

0 commit comments

Comments
 (0)