-
Notifications
You must be signed in to change notification settings - Fork 0
[luna] - class4-1 #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MoiJiyeon
wants to merge
11
commits into
main
Choose a base branch
from
luna
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e4d61eb
#38 1149.py
MoiJiyeon 5f8ae2f
#38 1629.py
MoiJiyeon ed32a05
#38 1932.py
MoiJiyeon 8249d18
#38 1991.py
MoiJiyeon da75771
#38 9465.py
MoiJiyeon 0b86cc7
#38 11053.py
MoiJiyeon 6611d23
#38 11725.py
MoiJiyeon bbfff20
#38 15650py
MoiJiyeon 7fe45e8
#38 15654.py
MoiJiyeon 2fd0b37
#38 15663.py
MoiJiyeon cd82a06
#38 1629.py 파일 수정
MoiJiyeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
|
|
||
| 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번 노드부터 출력 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]))})), | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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])) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zip을 활용하는 방법이 정말 파이썬 스럽고 좋네요 ㅎㅎ