알고리즘 17

깊이/너비 우선 탐색(DFS/BFS) Level 3 단어 변환 - python (프로그래머스)

소스 코드 from collections import deque def is_available_change(word, target): cnt = 0 for a1, a2 in zip(word, target): if a1 != a2: cnt += 1 return cnt == 1 def solution(begin, target, words): global answer answer = 100000000000000000000 if target not in words: return 0 visited = [False] * len(words) def dfs(word, depth): global answer if word == target: answer = min(answer, depth) return for index..

Python/알고리즘 2023.06.23

깊이/너비 우선 탐색(DFS/BFS) Level 2 타겟 네트워크 - python (프로그래머스)

def solution(n, computers): answer = 0 visited = [] def dfs(computers, node, visited): if node in visited: return 0 visited.append(node) connected_nodes = computers[node] for index in range(n): if node == index or connected_nodes[index] == 0: continue if index not in visited: dfs(computers, index, visited) return 1 for node in range(n): connected = dfs(computers, node, visited) answer += connect..

카테고리 없음 2023.06.23

깊이/너비 우선 탐색(DFS/BFS) Level 2 타겟 넘버 - python (프로그래머스)

소스 코드 def dfs(index, sum, target, numbers): if index == len(numbers): if sum == target: return 1 else: return 0 return dfs(index+1, sum + numbers[index], target, numbers) + dfs(index+1, sum - numbers[index], target, numbers) def solution(numbers, target): answer = 0 answer = dfs(0, 0, target, numbers) return answer print(solution([1,1,1,1,1], 3)) 풀이 위 문제는 백준에서도 본적 있는거 같아서 쉽게 풀었던거 같다. 나올수 있는 정수의 ..

Python/알고리즘 2023.06.23

완전탐색 Level 2 전력망을 둘로 나누기 - python (프로그래머스)

소스 코드 from collections import deque def make_graph(n, wires): # 트리형태를 그래프로 표현 graph = [[] for _ in range(n+1)] for node,neighbor in wires: graph[node].append(neighbor) graph[neighbor].append(node) return graph def solution(n, wires): answer_list = [] graph = make_graph(n, wires) def bfs(graph, start, visited): count = 1 visited[start] = True queue = deque([start]) while queue: node = queue.pople..

Python/알고리즘 2023.06.23

완전탐색 Level 2 모음사전 - python (프로그래머스)

소스 코드 def solution(word): word_list = ['A', 'E', 'I', 'O', 'U'] global answer answer = 0 def dfs(_word): global answer answer += 1 if _word == word: return True if len(_word) == 5: return False for string in word_list: if dfs(_word + string): return True for i in word_list: if dfs(i): return answer return answer 풀이 해당 문제는 dfs/bfs를 접해보지 않았기 때문에 기존에는 중복순열을 사용하여 가능한 모든 단어의 조합을 구한 다음 index를 사용하여 몇번째..

Python/알고리즘 2023.06.23

완전탐색 Level 2 피로도 - python (프로그래머스)

소스 코드 from itertools import permutations def solution(k, dungeons): path_list = list(permutations(dungeons, len(dungeons))) answer_list = [] for path in path_list: cur_fatigue = k count = 0 for dungeon in path: if cur_fatigue < dungeon[0]: break cur_fatigue -= dungeon[1] count += 1 answer_list.append(count) return max(answer_list) 풀이 문제의 제한사항에서 던전의 개수는 1이상 8이하라고 하였으므로 이 또한 모든 던전의 형태를 구한 다음 각 던전을..

Python/알고리즘 2023.06.23

완전탐색 Level 2 카펫 - python (프로그래머스)

소스 코드 def solution(brown, yellow): total_size = brown + yellow for i in range(1,total_size + 1): if total_size % i == 0: width = i height = total_size // i if width >= height and width == yellow // (height - 2) + 2: return [width, height] 풀이 해당 문제는 모든 카펫 모양을 구해보면서 문제 설명에서 언급한 조건에 맞는 카펫만 찾으면 된다. 가운데에 노란색 카펫이 있고 그 주위를 갈색 카펫으로 빠짐없이 덮기 위해서는 높이가 가로 길이보다 더 커서는 안되며 yellow의 층이 높아짐에 따라 width 는 width == y..

Python/알고리즘 2023.06.23

완전탐색 Level 2 소수 찾기 - python (프로그래머스)

코드 from itertools import permutations def solution(numbers): numbers = list(numbers) p_numbers = [] for i in range(1, len(numbers)+1): permute = permutations(numbers, i) p_numbers+= permute answer_list = [] for permute in p_numbers: number = "" for num in permute: number += num number = int(number) answer_list.append(number) answer = 0 answer_list = set(answer_list) for num in answer_list: if nu..

Python/알고리즘 2023.06.23

완전탐색 Level 1 최소직사각형 - python (프로그래머스)

코드 def solution(sizes): width = [] height = [] answer = 0 for i in range (len(sizes)): width.append(max(sizes[i])) height.append(min(sizes[i])) print(width, height) answer=max(width)*max(height) return answer 풀이 해당 문제는 특정 조건에 따라서 가로, 세로로 명함을 회전시킬 수 있다는 조건이 있다. 따라서 해당 기준을 가로로 할 것인지 세로로 할 것인지 정해야 했다. 나는 여기서는 가로를 최대 길이가 오도록 정하였다.(보통 명함은 가로가 더 길기 때문에) 따라서 명함들을 순회하며 가로, 세로 길이 중 가장 긴 값을 width 배열에 저장하고..

Python/알고리즘 2023.06.23

힙 Level 3 이중 우선순위 큐 - python (프로그래머스)

https://school.programmers.co.kr/learn/courses/30/lessons/42628 소스 코드 from heapq import heappush, heappop def solution(operations): min_heap = [] max_heap = [] for operation in operations: [operator, value] = operation.split(' ') value = int(value) if operator == "I": heappush(min_heap, value) heappush(max_heap, -value) elif operator == "D": if value == 1 and len(max_heap) > 0: heappop(max_heap)..

Python/알고리즘 2023.05.08