Skip to content

Commit cd7a5ae

Browse files
authored
Merge pull request #25 from Mingguriguri/minjeong
Minjeong / 5월 1주차 / 3문제 ( 스킬체크 포함: 5문제)
2 parents d0b868a + e096c63 commit cd7a5ae

10 files changed

+268
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def solution(sizes):
2+
widths = []
3+
heights = []
4+
for i in range(len(sizes)):
5+
if sizes[i][0] >= sizes[i][1]:
6+
widths.append(sizes[i][0])
7+
heights.append(sizes[i][1])
8+
else:
9+
widths.append(sizes[i][1])
10+
heights.append(sizes[i][0])
11+
answer = max(widths) * max(heights)
12+
return answer
13+
'''
14+
# 다른 코드 A
15+
def solution(sizes):
16+
return max(max(x) for x in sizes) * max(min(x) for x in sizes)
17+
18+
'''
19+
20+
'''
21+
# 다른 코드 B
22+
def solution(sizes):
23+
row = 0
24+
col = 0
25+
for a, b in sizes:
26+
if a < b:
27+
a, b = b, a
28+
row = max(row, a)
29+
col = max(col, b)
30+
return row * col
31+
'''
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def solution(brown, yellow):
2+
carpet_size = brown + yellow # 카펫의 전체 크기
3+
4+
for w in range(1,int(carpet_size**0.5)+1): # 약수 범위 설정. 여기서 w는 너비를 의미함
5+
if carpet_size % w == 0: # 약수라면
6+
h = carpet_size // w # 전체 크기에서 너비로 나눈 값을 높이로 설정
7+
8+
if (2*w + 2*h - 4 == brown) and ((w-2)*(h-2) == yellow): # 조건에 해당하는지 확인
9+
return [max(w, h), min(w,h)]
10+
11+
# # 다른 풀이 1
12+
# def solution(brown, yellow):
13+
# for i in range(1, int(yellow**(1/2))+1):
14+
# if yellow % i == 0:
15+
# if 2*(i + yellow//i) == brown-4:
16+
# return [yellow//i+2, i+2]
17+
18+
# # 다른 풀이2
19+
# import math
20+
# def solution(brown, yellow):
21+
# w = ((brown+4)/2 + math.sqrt(((brown+4)/2)**2-4*(brown+yellow)))/2
22+
# h = ((brown+4)/2 - math.sqrt(((brown+4)/2)**2-4*(brown+yellow)))/2
23+
# return [w,h]
24+
25+
# # 다른 풀이 3
26+
# def solution(brown, yellow):
27+
# nm = brown + yellow
28+
# for n in range(1, nm+1):
29+
# if nm%n != 0:
30+
# continue
31+
# m = nm//n
32+
# if (n-2)*(m-2) == yellow:
33+
# return sorted([n, m], reverse = True)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def solution(answers):
2+
answer = []
3+
p1 = [1, 2, 3, 4, 5] # 1번 수포자가 찍는 방식
4+
p2 = [2, 1, 2, 3, 2, 4, 2, 5] # 2번 수포자가 찍는 방식
5+
p3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] # 3번 수포자가 찍는 방식
6+
scores = [0, 0, 0] # 각각 1번, 2번, 3번의 점수를 담은 리스트
7+
8+
for i in range(len(answers)): # 시험 문제의 정답 개수만큼 반복
9+
# 정답인지 판별. 이때 찍는 방식의 크기를 넘어가면 나머지 연산을 이용해 반복할 수 있도록 처리
10+
if p1[i%(len(p1))] == answers[i]:
11+
scores[0] += 1
12+
if p2[i%(len(p2))] == answers[i]:
13+
scores[1] += 1
14+
if p3[i%(len(p3))] == answers[i]:
15+
scores[2] += 1
16+
17+
if scores.count(max(scores)) == 3: # 3명이 동점인 경우
18+
answer = [1,2,3]
19+
elif scores.count(max(scores)) == 2: # 2명이 동점인 경우
20+
# 최고점 한 사람 찾은 후, 그 사람의 점수를 0점으로 바꾸고, 다시 최고점을 찾는다.
21+
answer.append(scores.index(max(scores))+1)
22+
scores[scores.index(max(scores))] = 0
23+
answer.append(scores.index(max(scores))+1)
24+
else: # 최고점이 1명만 있는 경우
25+
answer = [scores.index(max(scores))+1]
26+
27+
return answer
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def dfs(start, graph, visited, check_link):
2+
cnt = 1
3+
visited[start] = True # 방문 체크
4+
for v in graph[start]: # v에 연결되어 있는 다른 노드 탐색.
5+
if visited[v] == False and check_link[start][v] == True:
6+
# 연결되어 있지만 방문한 적 없다면 -> DFS 호출하고, 반환값을 cnt에 누적해서 더함
7+
cnt += dfs(v, graph, visited, check_link)
8+
return cnt
9+
10+
def solution(n, wires):
11+
answer = float("inf") # answer 초기화
12+
check_link = [[True]*(n+1) for _ in range(n+1)] # 끊은 간선인지 아닌지 체크하는 용도
13+
graph = [[] for _ in range(n+1)] # 송전탑 그래프
14+
15+
# 그래프 연결
16+
for u, v in wires:
17+
graph[u].append(v)
18+
graph[v].append(u)
19+
20+
# 전력망 나누기
21+
for a, b in wires:
22+
# 1. 간선 정보를 확인하면서 a, b 그룹에 연결된 송전탑 개수를 세기 위해서 a에서 b로 가는 간선을 끊어본다.
23+
visited = [False] * (n+1)
24+
check_link[a][b] = False # 양망향 연결 끊기
25+
# 2. a와 b에 붙어있는 송전탑 개수
26+
cnt_a = dfs(a, graph, visited, check_link) # a랑 붙어있는 송전탑 개수
27+
cnt_b = dfs(b, graph, visited, check_link) # b랑 붙어있는 송전탑 개수
28+
check_link[a][b] = True
29+
30+
answer = min(answer, abs(cnt_a - cnt_b)) # 송전탑 개수의 차이(절대값) 가장 작은 걸로 저장함
31+
32+
return answer
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
sys.setrecursionlimit(10000) #재귀 limit 설정(파이썬 최대 깊이 늘리는 모듈 이용)
3+
4+
# 1. DFS 함수
5+
def dfs(u):
6+
visited[u] = True # 방문표시
7+
for v in graph[u]: # 연결되어 있는 리스트 내 요소들도 방문 표시
8+
if visited[v] == False:
9+
visited[v] = True
10+
dfs(v) # 연결되어 있는 요소의 연결된 요소도 dfs로 탐색
11+
12+
# 2. 입력 및 초기화
13+
input = sys.stdin.readline
14+
N, M = map(int, input().strip().split()) # N: 정점의 개수, M: 간선의 개수
15+
graph = [[] for _ in range(N+1)] # 그래프 초기화
16+
visited = [False] * (N+1) # 방문여부 리스트 초기화
17+
18+
# 3. 그래프 만들기
19+
for _ in range(M):
20+
u, v = map(int, input().strip().split()) # 양 끝점 u,v
21+
graph[u].append(v)
22+
graph[v].append(u)
23+
24+
25+
# 4. 연결 요소 개수 찾기
26+
cnt = 0
27+
for i in range(1, N+1):
28+
if visited[i] == False:
29+
cnt += 1 # 첫 방문시에만 cnt에 +1
30+
dfs(i)
31+
32+
# 5. 정답 출력
33+
print(cnt)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from itertools import product
2+
3+
4+
def solution(clockHands):
5+
answer = 9876543210
6+
n = len(clockHands)
7+
8+
dy = [-1, 1, 0, 0, 0]
9+
dx = [0, 0, -1, 1, 0]
10+
11+
def rotate(a, b, t, arr):
12+
for k in range(5):
13+
y, x = a + dy[k], b + dx[k]
14+
if 0 <= y < n and 0 <= x < n:
15+
arr[y][x] = (arr[y][x] + t) % 4
16+
17+
for case in product(range(4), repeat=n): # 첫째줄 최대4번까지 회전 한다는 가정 하에 모든 경우의 수를 만든다.
18+
arr = [i[:] for i in clockHands] # 깊은 복사는 deepcopy 보다 slicing 이 빠름
19+
20+
for j in range(n): # case 를 가지고 첫번째 줄만 회전 시킨다
21+
rotate(0, j, case[j], arr)
22+
23+
result = sum(case) # 첫번째 줄 조작 횟수의 합
24+
25+
for i in range(1, n): # 두번째 줄부터 체크
26+
for j in range(n):
27+
if arr[i-1][j]: # 12시 가있지 않은 시계만 조작
28+
temp = 4 - arr[i-1][j] # 12시에 가도록 하기 위한 조작 횟수
29+
rotate(i, j, temp, arr) # 회전
30+
result += temp # 조작 횟수 누적
31+
32+
if sum(arr[n-1]): # 마지막 라인에 12시를 향하지 않는 시계가 존재
33+
continue # pass
34+
35+
answer = min(answer, result) # 시계가 모두 12시를 가리킨다면 answer을 최솟값으로 갱신
36+
37+
return answer
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def solution(m, n, puddles):
2+
puddles = [[q, p] for [p, q] in puddles]
3+
4+
# dp 초기화
5+
dp = [[0] * (m+1) for _ in range(n+1)]
6+
dp[1][1] = 1
7+
8+
# 장애물 있는 경우가 아니면 dp에 누적하여 값 더하기
9+
for i in range(1, n+1):
10+
for j in range(1, m+1):
11+
if [i, j] in puddles:
12+
dp[i][j] = 0
13+
else:
14+
dp[i][j] += (dp[i-1][j] + dp[i][j-1])% 1000000007
15+
16+
# 정답 반환
17+
return dp[n][m]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
sys.setrecursionlimit(10000) #재귀 limit 설정(파이썬 최대 깊이 늘리는 모듈 이용)
3+
4+
# 1. DFS 함수
5+
def dfs(u):
6+
visited[u] = True # 방문표시
7+
for v in graph[u]: # 연결되어 있는 리스트 내 요소들도 방문 표시
8+
if visited[v] == False:
9+
visited[v] = True
10+
dfs(v) # 연결되어 있는 요소의 연결된 요소도 dfs로 탐색
11+
12+
# 2. 입력 및 초기화
13+
input = sys.stdin.readline
14+
N, M = map(int, input().strip().split()) # N: 정점의 개수, M: 간선의 개수
15+
graph = [[] for _ in range(N+1)] # 그래프 초기화
16+
visited = [False] * (N+1) # 방문여부 리스트 초기화
17+
18+
# 3. 그래프 만들기
19+
for _ in range(M):
20+
u, v = map(int, input().strip().split()) # 양 끝점 u,v
21+
graph[u].append(v)
22+
graph[v].append(u)
23+
24+
25+
# 4. 연결 요소 개수 찾기
26+
cnt = 0
27+
for i in range(1, N+1):
28+
if visited[i] == False:
29+
cnt += 1 # 첫 방문시에만 cnt에 +1
30+
dfs(i)
31+
32+
# 5. 정답 출력
33+
print(cnt)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def solution(array, commands):
2+
answer = []
3+
for i in range(len(commands)):
4+
start = commands[i][0]-1
5+
end = commands[i][1]
6+
k = commands[i][2]-1
7+
temp = array[start:end]
8+
temp.sort()
9+
answer.append(temp[k])
10+
return answer
11+
'''
12+
# 다른 코드
13+
def solution(array, commands):
14+
return [sorted(array[i-1:j])[k-1] for i,j,k in commands]
15+
'''
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def solution(arr):
2+
stack = []
3+
for i in range(len(arr)):
4+
if len(stack) == 0: # 스택이 비어 있다면 추가
5+
stack.append(arr[i])
6+
else: # 스택이 비어있지 않다면
7+
if arr[i] != stack[-1]: # 가장 최근의 스택값과 현재 값을 비교하고, 다를 경우에만 스택에 추가
8+
stack.append(arr[i])
9+
10+
return stack

0 commit comments

Comments
 (0)