Skip to content

Commit 38cb2d1

Browse files
authored
Merge pull request #93 from learntosurf/main
Learntosurf / 11월 4주차 / 5문제
2 parents 692d663 + dbe40d5 commit 38cb2d1

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
str = input()
2+
3+
# 'a'의 개수
4+
a_count = str.count('a')
5+
6+
# 'b'의 개수
7+
current_b = 0
8+
for i in range(a_count):
9+
if str[i] == 'b':
10+
current_b += 1
11+
12+
min_b = current_b
13+
14+
# 고정된 길이의 구간별 탐색
15+
for i in range(1, len(str)):
16+
# 이전 문자를 제거
17+
if str[i-1] == 'b':
18+
current_b -= 1
19+
# 새 문자를 추가
20+
if str[(i + a_count - 1) % len(str)] == 'b':
21+
current_b +=1
22+
min_b = min(min_b, current_b)
23+
24+
print(min_b)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import defaultdict, deque
2+
3+
N, M, V = map(int, input().split())
4+
5+
# 인접리스트로 그래프 표현
6+
graph = defaultdict(list)
7+
for _ in range(M):
8+
a, b = map(int, input().split())
9+
graph[a].append(b)
10+
graph[b].append(a)
11+
12+
for node in graph:
13+
graph[node].sort() # 정점 번호가 작은 순서대로 탐색하기 위해 정렬
14+
15+
def dfs(graph, start, visited): # visited 리스트를 재귀 호출 간에 공유해야 하므로, 함수 인자로 전달
16+
visited.append(start) # 현재 정점을 방문
17+
for neighbor in graph[start]:
18+
if neighbor not in visited: # 방문하지 않은 정점만 탐색
19+
dfs(graph, neighbor, visited)
20+
return visited
21+
22+
def bfs(graph, start):
23+
visited = [] # 방문 순서
24+
queue = deque([start])
25+
while queue:
26+
node = queue.popleft()
27+
if node not in visited: # 방문하지 않은 경우
28+
visited.append(node)
29+
queue.extend(graph[node]) # 이웃 정점 추가
30+
return visited
31+
32+
dfs_result = dfs(graph, V, [])
33+
bfs_result = bfs(graph, V)
34+
35+
print(*dfs_result)
36+
print(*bfs_result)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def dfs(x, y):
2+
# 배추밭의 범위를 벗어나면 종료
3+
if x < 0 or x >= M or y < 0 or y >= N:
4+
return
5+
# 배추가 없거나 이미 방문한 경우 종료
6+
if graph[y][x] == 0:
7+
return
8+
9+
# 현재 배추 방문 처리
10+
graph[y][x] = 0
11+
12+
# 상, 하, 좌, 우로 재귀 호출
13+
dfs(x + 1, y)
14+
dfs(x - 1, y)
15+
dfs(x, y + 1)
16+
dfs(x, y - 1)
17+
18+
T = int(input())
19+
20+
for _ in range(T):
21+
M, N, K = map(int, input().split())
22+
# NxM 행렬
23+
graph = [[0] * M for _ in range(N)]
24+
25+
for _ in range(K):
26+
x, y = map(int, input().split()) # 배추 위치 (x: 가로, y: 세로)
27+
graph[y][x] = 1 # (y,x) 위치에 배추 표시
28+
29+
count = 0 # 배추흰지렁이 수 (군집 개수)
30+
31+
for y in range(N):
32+
for x in range(M):
33+
if graph[y][x] == 1: # 배추가 있고 방문하지 않았다면
34+
dfs(x, y) # DFS 호출
35+
count += 1 # 군집 개수 증가
36+
37+
print(count)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import deque
2+
import sys
3+
input = sys.stdin.readline
4+
5+
def bfs(start, K):
6+
queue = deque()
7+
queue.append(start)
8+
visited[start] = 0 # 시작 위치까지의 시간은 0으로 초기화
9+
10+
while queue:
11+
node = queue.popleft()
12+
if node == K: # 동생의 위치에 도달했을 때
13+
print(visited[node])
14+
return
15+
16+
# 동생의 위치에 도달할 때까지, 3가지 이동 경우를 탐색
17+
for next in (node-1, node+1, node*2):
18+
if (0 <= next < 100001) and not visited[next]:
19+
visited[next] = visited[node] + 1 # 이동시간을 1초 증가시킨다.
20+
queue.append(next)
21+
22+
N, K = map(int, input().split())
23+
visited = [-1] * 100001
24+
bfs(N, K)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, S = map(int, input().split())
5+
arr = list(map(int, input().split()))
6+
7+
def min_subarray_length(N, S, arr):
8+
# 초기화
9+
start, end = 0, 0
10+
current_sum = 0
11+
min_length = float('inf') # 최소 길이를 아주 큰 값으로 초기화
12+
13+
# 슬라이딩 윈도우 실행
14+
while end < N:
15+
# 현재 값을 current_sum에 추가
16+
current_sum += arr[end]
17+
18+
# current_sum이 S 이상이면 조건 만족
19+
while current_sum >= S:
20+
# 구간 길이 계산 후 최소 길이 갱신
21+
min_length = min(min_length, end - start + 1)
22+
# start를 오른쪽으로 이동하며 구간을 줄임
23+
current_sum -= arr[start]
24+
start += 1
25+
26+
# end를 오른쪽으로 이동
27+
end += 1
28+
29+
# 조건을 만족하는 구간이 없으면 0 반환, 아니면 최소 길이 반환
30+
return 0 if min_length == float('inf') else min_length
31+
32+
result = min_subarray_length(N, S, arr)
33+
print(result)

0 commit comments

Comments
 (0)