max(n1, n2, n3)
def solution(answers):
answer = []
people = {}
people['m1'] = [[1, 2, 3, 4, 5], 5, 0]
people['m2'] = [[2, 1, 2, 3, 2, 4, 2, 5], 8, 0]
people['m3'] = [[3, 3, 1, 1, 2, 2, 4, 4, 5, 5], 10, 0]
for i in range(len(answers)):
for p in people:
Len = people[p][1]
if people[p][0][i%Len] == answers[i]:
people[p][2] += 1
M = max(people['m1'][2], people['m2'][2], people['m3'][2])
if people['m1'][2] == M:
answer.append(1)
if people['m2'][2] == M:
answer.append(2)
if people['m3'][2] == M:
answer.append(3)
return answer
def solution(brown, yellow):
answer = []
C = 0
R = 0
for row in range(1, brown + 1):
for col in range(1, brown + 1):
if (col * 2 + (row-2) * 2) == brown and (row*col - brown) == yellow:
answer.append(col)
answer.append(row)
return answer
규칙 찾기
1. 갈색 개수: 2*세로 + 2*(가로-2) = 갈색 개수
2. 노란색 개수: 가로*세로-갈색
3. for 문 돌려서 모든 경우의 세로의 길이와 가로의 길이를 찾아야 함
1억번의 연산이 1초가 걸린다.
그래프에 방향이 존재한다.
그래프에 방향이 존재하지 않는다.
adj = [[] for i in range(5)]
# 각 노드가 방문한 정보를 리스트 자료형으로 표현
visited = [False]*N
stack = []
# graph는 연결 리스트(인접리스트)가 제공된다고 가정
def dfs(graph, start):
# start는 시작 위치이기 때문에 방문했다고 표시
# start부터 그래프 탐색을 진행하기 위해 stack에 start 노드를 추가
visited[start] = True
stack.append(start)
while len(stack) != 0:
# 스택의 마지막에 있는 노드를 기준으로 탐색 진행
current = stack.pop()
for next in graph[current]:
if visited[next] == False:
visited[next] = True
stack.append(next)
dfs(graph, 1)
인접 행렬 방법의 시간복잡도 = O(V^2)
(노드의 개수의 제곱)인접 리스트 방법의 시간복잡도 = O(V+E)
(노드의 개수 + 간선의 개수)N = int(input()) # 컴퓨터 개수
visited = [False]*(N+1)
M = int(input()) # 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수
stack = []
con = [[] for i in range(N+1)]
for i in range(M):
a, b = map(int, input().split())
con[a].append(b)
con[b].append(a)
visited[1] = True
stack.append(1)
cnt = 0
while len(stack) !=0:
cur = stack.pop()
for next in con[cur]:
if visited[next] == False:
visited[next] = True
stack.append(next)
cnt += 1
print(cnt)
단방향이 아니라 양방향이라는 걸 생각해야한다.
from collections import deque
# 각 노드가 방문한 정보를 리스트 자료형으로 표현
visited = [False]*N
queue = deque()
# graph는 연결리스트가 제공된다고 가정
def bfs(graph, start):
# start는 시작 위치이기 떄문에 방문했다고 표현
# start부터 그래프 탐색을 진행하기 위해 queue에 start 노드를 추가
visited[start] = True
queue.append(start)
# 큐가 빌 때까지 탐색을 진행
while len(queue) != 0:
# 큐의 첫번째 노드를 기준으로 탐색 진행
current = queue.pop()
for next in graph[current]:
if visited[next] == False:
visited[next] = True
queue.append(next)
bfs(graph, 1)
인접 행렬 방법의 시간복잡도 = O(V^2)
인접 리스트 방법의 시간복잡도 = O(V+E)
from collections import deque
visited = [[-1 for i in range(100)] for j in range(100)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
q = deque()
def solution(maps):
answer = 0
q.append([0, 0])
visited[0][0] = 1
N, M = len(maps), len(maps[0])
while len(q) != 0:
curr = q.popleft()
x, y = curr[0], curr[1]
for i in range(4):
next_x = x +dx[i]
next_y = y + dy[i]
if 0 <= next_x and next_x < N and 0 <= next_y and next_y < M:
if visited[next_x][next_y] == -1 and maps[next_x][next_y] != 0:
visited[next_x][next_y] = visited[x][y] +1
q.append([next_x, next_y])
return visited[N-1][M-1]
⇒ 그래프 탐색을 수행할 수 있다.
시작 노드로부터 목표 노드까지 거리를 최단 거리를 찾아야 한다.
⇒ 시작 노드 (1,1) 부터 시작하여 (N, M)까지의 최단 거리가 K 라고 구할 수 있다.
N, M = map(int, input().split())
num = [i + 1 for i in range(N)]
visited = [False]*N
answer = []
def dfs(cnt):
# print(answer)
if cnt == M:
print(*answer)
return
for i in range(N):
if visited[i] == True:
continue
visited[i] = True
answer.append(num[i])
dfs(cnt + 1)
answer.pop()
visited[i] = False
dfs(0)
유익한 글이었습니다.