Skip to content
Open
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
25 changes: 25 additions & 0 deletions luna/class4-1/11053.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys

def largest_A(n, A_s):
dp = [1] * n

for i in range(1, n):
for j in range(i):
if A_s[j] < A_s[i]:
dp[i] = max(dp[i], dp[j] + 1)

return max(dp)

# 입력 처리
input = sys.stdin.read
data = input().strip().splitlines()

# 첫째 줄: 수열의 크기
n = int(data[0])

# 둘째 줄: 수열을 이루고 있는 N개의 A_i들
A_s = list(map(int, data[1].split()))

# 출력 결과
output = str(largest_A(n, A_s))
sys.stdout.write(output + '\n')
13 changes: 13 additions & 0 deletions luna/class4-1/1149.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
num_house, *costs = map(int,open(0).read().split()) # num_house = 3, costs = [26, 40, 83, 49, 60, 57, 13, 89, 99]

for i in range(3, len(costs)): # cost의 인덱스는 0부터 시작
if i == 3:
costs[i] += min(costs[1], costs[2])
elif i % 3 == 0:
costs[i] += min(costs[i-1], costs[i-2])
elif i % 3 == 1:
costs[i] += min(costs[i-2], costs[i-4])
else:
costs[i] += min(costs[i-4], costs[i-5])

print(min(costs[-1],costs[-2],costs[-3]))
20 changes: 20 additions & 0 deletions luna/class4-1/11725.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from collections import deque

N, *edges = map(int, open(0).read().split()) # edges = [1, 6, 6, 3, ...]
tree = [[] for _ in range(N + 1)]

for a, b in zip(edges[::2], edges[1::2]): # 2씩 건너뛰며 zip [1, 6], [6, 3], ...
tree[a] += [b] # 연결된 노드 리스트 저장
tree[b] += [a]
Comment on lines +3 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zip을 활용하는 방법이 정말 파이썬 스럽고 좋네요 ㅎㅎ


parents = [0] * (N + 1)
queue = deque([1])

while queue:
current = queue.popleft()
for next_node in tree[current]:
if not parents[next_node]:
parents[next_node] = current
queue.append(next_node)

print('\n'.join(map(str, parents[2:]))) # 2번 노드부터 출력
17 changes: 17 additions & 0 deletions luna/class4-1/15650.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
from itertools import combinations

def generate_combinations(n, m):
# 1부터 n까지의 수 중에서 m개를 선택하는 조합(c) 생성
return combinations(range(1, n+1), m)

# 입력 처리
input = sys.stdin.read
data = input().strip().splitlines()

# 첫째 줄: 자연수 N, M
N, M = map(int, data[0].split())

# 출력 결과
output = '\n'.join(' '.join(map(str, combo)) for combo in generate_combinations(N, M))
sys.stdout.write(output + '\n')
19 changes: 19 additions & 0 deletions luna/class4-1/15654.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
from itertools import permutations

def generate_permutations(numbers, m):
# n개의 수(=numbers) 중에서 m개를 선택하는 순열(p) 생성
return sorted(permutations(numbers, m))

# 입력 처리
input = sys.stdin.read
data = input().strip().splitlines()

# 첫째 줄: 자연수 N, M (1 <= M <= N <= 8)
N, M = map(int, data[0].split())
# 둘째 줄: N개의 수 (x <= 10,000)
nums = list(map(int, data[1].split()))

# 출력 결과
output = '\n'.join(' '.join(map(str, perm)) for perm in generate_permutations(nums, M))
sys.stdout.write(output + '\n')
13 changes: 13 additions & 0 deletions luna/class4-1/15663.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from itertools import permutations, starmap

# open(0) : 표준 입력 받아오기
NM, nums = [*open(0)] # NM = "3 1\n", nums = "4 4 2\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한줄씩 값을 받아올때 정말 편하겠네요
좋은거 하나 알아갑니다!


# nums의 개행문자를 지우고 int로 변환해서 set에 넣은 후 정렬
sorted_set = sorted({*permutations(map(int, nums.split()), int(NM[2]))}) # {(2, 4, 4), (4, 2, 4), (4, 4, 2)}

for perm in sorted_set:
print(*perm)

# 아래처럼 starmap 사용 가능
# *starmap(print,sorted({*permutations(map(int, nums.split()), int(NM[2]))})),
2 changes: 2 additions & 0 deletions luna/class4-1/1629.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A, B, C = map(int, input().split())
print(pow(A, B, C))
22 changes: 22 additions & 0 deletions luna/class4-1/1932.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys

def max_sum_triangle(triangle, n):
for i in range(n-2, -1, -1): # 아래에서 위로 역순으로 이동, i의 범위는 n-1 ~ 0
for j in range(i+1): # j의 범위는 i의 x 좌표의 +0 ~ +1
triangle[i][j] += max(triangle[i+1][j], triangle[i+1][j+1]) # 아래 두 값 중 더 큰 값을 더함

return triangle[0][0] # 최종적으로 가장 위에 최대 합이 남음

# 입력 처리
input = sys.stdin.read
data = input().strip().splitlines()

# 첫째 줄: 삼각형의 크기
n = int(data[0])

# 다음 n줄: 정수 삼각형의 정보 (각 줄의 값들, 공백 구분)
triangle = [list(map(int, line.split())) for line in data[1:]]

# 출력 결과
output = str(max_sum_triangle(triangle, n))
sys.stdout.write(output + '\n')
32 changes: 32 additions & 0 deletions luna/class4-1/1991.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
N, *lines = open(0).read().splitlines() # 줄마다 읽어옴, n = 7, lines = ['A B C', ...]
data = {a: (b, c) for a, b, c in (line.split() for line in lines)} # {'A': ('B', 'C'), ...}

def order_n1(i): # 전위 순회
print(i, end='') # 루트 출력
left, right = data[i]
if left != '.':
order_n1(left) # 왼쪽 자식 순회
if right != '.':
order_n1(right) # 오른쪽 자식 순회

def order_n2(i): # 중위 순회
left, right = data[i]
if left != '.':
order_n2(left) # 왼쪽 자식 순회
print(i, end='') # 루트 출력
if right != '.':
order_n2(right) # 오른쪽 자식 순회

def order_n3(i): # 후위 순회
left, right = data[i]
if left != '.':
order_n3(left) # 왼쪽 자식 순회
if right != '.':
order_n3(right) # 오른쪽 자식 순회
print(i, end='') # 루트 출력

order_n1('A')
print()
order_n2('A')
print()
order_n3('A')
17 changes: 17 additions & 0 deletions luna/class4-1/9465.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
N, *data = [*open(0)]
lines = [list(map(int, _.split())) for _ in data]

for i in range(int(N)):
num = lines[3*i][0]
top_row = lines[3*i + 1]
bottom_row = lines[3*i + 2]

if num > 1:
top_row[1] += bottom_row[0]
bottom_row[1] += top_row[0]

for j in range(2, num):
top_row[j] += max(bottom_row[j-1], bottom_row[j-2])
bottom_row[j] += max(top_row[j-1], top_row[j-2])

print(max(top_row[-1], bottom_row[-1]))