Python/Coding_Problems
-
백준(BeakJoon)_1260_DFS와BFSPython/Coding_Problems 2022. 12. 18. 12:05
# DFS를 재귀로 풀었을 때 from collections import deque def DFS(graph, visited, start_node): visited[start_node] = True print(start_node, end=" ") for i in graph[start_node]: if not visited[i]: DFS(graph,visited,i) def BFS(graph, visited, start_node): queue = deque() queue.append(start_node) # 노드를 qeue에 넣어준다 visited[start_node] = True while queue: current_node = queue.popleft() print(current_node, end=" ..
-
프로그래머스(Programmers)_Level2_올바른괄호Python/Coding_Problems 2022. 12. 10. 13:05
첫번째시도 def solution(s): stack = [] for i in s: if len(stack) > 0 and stack[-1] != i: stack.pop() else: stack.append(i) return True if len(stack) == 0 else False 정확성: 50.2 효율성: 30.5 합계: 80.7 / 100.0 3,4,5,11,18 틀림 두번째시도 def solution(s): stack = [] if s[0] == ")": return False else: for i in s: if len(stack) > 0 and stack[-1] != i: stack.pop() else: stack.append(i) return True if len(stack) == 0 el..
-
프로그래머스(Programmers)_Level1_같은숫자는싫어Python/Coding_Problems 2022. 12. 10. 11:02
# 처음에 생각한것 s = list("baabaa") for i in range(len(s)-1): if s[i] == s[i+1]: wanted_remove_index = [i, i+1] while len(wanted_remove_index): s = [v for idx_s, v in enumerate(s) if idx_s not in wanted_remove_index] wanted_remove_index.clear() 위에처럼 하면은.. wanted_remove_ndex = [1,2] 라고 할 때 i는 1인 상태에서 위의 인덱스를 뽑아낸것.. 무튼.. 저걸 다 처리하고나면은 1,2의 인덱스가 없어진 [b,b,a,a] 가 되는데! for문의 i는 1에서 2로 넘어간다. 따랏 [b,b,a,a]에서 a부..