Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions YoonYn9915/binary search/2025-04-18-[백준]-#2470-두 용액.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys

N = int(sys.stdin.readline())
arr = sorted(map(int, sys.stdin.readline().split()))

left = 0
right = N - 1

answer = (arr[left], arr[right], abs(arr[left] + arr[right]))

# 투 포인터 사용해서 절대값이 0에 가장 가까운 두 값 찾기
while left < right:
total = arr[left] + arr[right]
abs_total = abs(total)

if abs_total < answer[2]:
answer = (arr[left], arr[right], abs_total)
if abs_total == 0:
break
# 두 용액의 값이 0보다 크면 right를 줄여서 합을 작게 만든다
if total > 0:
right -= 1
else:
# 두 용액의 값이 0보다 작으면 left를 늘려서 합을 크게 만든다
left += 1

print(f"{answer[0]} {answer[1]}")
43 changes: 43 additions & 0 deletions YoonYn9915/binary search/2025-04-26-[백준]-#2473-세 용액.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys

inp = sys.stdin.readline

N = int(inp())
arr = list(map(int, inp().split()))
arr.sort()

min_value = int(1e10)
answer = [-1, -1, -1]

# 각 용액을 순회하며
for i in range(N):
left = i + 1
right = N - 1

# i번 용액을 넣었을 때 값이 가장 0에 가까운 두 값 찾기
while left < right:
if left == i:
left = i + 1
continue
elif right == i:
right = i - 1
continue

total = arr[i] + arr[left] + arr[right]

# 세 용액의 합이 0에 가장 가까우면 업데이트
if abs(total) < min_value:
min_value = abs(total)
answer = [arr[i], arr[left], arr[right]]

if total == 0:
break
elif total < 0:
left += 1
else:
right -= 1

answer.sort()

for ans in answer:
print(ans, end=" ")
34 changes: 34 additions & 0 deletions YoonYn9915/dp/2025-04-26-[백준]-#2240-자두 나무.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from sys import stdin

T, W = map(int, stdin.readline().split())


lst = [0]
dp = [[0]*(W+1) for _ in range(T+1)]

# 입력 받기
for _ in range(T):
lst.append(int(stdin.readline()))

# DP 테이블 채우기
for i in range(1, T+1):
if lst[i] == 1:
# 이동을 한 번도 안 했을 경우 (항상 1번 나무 아래 있음)
dp[i][0] = dp[i-1][0] + 1 # 자두 먹으면 +1
else:
# 1번 나무 아래 있는데 2번 나무에서 자두 떨어지면 못 먹음
dp[i][0] = dp[i-1][0]

# 이동 횟수 1회 이상부터
for j in range(1, W+1):
if lst[i] == 2 and j % 2 == 1:
# 홀수번 이동했을 때는 2번 나무 아래 있음 → 2번 나무에서 자두 떨어지면 +1
dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + 1
elif lst[i] == 1 and j % 2 == 0:
# 짝수번 이동했을 때는 1번 나무 아래 있음 → 1번 나무에서 자두 떨어지면 +1
dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + 1
else:
# 자두 못 먹을 경우, 이동하거나 이동 안 하거나 둘 중 최대값만 가져오기
dp[i][j] = max(dp[i-1][j-1], dp[i-1][j])

print(max(dp[-1]))