diff --git "a/H0ngJu/BFS/\354\210\250\353\260\224\352\274\255\354\247\2104.py" "b/H0ngJu/BFS/\354\210\250\353\260\224\352\274\255\354\247\2104.py" new file mode 100644 index 00000000..d1db02cf --- /dev/null +++ "b/H0ngJu/BFS/\354\210\250\353\260\224\352\274\255\354\247\2104.py" @@ -0,0 +1,33 @@ +import sys +from collections import deque + +def input(): return sys.stdin.readline() + +N, K = map(int, input().split()) + +visited = [0] * 100001 +time = 0 +parent = [-1] * 100001 + +q = deque([N]) +visited[N] = 1 + +while q: + size = len(q) + for _ in range(size): + location = q.popleft() + if location == K: + print(time) + answer = [] + while location != -1: + answer.append(location) + location = parent[location] + answer.reverse() + print(" ".join(map(str, answer))) + break + for next_location in (location + 1, location - 1, location * 2): + if 0 <= next_location <= 100000 and not visited[next_location]: + q.append(next_location) + visited[next_location] = 1 + parent[next_location] = location + time += 1 diff --git "a/H0ngJu/DP/RGB\352\261\260\353\246\254 2.py" "b/H0ngJu/DP/RGB\352\261\260\353\246\254 2.py" new file mode 100644 index 00000000..3ccfcfe4 --- /dev/null +++ "b/H0ngJu/DP/RGB\352\261\260\353\246\254 2.py" @@ -0,0 +1,21 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +N = int(input()) +INF = 1000*1000+1 + +house = [[int(x) for x in input().split()] for _ in range(N)] +cost = INF + +for first_house in range(3): + dp = [[INF] * 3 for _ in range(N)] + dp[0][first_house] = house[0][first_house] + for i in range(1, N): + dp[i][0] = min(dp[i-1][1], dp[i-1][2]) + house[i][0] + dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + house[i][1] + dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + house[i][2] + dp[N-1][first_house] = INF + cost = min(cost, min(dp[N-1])) + +print(cost) \ No newline at end of file diff --git a/H0ngJu/README.md b/H0ngJu/README.md index 1da5a7b5..362c56db 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -9,6 +9,10 @@ | 5차시 | 2024.03.16 | 구현 | [요세푸스 문제](https://www.acmicpc.net/problem/1158) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/161 | | 6차시 | 2024.03.19 | 스택 | [오큰수](https://www.acmicpc.net/problem/17298) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/164 | | 7차시 | 2024.03.22 | DP | [1,2,3 더하기](https://www.acmicpc.net/problem/9095) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/166 | +| 8차시 | 2024.03.16 | DP | [쉬운 계단 수](https://www.acmicpc.net/problem/10844) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/170 | +| 9차시 | 2024.03.22 | DP | [RGB거리 2](https://www.acmicpc.net/problem/17404) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/172 | +| 10차시 | 2024.04.03 | BFS | [숨바꼭질 4](https://www.acmicpc.net/problem/13913) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/177 | +| 11차시 | 2024.04.07 | 구현 | [사탕 게임](https://www.acmicpc.net/problem/9095) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/178 | | 12차시 | 2024.04.09 | DFS | [ABCDE](https://www.acmicpc.net/problem/13023) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/180 | --- diff --git "a/H0ngJu/\352\265\254\355\230\204/\354\202\254\355\203\225 \352\262\214\354\236\204.py" "b/H0ngJu/\352\265\254\355\230\204/\354\202\254\355\203\225 \352\262\214\354\236\204.py" new file mode 100644 index 00000000..e2c0a578 --- /dev/null +++ "b/H0ngJu/\352\265\254\355\230\204/\354\202\254\355\203\225 \352\262\214\354\236\204.py" @@ -0,0 +1,44 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +def eat(row, col): + row_cnt = 0 + col_cnt = 0 + tmp_col = 1 + tmp_row = 1 + for idx in range(N-1): + # 열에서 먹을 때 + if data[row][idx] == data[row][idx+1]: + tmp_col += 1 + col_cnt = max(col_cnt, tmp_col) + else: + tmp_col = 1 + + # 행에서 먹을 때 + if data[idx][col] == data[idx+1][col]: + tmp_row += 1 + row_cnt = max(row_cnt, tmp_row) + else: + tmp_row = 1 + + return max(row_cnt, col_cnt) + + +N = int(input()) +data = [[x for x in input()] for _ in range(N)] +directions = [(0,-1), (0,1), (-1,0),(1,0)] +cnt = 0 + +for i in range(N): + for k in range(N): + for dir in directions: + dx, dy = dir + x = i+dx + y = k+dy + if 0<=x단어 마방진 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 | | 36차시 | 2024.02.21 | 문자열 | 회문은 회문아니야!! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 | | 37차시 | 2024.03.05 | 백트래킹 | 석유 시추 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 | -| 37차시 | 2024.03.08 | 트라이 | 전화번호 목록 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 | +| 38차시 | 2024.03.08 | 트라이 | 전화번호 목록 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 | +| 39차시 | 2024.03.14 | 문자열 | 물약 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/160 | --- diff --git "a/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" "b/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" new file mode 100644 index 00000000..efcfe576 --- /dev/null +++ "b/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" @@ -0,0 +1,45 @@ +stdin = open(0) + +def input(): + return stdin.readline().rstrip() + +N, M = map(int, input().split()) + +prices = {} + +for _ in range(N): + item, price = input().split() + prices[item] = int(price) + +recipes = [] + +for _ in range(M): + target, formula = input().split('=') + terms = formula.split('+') + recipes.append([target, terms]) + +def updatePrice(target, terms): + price = 0 + + for term in terms: + count = int(term[0]) + item = term[1:] + + if item not in prices: + return + + price += count * prices[item] + + if target not in prices or prices[target] > price: + prices[target] = price + +for _ in range(M): + for recipe in recipes: + updatePrice(recipe[0], recipe[1]) + +if 'LOVE' not in prices: + print('-1') + exit() + +answer = min(prices['LOVE'], 1000000001) +print(answer) diff --git "a/pknujsp/BFS/42-\353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260.py" "b/pknujsp/BFS/42-\353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260.py" new file mode 100644 index 00000000..478f5364 --- /dev/null +++ "b/pknujsp/BFS/42-\353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260.py" @@ -0,0 +1,73 @@ +from collections import * + +map_size = int(input()) +matrix = [list(map(int, input().split())) for _ in range(map_size)] + +directions = [(1, 0), (0, -1), (-1, 0), (0, 1)] +island_id = 2 +costs = [[0] * map_size for _ in range(map_size)] + +# 다리를 놓을 위치를 저장할 큐 +bridge_queue = deque() + +# 전체 맵을 순회하면서 각 섬에 ID 부여 +for row in range(map_size): + for col in range(map_size): + + if matrix[row][col] != 1: + continue + + # 현재 칸을 시작으로 하는 섬에 고유 ID 할당 + matrix[row][col] = island_id + queue = deque([(row, col)]) + + # BFS를 통해 같은 섬인 육지에 ID 할당 + while queue: + r, c = queue.popleft() + for dr, dc in directions: + nr, nc = r + dr, c + dc + + if not 0 <= nr < map_size or not 0 <= nc < map_size: + continue + + # 다른 섬과 연결된 바다 영역에 접근하면 종료 + if 1 < matrix[nr][nc] < island_id: + print(1) + exit() + + # 새로운 육지 발견 시 큐에 추가 + if matrix[nr][nc] == 1: + queue.append((nr, nc)) + # 육지와 바로 맞닿은 바다 영역을 다리 후보로 추가 + elif matrix[nr][nc] == costs[nr][nc] == 0: + bridge_queue.append((nr, nc, 1, island_id)) + + costs[nr][nc] = 1 + matrix[nr][nc] = island_id + + island_id += 1 + +min_cost = int(1e6) + +# 다리 후보 위치를 순회하며 최소 다리 길이 계산 +while bridge_queue: + r, c, cost, curr_island_id = bridge_queue.popleft() + + for dr, dc in directions: + nr, nc = r + dr, c + dc + + if not 0 <= nr < map_size or not 0 <= nc < map_size: + continue + + # 아직 다리가 놓이지 않은 바다 영역이면 + # 다리 길이를 증가시키고 큐에 추가 + if costs[nr][nc] == 0: + bridge_queue.append((nr, nc, cost + 1, curr_island_id)) + + costs[nr][nc] = cost + 1 + matrix[nr][nc] = curr_island_id + # 다른 섬과 연결된 다리 영역에 접근하였다면 최소 비용을 갱신 + elif matrix[nr][nc] > 1 and matrix[nr][nc] != curr_island_id: + min_cost = min(min_cost, cost + costs[nr][nc]) + +print(min_cost) \ No newline at end of file diff --git "a/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" "b/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" new file mode 100644 index 00000000..e1cc927a --- /dev/null +++ "b/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" @@ -0,0 +1,50 @@ +# 자물쇠의 중간 부분이 모두 1인지 확인 +def is_valid(new_lock): + length = len(new_lock) // 3 + + for r in range(length, length * 2): + for c in range(length, length * 2): + if new_lock[r][c] != 1: + return False + + return True + +def solution(key, lock): + n = len(lock) + k = len(key) + new_lock = [[0] * (n * 3) for _ in range(n * 3)] + + for r in range(n): + for c in range(n): + new_lock[r + n][c + n] = lock[r][c] + + for _ in range(4): + rev_key = key[::-1] + key = [] + for c in range(k): + row = [] + for r in range(k): + row.append(rev_key[r][c]) + key.append(row) + + """ + 열쇠를 돌리는 로직은 한 줄로도 구현가능 합니다 + key = [row for row in zip(*reversed(key))] + """ + + for r in range(n * 2): + for c in range(n * 2): + # 자물쇠에 열쇠를 끼운다 + for i in range(k): + for j in range(k): + new_lock[r + i][c + j] += key[i][j] + + # 자물쇠에 열쇠가 딱 들어갔는지 확인 + if is_valid(new_lock): + return True + + # 자물쇠에서 열쇠를 빼서 복구시킨다 + for i in range(k): + for j in range(k): + new_lock[r + i][c + j] -= key[i][j] + return False \ No newline at end of file diff --git a/pknujsp/README.md b/pknujsp/README.md index 571b70d7..489d3d88 100644 --- a/pknujsp/README.md +++ b/pknujsp/README.md @@ -38,4 +38,10 @@ | 34차시 | 2024.02.12 | BFS | [이분 그래프](https://www.acmicpc.net/problem/1707) | [#135](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/135) | | 35차시 | 2024.02.18 | 그리디 | [선물할인](https://www.acmicpc.net/problem/25947) | [#137](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137) | | 36차시 | 2024.02.21 | 이진탐색 | [휴게소 세우기](https://www.acmicpc.net/problem/1477) | [#143](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143) | -| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) | \ No newline at end of file +| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) | +| 38차시 | 2024.03.08 | BRUTE_FORCE | [자물쇠와 열쇠](https://school.programmers.co.kr/learn/courses/30/lessons/60059) | [#154](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/154) | +| 39차시 | 2024.03.19 | 큐 | [디스크 컨트롤러](https://school.programmers.co.kr/learn/courses/30/lessons/42627) | [#163](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/163) | +| 40차시 | 2024.03.22 | 그리디 | [센서](https://www.acmicpc.net/problem/2212) | [#168](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/168) | +| 41차시 | 2024.03.25 | 다익스트라 | [알고스팟](https://www.acmicpc.net/problem/1261) | [#169](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/169) | +| 42차시 | 2024.03.29 | BFS | [다리 만들기](https://www.acmicpc.net/problem/2146) | [#173](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/173) | +| 43차시 | 2024.04.03 | 그리디 | [저울](https://www.acmicpc.net/problem/2437) | [#175](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175) | \ No newline at end of file diff --git "a/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" "b/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" new file mode 100644 index 00000000..07494620 --- /dev/null +++ "b/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" @@ -0,0 +1,18 @@ +from sys import * + +N = int(stdin.readline()) +K = int(stdin.readline()) +SENSORS = sorted(set(map(int, stdin.readline().split()))) + +if K >= N: + print(0) + exit() + +distances = [SENSORS[i] - SENSORS[i - 1] for i in range(1, len(SENSORS))] +distances.sort(reverse=True) + +result = 0 +for i in range(K - 1, len(distances)): + result += distances[i] + +print(result) diff --git "a/pknujsp/\352\267\270\353\246\254\353\224\224/43-\354\240\200\354\232\270.py" "b/pknujsp/\352\267\270\353\246\254\353\224\224/43-\354\240\200\354\232\270.py" new file mode 100644 index 00000000..8000dc85 --- /dev/null +++ "b/pknujsp/\352\267\270\353\246\254\353\224\224/43-\354\240\200\354\232\270.py" @@ -0,0 +1,13 @@ +N = int(input()) +weights = list(map(int, input().split())) +weights.sort() + +x = 0 +for weight in weights: + # x + 1이 현재 추의 무게보다 작다면, 측정이 불가능함 + if x + 1 < weight: + break + + x += weight + +print(x + 1) \ No newline at end of file diff --git "a/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" "b/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" new file mode 100644 index 00000000..364af0a3 --- /dev/null +++ "b/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" @@ -0,0 +1,33 @@ +from heapq import * +from sys import * + +C, R = map(int, stdin.readline().split()) +arr = [list(map(int, list(stdin.readline().strip()))) for _ in range(R)] + +drc = ((1,0),(-1,0),(0,1),(0,-1)) +visited = [[False] * C for _ in range(R)] +heap = [(0, 0, 0)] +target_r = R - 1 +target_c = C - 1 + +while heap: + cost, r, c = heappop(heap) + + if r == target_r and c == target_c: + print(cost) + break + if visited[r][c]: + continue + + visited[r][c] = True + + for dr, dc in drc: + nr = r + dr + nc = c + dc + + if not 0 <= nr < R or not 0 <= nc < C: + continue + if visited[nr][nc]: + continue + + heappush(heap, (cost + arr[nr][nc], nr, nc)) diff --git "a/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" "b/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" new file mode 100644 index 00000000..0a0b4d8e --- /dev/null +++ "b/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" @@ -0,0 +1,24 @@ +from heapq import * +from collections import * + +def solution(jobs): + jobs = deque(sorted(jobs)) + jobs_num = len(jobs) + + curr_time = wait_time = 0 + heap = [] + + while heap or jobs: + while jobs and jobs[0][0] <= curr_time: + requested_time, duration = jobs.popleft() + heappush(heap, (duration, requested_time)) + + if heap: + duration, requested_time = heappop(heap) + + curr_time += duration + wait_time += curr_time - requested_time + else: + curr_time += 1 + + return wait_time // jobs_num \ No newline at end of file diff --git "a/tgyuuAn/BFS/\352\263\274\354\231\270\353\247\250.py" "b/tgyuuAn/BFS/\352\263\274\354\231\270\353\247\250.py" new file mode 100644 index 00000000..e69de29b diff --git "a/tgyuuAn/BFS/\354\227\264\354\207\240.py" "b/tgyuuAn/BFS/\354\227\264\354\207\240.py" new file mode 100644 index 00000000..36494506 --- /dev/null +++ "b/tgyuuAn/BFS/\354\227\264\354\207\240.py" @@ -0,0 +1,97 @@ +import sys +from collections import deque, defaultdict + +def input(): return sys.stdin.readline().rstrip() + +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] + +T = int(input()) + +for _ in range(T): + + H, W = map(int, input().split()) + building = [["." for _ in range(W+2)]] + door_info = defaultdict(set) + keys_i_have = set() + + for row in range(H): + _input = "." + _input += input() + _input += "." + + for col in range(W+2): + if _input[col] not in ("*", ".", "$") and _input[col].isupper(): + door_info[_input[col]].add((row+1, col)) + + building.append(list(_input)) + + building.append(["." for _ in range(W+2)]) + + keys_info = input() + if keys_info != "0": + keys_i_have.update(set(keys_info)) + + answer = 0 + visited = set() + locked_doors_to_access = set() + + deq = deque([(0, 0)]) + while deq: + now_row, now_col = deq.popleft() + + for dir in range(4): + new_row = now_row + dy[dir] + new_col = now_col + dx[dir] + + if new_row < 0 or new_row >= H+2: continue + if new_col < 0 or new_col >= W+2: continue + if (new_row, new_col) in visited: continue + if building[new_row][new_col] == "*": continue + + # print(now_row, now_col,building[new_row][new_col]) + # print(locked_doors_to_access) + # print(keys_i_have) + # print() + + if building[new_row][new_col] == "$": + answer += 1 + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + continue + + # 문을 만났을 경우, 이 때 까지 얻은 열쇠로 열 수 있는 지 확인함. 아닐 경우 접근할 수 있는 문 목록에 추가 + if building[new_row][new_col].isalpha() and building[new_row][new_col].isupper(): + + # 열쇠를 이미 가지고 있는 경우 + if building[new_row][new_col].lower() in keys_i_have: + building[new_row][new_col] = "." + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + + # 열쇠가 없어서 문을 못 열 경우 접근할 수 있는 문 목록에 추가 + else: locked_doors_to_access.add((new_row, new_col)) + + continue + + # 열쇠를 획득했을 경우, 이 때 까지 만난 문들 중에 열 수 있는 것들을 queue에 넣음 + if building[new_row][new_col].isalpha() and building[new_row][new_col].islower(): + keys_i_have.add(building[new_row][new_col]) + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + + for can_open_row, can_open_col in door_info[building[new_row][new_col].upper()]: + if (can_open_row, can_open_col) in locked_doors_to_access: + building[can_open_row][can_open_col] = "." + visited.add((can_open_row, can_open_col)) + deq.append((can_open_row, can_open_col)) + locked_doors_to_access.discard((can_open_row, can_open_col)) + + continue + + # 빈 공간일 경우, 그냥 지나감 + if building[new_row][new_col] == ".": + visited.add((new_row, new_col)) + deq.append((new_row, new_col)) + + print(answer) \ No newline at end of file diff --git a/tgyuuAn/DP/KCM Travel.py b/tgyuuAn/DP/KCM Travel.py new file mode 100644 index 00000000..4e279062 --- /dev/null +++ b/tgyuuAn/DP/KCM Travel.py @@ -0,0 +1,30 @@ +from collections import defaultdict, deque +import sys + +def input(): return sys.stdin.readline().rstrip() + +T = int(input()) +for _ in range(T): + N, M, K = map(int, input().split()) + + costs = [[int(1e9) for _ in range(M+1)] for _ in range(N+1)] + costs[1][0] = 0 + + graph = [[] for _ in range(N+1)] + for _ in range(K): + start, destination, cost, duration = map(int,input().split()) + graph[start].append((destination, cost, duration)) + + # print(edges) + + for cost in range(M+1): + for city in range(1, N): + if costs[city][cost] == int(1e9): continue + + for now_destination, now_cost, now_duration in graph[city]: + + if now_cost + cost <= M and costs[now_destination][cost + now_cost] > costs[city][cost] + now_duration: + costs[now_destination][cost + now_cost] = costs[city][cost] + now_duration + + result = min(costs[N]) + print(result) if result != int(1e9) else print("Poor KCM") \ No newline at end of file diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 5fd3dcb3..8b390814 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -29,22 +29,28 @@ | 25차시 | 2023.12.26 | 구현 | 방의 개수 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/90 | 26차시 | 2023.12.29 | BFS | 백조의 호수 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/95 | 27차시 | 2024.01.01 | BFS | 탈옥 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/96 -| 28차시 | 2023.01.04 | 스택 | 히스토그램에서 가장 큰 직사각형 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99 -| 29차시 | 2023.01.07 | 그리디 | 소트 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103 -| 30차시 | 2023.01.10 | BFS | 아이템 줍기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104 -| 31차시 | 2023.01.13 | DP | 진우의 달 여행 (Large) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105 -| 32차시 | 2023.01.16 | 그리디 | 멀티탭 스케줄링 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108 -| 33차시 | 2023.01.19 | 이분 탐색 | 배열에서 이동 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115 -| 34차시 | 2023.01.22 | 힙 | 가운데를 말해요 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116 -| 35차시 | 2023.01.25 | 이분 탐색 | 공유기 설치 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120 -| 36차시 | 2023.02.04 | BFS | 로봇 청소기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126 -| 37차시 | 2023.02.04 | BFS | 교환 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131 -| 38차시 | 2023.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138 -| 39차시 | 2023.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 -| 40차시 | 2023.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 -| 41차시 | 2023.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 -| 42차시 | 2023.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 +| 28차시 | 2024.01.04 | 스택 | 히스토그램에서 가장 큰 직사각형 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/99 +| 29차시 | 2024.01.07 | 그리디 | 소트 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/103 +| 30차시 | 2024.01.10 | BFS | 아이템 줍기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/104 +| 31차시 | 2024.01.13 | DP | 진우의 달 여행 (Large) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/105 +| 32차시 | 2024.01.16 | 그리디 | 멀티탭 스케줄링 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/108 +| 33차시 | 2024.01.19 | 이분 탐색 | 배열에서 이동 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/115 +| 34차시 | 2024.01.22 | 힙 | 가운데를 말해요 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/116 +| 35차시 | 2024.01.25 | 이분 탐색 | 공유기 설치 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/120 +| 36차시 | 2024.02.04 | BFS | 로봇 청소기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/126 +| 37차시 | 2024.02.04 | BFS | 교환 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/131 +| 38차시 | 2024.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/138 +| 39차시 | 2024.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 +| 40차시 | 2024.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 +| 41차시 | 2024.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 +| 42차시 | 2024.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 | 43차시 | 2024.03.10 | 이분 탐색 | 징검다리 건너기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157 | 44차시 | 2023.03.13 | 트라이 | 개미굴 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159 | 45차시 | 2023.03.16 | 트라이 | 트라이 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162 +| 46차시 | 2023.03.20 | 트라이 | AB | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/165 +| 47차시 | 2023.03.22 | 수학, 분할정복 | 너 봄에는 캡사이신이 맛있단다 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/167 +| 48차시 | 2023.03.25 | 벨만 포드 | 골목길 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/171 +| 49차시 | 2023.03.29 | DP | KCM Travel | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/174 +| 50차시 | 2024.04.01 | BFS | 열쇠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175 +| 51차시 | 2023.04.07 | BFS | 과외맨 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179 --- diff --git "a/tgyuuAn/\353\262\250\353\247\214 \355\217\254\353\223\234/\352\263\250\353\252\251\352\270\270.py" "b/tgyuuAn/\353\262\250\353\247\214 \355\217\254\353\223\234/\352\263\250\353\252\251\352\270\270.py" new file mode 100644 index 00000000..b879b593 --- /dev/null +++ "b/tgyuuAn/\353\262\250\353\247\214 \355\217\254\353\223\234/\352\263\250\353\252\251\352\270\270.py" @@ -0,0 +1,78 @@ +import sys +from collections import deque + +def input(): return sys.stdin.readline().rstrip() + +N, M = map(int, input().split()) +edge = [[] for _ in range(N+1)] + +# 간선 정보 받음 +for _ in range(M): + start, destination, cost = map(int,input().split()) + edge[start].append([destination, cost]) + +# 초기 세팅 +board = [-int(1e9) for _ in range(N+1)] +board[1] = 0 + +# 최적의 경로를 찾기 위해 역추적 하기 위해서 이전 노드를 기록 +prev_node = [-1 for _ in range(N+1)] +prev_node[1] = 0 + +for _ in range(N-1): + for start in range(1,N+1): + for destination, cost in edge[start]: + if board[destination] < board[start] + cost: + board[destination] = board[start] + cost + prev_node[destination] = start + +has_cycle = False +is_connect_target = False +for start in range(1,N+1): + for destination, cost in edge[start]: + # 사이클 발생 + if board[destination] < board[start] + cost: + has_cycle = True + + # 사이클이 발생해도 경로랑 관련이 없을 수도 있으므로, + # 사이클이 발생한 지점이 목표 지점과 관련이 있는지 체크크 + deq = deque([start]) + visited = {start,} + while deq: + now = deq.popleft() + + for d, c in edge[now]: + if d in visited: continue + + deq.append(d) + visited.add(d) + + # 사이클이 있고 목표지점 혹은 시작지점과 붙어있으면 -1 + if d == 1 or d == N: + is_connect_target = True + break + + if is_connect_target: break + break + +# 사이클이 있는데 해당 사이클이 목표와 연결되어 있을 경우 +if has_cycle and is_connect_target: print(-1) +else: + answer = [] + now = N + while now != 1: + answer.append(now) + now = prev_node[now] + + answer.append(now) + + if now != 1: print(-1) + else: print(*answer[::-1]) + +# 총 간선 = 2만개, +# 총 노드 = 100개 +# 벨만 포드 = ( 간선 X 노드 -1 ) -> 198만 시간 복잡도 가능. + +# 최적의 경로 +# 사이클이 발생해도 갈 수 있을 수 있음. +# 사이클이 없더라도 도달할 수 없을 수 있음. \ No newline at end of file diff --git "a/tgyuuAn/\354\210\230\355\225\231/\353\204\210 \353\264\204\354\227\220\353\212\224 \354\272\241\354\202\254\354\235\264\354\213\240\354\235\264 \353\247\233\354\236\210\353\213\250\353\213\244.py" "b/tgyuuAn/\354\210\230\355\225\231/\353\204\210 \353\264\204\354\227\220\353\212\224 \354\272\241\354\202\254\354\235\264\354\213\240\354\235\264 \353\247\233\354\236\210\353\213\250\353\213\244.py" new file mode 100644 index 00000000..ac903fbb --- /dev/null +++ "b/tgyuuAn/\354\210\230\355\225\231/\353\204\210 \353\264\204\354\227\220\353\212\224 \354\272\241\354\202\254\354\235\264\354\213\240\354\235\264 \353\247\233\354\236\210\353\213\250\353\213\244.py" @@ -0,0 +1,33 @@ +import sys + +DIV = 1_000_000_007 + +def input(): return sys.stdin.readline().rstrip() + +def power(n, over): + if over == 0: return 1 + elif over == 1: return n + elif over %2 == 0: + half = (power(n, over//2) % DIV) + return (half * half) % DIV + else: + half = (power(n, over//2) % DIV) + return (half * half * (n % DIV)) % DIV + +N = int(input()) +numbers = sorted(list(map(int,input().split()))) +DP = [-1 for _ in range(N)] +answer = 0 + +for start_idx in range(N): + start_num = numbers[start_idx] + end_num = numbers[N-start_idx-1] + + # 만약 캐싱이 되어있지 않을 경우 직접 계산 + if DP[N-start_idx-1] == -1: DP[N-start_idx-1] = power(2, N-start_idx-1) + + # 한번이라도 계산 했으면 바로 이용 + answer += ((end_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV + answer -= ((start_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV + +print(answer % DIV) \ No newline at end of file diff --git "a/tgyuuAn/\355\212\270\353\235\274\354\235\264/AB.py" "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/AB.py" new file mode 100644 index 00000000..6ae80188 --- /dev/null +++ "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/AB.py" @@ -0,0 +1,80 @@ +import sys +from collections import defaultdict + +class Node(): + def __init__(self, key = None): + self.key = key + self.count = 0 + self.children = {} + +class Tries(): + def __init__(self): + self.head = Node(None) + + def add(self, element): + now = self.head + + for char in element: + if char not in now.children: + now.children[char] = Node(char) + + now = now.children[char] + now.count += 1 + + def delete(self, element): + now = self.head + + for char in element: + child = now.children[char] + child.count -= 1 + if child.count == 0: del now.children[char] + now = child + + def find(self, element): + now = self.head + dic = defaultdict(int) + + string = "" + for char in element: + if char not in now.children: return dic + + string = string + char + now = now.children[char] + dic[string] = now.count + + return dic + +def input(): return sys.stdin.readline().rstrip() + +def query(method, target, element): + if method == "add": target.add(element) + + if method == "delete": target.delete(element) + + if method == "find": + n = len(element) + a_result = A.find(element) + b_result = B.find(element[::-1]) + + answer = 0 + for a_len in range(1,n): + answer += a_result[element[:a_len]] * b_result[element[:a_len-1:-1]] + + print(answer) + +N = int(input()) +A = Tries() +B = Tries() +method, target, element = "", "", "" + +for _ in range(N): + _input = input() + if _input[:4] == "find": + method,element = _input.split() + query(method, A, element) + + else: + method, target, element = _input.split() + + if target == "A": query(method, A, element) + else: query(method, B, element[::-1]) \ No newline at end of file