본문 바로가기

Algorithm475

[파이썬] 백준 28078 : 중력 큐 (골드5) [파이썬] 백준 28078 : 중력 큐 (골드5)[백준 28078 : 중력 큐 (골드5)](28078번: 중력 큐)풀이방향성 생각덱 구현하기.어느 방향인지 나타내는 state 변수를 사용.개수는 counts로 관리해주기코드from collections import dequeimport sysinput = sys.stdin.readlinestate = 0counts = [0,0]Q = deque()for _ in range(int(input())): cmd = input().split() if cmd[0] == 'pop' and Q: counts[Q.pop()] -= 1 elif cmd[0] == 'push': if cmd[1] == 'b': Q.. 2025. 5. 21.
[파이썬] 백준 2504 : 괄호의 값 (골드5) [파이썬] 백준 2504 : 괄호의 값 (골드5)[백준 2504 : 괄호의 값](2504번: 괄호의 값)풀이방향성 생각닫는 괄호가 일치할 때 스택에 int만 쌓여있는 경우는 전부 temp에 더해줘서 누적값 구하기.int를 전부 터뜨리고도 stack이 남았으면 그 괄호는 현재 닫는 괄호와 일치하는 여는 괄호이므로 제거하기.코드stack = []string = input().strip()if string.count('[') != string.count(']') or string.count('(') != string.count(')'): print(0) exit()for s in string: if s in '([': stack.append(s) else: if .. 2025. 5. 18.
[파이썬] 백준 13398 : 연속합 2 (골드5) [파이썬] 백준 13398 : 연속합 2 (골드5)[백준 13398 : 연속합 2](13398번: 연속합 2)풀이방향성 생각제거할 노드를 선택/미선택 2개 상태로 나누어서 접근한다.$2*N$ 크기 배열로 만들고 dp 테이블 바텀업으로 업데이트.코드N = int(input())arr = list(map(int,input().split()))INF = float('inf')dp = [[-INF]*N for _ in range(2)]dp[0][0] = arr[0]for i in range(N-1): dp[0][i+1] = max(dp[0][i]+arr[i+1],arr[i+1]) dp[1][i+1] = max(dp[0][i],dp[1][i]+arr[i+1])print(max(max(dp[0]),max.. 2025. 5. 18.
[파이썬] 백준 14461 : 소가 길을 건너간 이유 7 (골드2) [파이썬] 백준 14461 : 소가 길을 건너간 이유 7 (골드2)백준 14461 : 소가 길을 건너간 이유 7풀이방향성 생각3차원 다익스트라같은 노드에 도착하더라도, 몇 번째 사이클에 도착했는지가 중요하다.사이클이 3이므로 $3NN$ 크기의 방문 배열을 만든다.코드import heapq as hqimport sysinput = lambda : sys.stdin.readline().rstrip()INF = float('inf')dires = [(1,0),(-1,0),(0,1),(0,-1)]inside = lambda x,y : 0 V[turn][y][x]: continue for dx,dy in dires: nx,ny = x+dx,y+dy if not insid.. 2025. 5. 15.