본문 바로가기

Python265

[백준 2631번] 줄세우기 n = int(input()) data = [] dp = [1]*n for i in range(n): data.append(int(input())) for i in range(n): for j in range(i): if data[i] > data[j]: dp[i] = max(dp[i],dp[j]+1) print(n - max(dp)) 이번 문제는 임의의 순서로 세워진 번호를 번호 순서대로 배치하기 위한 최소한의 이동횟수를 구하는 문제이다. 번호를 옮기는 횟수를 최소화하기 위해서는 번호 순서대로 세워져있는 어린이들을 최대한 많이 고정시키고 나머지만 이동시켜야 한다. 즉, 가장 긴 증가하는 부분수열 LIS(Longest Increasing Subsequence)의 길이를 구하고 이를 전체 어린이 수에서 빼.. 2023. 8. 14.
[백준 1525번] 퍼즐 from copy import deepcopy from collections import deque import sys sys.setrecursionlimit(10**7) graph = [list(map(int,input().split())) for _ in range(3)] final_graph = [[1,2,3],[4,5,6],[7,8,0]] dx = [-1,1,0,0] dy = [0,0,-1,1] visited = {} temp_graph = deepcopy(graph) for i in range(3): for j in range(3): if graph[i][j] == 0: first_x, first_y = i, j def changeToString(arr): # 퍼즐의 상태를 문자열로 바꿔주는 함.. 2023. 8. 12.
[백준 16637번] 괄호 추가하기 # 우선순위 X, 중첩괄호 X, 괄호 없어도 Ok # 괄호 안에는 연산자 1개만 가능 # 괄호를 적절히 추가해서 최대값 만들기 n = int(input()) formula = list(input()) # 최대값 초기화 max_value = float("-inf") # 주어진 값 2개를 연산하는 함수 def calculate(num1,operator,num2): if operator == '+': return num1 + num2 elif operator == '-': return num1 - num2 elif operator == "*": return num1 * num2 def dfs(index,value): global max_value # dfs로 마지막 값까지 계산을 마쳤을 경우 if index .. 2023. 8. 10.
[백준 2206번] 벽 부수고 이동하기 from collections import deque n, m = map(int,input().split()) maze = [list(map(int,list(input()))) for _ in range(n)] visited = [[[0]*m for _ in range(n)] for _ in range(2)] dx = [-1,1,0,0] dy = [0,0,-1,1] def bfs(): visited[0][0][0] = 1 queue = deque([(0,0,0)]) while queue: w,x,y = queue.popleft() if x == n-1 and y == m-1: return visited[w][x][y] for i in range(4): nx = x + dx[i] ny = y + dy[i].. 2023. 8. 4.
[백준 14923번] 미로 탈출 from collections import deque dx = [-1,1,0,0] dy = [0,0,-1,1] n, m = map(int,input().split()) hx, hy = map(int,input().split()) ex, ey = map(int,input().split()) maze = [list(map(int,input().split())) for _ in range(n)] visited = [[[0]*m for _ in range(n)] for _ in range(2)] def bfs(): visited[0][hx-1][hy-1] = 0 queue = deque([(0,hx-1,hy-1)]) while queue: v = queue.popleft() w, x, y = v[0], v[1].. 2023. 8. 4.
[프로그래머스 Lv.3] 네트워크 def solution(n, computers): answer = 0 graph = [[] for i in range(n)] visited = [False]*n for i,row in enumerate(computers): for j,value in enumerate(row): if i != j and value == 1: graph[j].append(i) def dfs(n): if visited[n]: return False visited[n] = True for i in graph[n]: if not visited[i]: dfs(i) return True for i in range(n): if dfs(i): answer += 1 return answer 이번 문제는 컴퓨터의 개수 n과 연결에 대한 정.. 2023. 7. 31.