Skip to content

Commit a624795

Browse files
committed
2 parents de89c53 + e29faa4 commit a624795

21 files changed

+467
-4
lines changed

Hongjoo/lv2/숫자의표현.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
[문제] : https://school.programmers.co.kr/learn/courses/30/lessons/12924#
3+
[Flow]
4+
연속된 자연수의 조합-> two point
5+
0. 초기화
6+
start == end-1 = 0
7+
while end <= len(words) and start < end
8+
1. sum(words[start,end]) < target
9+
-> end += 1
10+
2. sum(words[start,end]) == target
11+
-> count += 1
12+
-> start += 1
13+
-> end = start +1
14+
15+
3. sum(words[start,end]) > 0
16+
-> 2와 동일
17+
18+
'''
19+
def solution(n):
20+
answer = 1 # 본인= target
21+
start = 0 ; end = start +1 ;
22+
answer_li = []
23+
half= round(n//2) +2
24+
field = range(1,half) # 반올림
25+
field = list(field)
26+
27+
while n > 2 and end <= len(field) and start < end and start < len(field): #
28+
current =sum(field[start : end])
29+
if current < n :
30+
end += 1
31+
else : # currnet >= n
32+
if current == n :
33+
answer += 1
34+
answer_li.append([start, end])
35+
start += 1
36+
end = start +1
37+
return answer

Hongjoo/백준/문자열교환.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
words= input()
66
window_size = words.count("a") # 1. a의 개수 = sliding window 크기
7-
result = 999999999
7+
result = 99999999
88

99
# 원형 문자열
1010
words += words[0:window_size-1]
1111
# 2. 최대한 a 가 연속해 있는 idx 범위 찾기
1212
for start in range(len(words) - (window_size-1)):
1313
result = min(result, words[start: start+ window_size].count("b"))
1414

15-
print(result)
15+
print(result)
16+

Hongjoo/백준/부분합.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"""
2+
문제 : # 1806부분합
23
https://www.acmicpc.net/problem/1806
4+
5+
해설 :
6+
https://aia1235.tistory.com/46
7+
38
"""
49
import sys
510
N ,S = map(int, sys.stdin.readline().split())
@@ -17,11 +22,11 @@
1722
end += 1
1823
if end < N :
1924
partial_sum += arr[end]
20-
else : # 수열 끝 -> 조건 충족 x
25+
else : #반복문 끝
2126
break
2227

2328

24-
if min_length == 100000 :
29+
if min_length == 100000 : # 수열 끝 -> 조건 충족 x
2530
print(0)
2631
else :
2732
print(min_length)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
BOJ #17845. 수강과목 (골드5)
3+
https://www.acmicpc.net/problem/17845
4+
유형: DP, 배낭문제
5+
'''
6+
import sys
7+
input = sys.stdin.readline
8+
9+
n, k = list(map(int, input().rstrip().split())) # n은 최대 공부 시간, k는 과목 수
10+
info = [0] + [list(map(int, input().rstrip().split())) for _ in range(k)]
11+
dp = [[0] * (n + 1) for _ in range(k + 1)]
12+
13+
for i in range(1, k + 1): # i는 현재 고른 과목 수
14+
for j in range(1, n + 1): # j는 최대 시간
15+
if j < info[i][1]: # 시간이 넘친다면
16+
dp[i][j] = dp[i - 1][j] # 해당 과목은 제외
17+
else: # 시간이 넘치지 않는다면
18+
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - info[i][1]] + info[i][0]) # 해당 과목 제외한거 vs 해당 과목 중요도 + (해당 과목 제외) 해당 과목의 시간을 뺀 중요도
19+
20+
print(dp[k][n])
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## 🚀 11월 4주차 (11/25) 스터디 발제 주제: Dynamic Programming
2+
> 발제자: 김민정 (@Mingguriguri)
3+
4+
### 🗂️ 스터디 자료
5+
- PDF: [바로가기](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W02-%5BDP%5D/Study_BOJ_1125.pdf)
6+
<img width="800" alt="스크린샷 2024-11-25 오후 9 21 34" src="https://github.com/user-attachments/assets/5e0d93dd-4f45-4405-b0e1-ab9383c36bc1">
7+
8+
### 📖 문제
9+
- [백준 #12865. 평범한 배낭](https://www.acmicpc.net/problem/12865): DP, 배낭문제 / 골드5
10+
- 정답 코드: [Study_BOJ_12865_평범한배낭.py](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W02-%5BDP%5D/Study_BOJ_12865_%ED%8F%89%EB%B2%94%ED%95%9C%EB%B0%B0%EB%82%AD.py#L1-L30)
11+
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/bc8ac11b8f5d9d9e8e57baaef984b192b2b03312/_WeeklyChallenges/W02-%5BDP%5D/Study_BOJ_12865_%ED%8F%89%EB%B2%94%ED%95%9C%EB%B0%B0%EB%82%AD.py#L1-L30
12+
13+
### 💻 과제
14+
- [백준 #17845. 수강과목](https://www.acmicpc.net/problem/17845): DP, 배낭문제/ 골드5
15+
- 정답 코드: [Assignment_BOJ_17845_수강과목.py](https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W02-%5BDP%5D/Assignment_BOJ_17845_%EC%88%98%EA%B0%95%EA%B3%BC%EB%AA%A9.py#L1-L20)
16+
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/49c33469b809a7ff23a1c78270c73d4eeb45cb33/_WeeklyChallenges/W02-%5BDP%5D/Assignment_BOJ_17845_%EC%88%98%EA%B0%95%EA%B3%BC%EB%AA%A9.py#L1-L20
17+
18+
> **오늘 문제 풀이와 관련하여 참고하면 좋을 자료 모음 (문제에 대한 정답X, 공부하기 좋은 자료):**
19+
- [배낭 문제(KnapSack Problem) 그림으로 쉽게 이해하기](https://howudong.tistory.com/106)
20+
- [[알고리즘 트레이닝] 5장 - 동적계획법과 냅색(Knapsack) (백준 12865번 평범한 배낭 문제로 살펴보기)](https://chanhuiseok.github.io/posts/improve-6/)
21+
- [나무위키 - 배낭 문제](https://namu.wiki/w/%EB%B0%B0%EB%82%AD%20%EB%AC%B8%EC%A0%9C)
22+
23+
<img width="800" alt="스크린샷 2024-11-25 오후 9 21 18" src="https://github.com/user-attachments/assets/81c86394-9ee8-457b-9e64-6798a6c5ab4c">
872 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
BOJ #12865. 평범한 배낭 (골드5)
3+
https://www.acmicpc.net/problem/12865
4+
유형: Dynamic Programming(DP), Knapsack
5+
'''
6+
import sys
7+
input = sys.stdin.readline
8+
9+
# 입력
10+
N, K = map(int, input().split()) # N: 물품의 수, K: 버틸 수 있는 무게
11+
items = [[0, 0]]
12+
for _ in range(N):
13+
items.append(list(map(int, input().split())))
14+
15+
# DP
16+
knapsack = [[0] * (K+1) for _ in range(N+1)] # DP표는 0~K+1, 0~N+1로 구성
17+
18+
for i in range(1, N+1):
19+
for j in range(1, K+1):
20+
weight = items[i][0]
21+
value = items[i][1]
22+
23+
if j >= weight: # "현재최대무게j가 해당물건무게보다 큰 경우
24+
# 표의 윗 셀의 값과 현재물건의V+이전물건의V값의 최댓값을 DP[i][j]에 저장
25+
knapsack[i][j] = max(knapsack[i-1][j], knapsack[i-1][j-weight] + value)
26+
else: # #"현재최대무게j가 해당물건무게보다 작은 경우 (현재 물건을 담을 수 없는 경우)
27+
# 이전 값을 가져온다.
28+
knapsack[i][j] = knapsack[i-1][j]
29+
30+
print(knapsack[N][K])
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)

0 commit comments

Comments
 (0)