♟️ 알고리즘/알고리즘_백준 73

[Python][백준] 11286. 절댓값 힙 / 우선순위 큐(S1)

🔗링크 :  https://www.acmicpc.net/problem/11286🗒️파이썬 코드 풀이import sysimport heapqinput = sys.stdin.readlineN = int(input())heap = []for _ in range(N): num = int(input()) if num == 0 : if heap: print(heapq.heappop(heap)[1]) pass else : print(0) else: heapq.heappush(heap,(abs(num),num)) 1. 최대, 최소 관련된 문제가 나오면 heap을 생각해봐야한다. 2. 조건에 따라 he..

[Python][백준] 11053. 가장 긴 증가하는 부분 수열/ DP (S2)

🔗링크 :  https://www.acmicpc.net/problem/11053🗒️파이썬 코드 풀이N = int(input())lst = list(map(int,input().split()))dp = [1] * N for i in range(1,len(lst)): for j in range(i): if lst[i] > lst[j] : dp[i] = max(dp[i],dp[j]+1)print(max(dp)) 1. 이전 값들은 계속 다음 값들에 영향을 주기 때문에 DP를 생각한다.  2. 브루트포스 방식으로 하나 하나 조사를 하고, 자신보다 작은 경우 dp 값을 갱신해준다. 3. 갱신 할 때, 가장 큰 값들만 갱신되게 하면 되므로 max 함수를 이용한다.  4. 이후..

[Python][백준] 11866. 요세푸스 문제 0 / 구현,큐(S4)

🔗링크 :  https://www.acmicpc.net/problem/11866🗒️파이썬 코드 풀이from collections import dequeN,K = map(int,input().split())res = []q = deque([i for i in range(1,N+1)])while q : for _ in range(K-1): q.append(q.popleft()) res.append(q.popleft())print('',end="") 1. 이 문제 같은 경우 queue로 간단하게 풀 수 있다. 2. K-1번 만큼 큐의 맨 앞 부분을 빼서 뒤에 붙여준다.  3. 그리고 K 번째 수를 제거해준다.  4. queue가 완전히 사라질 때 까지 반복한다.   🔎 내 풀이..

[Python][백준] 2468. 안전 영역/ DFS,BFS (S1)

🔗링크 :  https://www.acmicpc.net/problem/2805 🗒️파이썬 코드 풀이from collections import dequeN = int(input())graph = [list(map(int,input().split())) for _ in range(N)]mn = min(map(min,graph))mx = max(map(max,graph))di,dj = [0,1,0,-1],[1,0,-1,0]mx_count = 1count = 0 for c in range(mn,mx+1) : count = 0 visited = [[0] * N for _ in range(N)] for i in range(N): for j in range(N): ..

[Python][백준] 2805. 나무 자르기/ 이진탐색 (S2)

🔗링크 :  https://www.acmicpc.net/problem/2805 🗒️파이썬 코드 풀이N,M = map(int,input().split())lst = list(map(int,input().split()))start, end = 1, max(lst)cnt = 0 while start mid : cnt += (ls-mid) if cnt >= M : start = mid + 1 elif cnt  1. 이진탐색으로 문제를 해결하면 된다.  2. 이진탐색에서 start point와 end point를 만들어준다. 3. start와 end의 중간 값을 mid 값으로 주고,  전체 리스트 반복문으로 mid 보다 큰 값을 필터링하여 더해준다. (..

[Python][백준] 1914. 하노이 탑 / 재귀함수 (G5)

🔗링크 :  https://www.acmicpc.net/problem/1914🗒️파이썬 코드 풀이N = int(input())if N  1. 우선 첫번째로hanoi(n-1,from_pos,aux_pos,to_pos)해당 재귀함수를 통해 덩어리를 타고 타고 aux(보조)로 이동한다.   2. 두번째로print(from_pos,to_pos)  n = 1 인 경우와, 그 외 경우를 목적지로 보낸다.   3. 세번째로hanoi(n-1,aux_pos,to_pos,from_pos)그럼 다시 재귀함수를 타고 타고 aux를 기점(from)으로  바꿔준다.     📌  문제 코멘트재귀함수를 통해 타고타고 이동하고, print로 마무리 이동을 해준다...  대표적인 재귀함수 문제로 코드는 간단한데 이해가 너무 어렵..