diff --git "a/H0ngJu/BFS/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.py" "b/H0ngJu/BFS/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.py" new file mode 100644 index 00000000..d2ab3dc4 --- /dev/null +++ "b/H0ngJu/BFS/\353\247\245\354\243\274 \353\247\210\354\213\234\353\251\264\354\204\234 \352\261\270\354\226\264\352\260\200\352\270\260.py" @@ -0,0 +1,45 @@ +import sys +from collections import deque + +def input() : return sys.stdin.readline().rstrip() + +t = int(input()) +dx = [-1, 1, 0, 0] +dy = [0, 0, -1, 1] + + +for i in range(t): + n = int(input()) + hx, hy = map(int, input().split()) + store = [list(map(int, input().split())) for _ in range(n)] + fx, fy = map(int, input().split()) + + x, y = hx, hy + result = "sad" + + q = deque([(x, y, 20)]) + visited = set([x, y]) + + while q: + x, y, beer = q.popleft() + + if abs(x-fx) + abs(y-fy) <= 50*beer: # 축제까지 갈 수 있는지 + result = "happy" + break + + for sx, sy in store: # 편의점 들릴 수 있는지 + if abs(x-sx) + abs(y-sy) <= 50 * beer and (sx, sy) not in visited: + visited.add((sx, sy)) + q.append((sx, sy, 20)) + + if beer > 0: # beer 먹고 가기 + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if (nx, ny) not in visited: + visited.add((nx, ny)) + q.append((nx, ny, beer - 1)) + + print(result) + \ No newline at end of file diff --git "a/H0ngJu/BFS/\354\225\204\352\270\260\354\203\201\354\226\264.py" "b/H0ngJu/BFS/\354\225\204\352\270\260\354\203\201\354\226\264.py" new file mode 100644 index 00000000..70ef0709 --- /dev/null +++ "b/H0ngJu/BFS/\354\225\204\352\270\260\354\203\201\354\226\264.py" @@ -0,0 +1,64 @@ +import sys +from collections import deque + +def input() : return sys.stdin.readline().rstrip() + +N = int(input()) +space = [list(map(int, input().split())) for _ in range(N)] + +dir = [(0,1), (0,-1), (1,0), (-1,0)] +x = 0 +y = 0 + +for i in range(N): + for j in range(N): + if space[i][j] == 9: + x, y = i, j + space[x][y] = 0 + +def check(a, b): + if 0<=a Int { - var maps = maps - let dx = [-1, 1, 0, 0] - let dy = [0, 0, -1, 1] - let rowCount = maps.count - let colCount = maps[0].count - - func bfs(_ x: Int, _ y: Int) -> Int { - var queue = [(x, y)] - var index = 0 - - while index < queue.count { - let (x, y) = queue[index] - index += 1 - - for i in 0..<4 { - let nx = x + dx[i] - let ny = y + dy[i] - - if nx < 0 || nx >= rowCount || ny < 0 || ny >= colCount { - continue - } - - if maps[nx][ny] == 0 { - continue - } - - if maps[nx][ny] == 1 { +from collections import deque +def solution(maps): + answer = 0 + + dx = [-1, 1, 0, 0] + dy = [0, 0, -1, 1] + + def bfs(x, y): + queue = deque() + queue.append((x, y)) + + while queue: + x, y = queue.popleft() + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if nx < 0 or nx >= len(maps) or ny < 0 or ny >= len(maps[0]): continue + + if maps[nx][ny] == 0: continue + + if maps[nx][ny] == 1: maps[nx][ny] = maps[x][y] + 1 queue.append((nx, ny)) - } - } - } - - return maps[rowCount - 1][colCount - 1] - } - - let answer = bfs(0, 0) - return answer == 1 ? -1 : answer -} - -// 예시 호출 -let maps = [ - [1, 0, 1, 1, 1], - [1, 0, 1, 0, 1], - [1, 1, 1, 0, 1], - [0, 0, 0, 0, 1] -] -print(solution(maps)) // 출력: 11 \ No newline at end of file + + return maps[len(maps)-1][len(maps[0])-1] + + answer = bfs(0, 0) + return -1 if answer == 1 else answer \ No newline at end of file diff --git a/alstjr7437/README.md b/alstjr7437/README.md index 2c0cbd2d..ecfd040c 100644 --- a/alstjr7437/README.md +++ b/alstjr7437/README.md @@ -32,4 +32,5 @@ | 28차시 | 2024.05.30 | 브루트 포스 | 카잉 달력 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/203 | | 29차시 | 2024.06.11 | 이분 탐색 | 나무 자르기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/210 | | 30차시 | 2024.06.19 | 방 번호 | 구현 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 | -| 31차시 | 2024.06.19 | 게임 맵 최단거리 | BFS | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/218 | \ No newline at end of file +| 31차시 | 2024.06.19 | 게임 맵 최단거리 | BFS | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/218 | +| 32차시 | 2024.08.15 | 과일 탕후루 | 투 포인터 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/230 | \ No newline at end of file diff --git "a/alstjr7437/\355\210\254\355\217\254\354\235\270\355\204\260/\352\263\274\354\235\274\355\203\225\355\233\204\353\243\250.py" "b/alstjr7437/\355\210\254\355\217\254\354\235\270\355\204\260/\352\263\274\354\235\274\355\203\225\355\233\204\353\243\250.py" new file mode 100644 index 00000000..4135cc44 --- /dev/null +++ "b/alstjr7437/\355\210\254\355\217\254\354\235\270\355\204\260/\352\263\274\354\235\274\355\203\225\355\233\204\353\243\250.py" @@ -0,0 +1,22 @@ +N = int(input()) +tanghuru = list(map(int, input().split())) +fruit = [0] * 10 +kind = 0 +start, end = 0, 0 + +while True: + if end == N: + print(end - start) + break + if fruit[tanghuru[end]] == 0: + kind += 1 + fruit[tanghuru[end]] += 1 + end += 1 + + if kind > 2: + fruit[tanghuru[start]] -= 1 + if fruit[tanghuru[start]] == 0: + kind -= 1 + start += 1 + + diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 7789f035..73034d56 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -74,4 +74,7 @@ | 65차시 | 2024.07.19 | 최소 공통 조상 | 가장 가까운 공통 조상 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/220 | 66차시 | 2024.07.22 | DP | 로봇 조종하기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/222 | 67차시 | 2024.07.26 | DP | 성냥개비 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/223 +| 68차시 | 2024.08.06 | 그리디 | 가희와 탑 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/226 +| 69차시 | 2024.08.10 | 누적합, 수학 | 1의 개수 세기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/228 +| 70차시 | 2024.08.16 | 스택 | 탑 보기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232 --- diff --git "a/tgyuuAn/\352\267\270\353\246\254\353\224\224/\352\260\200\355\235\254\354\235\230 \355\203\221.py" "b/tgyuuAn/\352\267\270\353\246\254\353\224\224/\352\260\200\355\235\254\354\235\230 \355\203\221.py" new file mode 100644 index 00000000..50cf76f1 --- /dev/null +++ "b/tgyuuAn/\352\267\270\353\246\254\353\224\224/\352\260\200\355\235\254\354\235\230 \355\203\221.py" @@ -0,0 +1,33 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +N, a, b = map(int, input().split()) +if(N+1<(a+b)): print(-1) +else: + answer = [0 for _ in range(N)] + + if a>b: + for idx, element in enumerate(range(N-a-b+1, N-b+1)): + answer[element] = idx+1 + + for idx, element in enumerate(range(N-1, N-b, -1)): + answer[element] = idx+1 + + for idx in range(N-a-b+1): + answer[idx] = 1 + + else: + for idx, element in enumerate(range(N-b, N)): + answer[element] = b - idx + + for idx, element in enumerate(range(N-b-a+1, N-b)): + answer[element] = idx+1 + + for idx in range(N-b-a+1): + answer[idx] = 1 + + if a == 1: + answer[N-b], answer[0] = answer[0], answer[N-b] + + print(*answer) \ No newline at end of file diff --git "a/tgyuuAn/\353\210\204\354\240\201\355\225\251/1\354\235\230 \352\260\234\354\210\230 \354\204\270\352\270\260.py" "b/tgyuuAn/\353\210\204\354\240\201\355\225\251/1\354\235\230 \352\260\234\354\210\230 \354\204\270\352\270\260.py" new file mode 100644 index 00000000..d9f7ee6d --- /dev/null +++ "b/tgyuuAn/\353\210\204\354\240\201\355\225\251/1\354\235\230 \352\260\234\354\210\230 \354\204\270\352\270\260.py" @@ -0,0 +1,22 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +A, B = map(int, input().split()) + +bits = [0 for _ in range(len(bin(B)[2:]))] + +for step in range(len(bits)): + one_count_per_bundle = (2**step) + total_count_bundle = 2 * (one_count_per_bundle) + + B_d, B_m = divmod(B,total_count_bundle) + B_count = one_count_per_bundle * B_d + if(B_m >= one_count_per_bundle): B_count += min(one_count_per_bundle,(B_m - one_count_per_bundle + 1)) + + A_d, A_m = divmod(A-1,total_count_bundle) + A_count = one_count_per_bundle * A_d + if(A_m >= one_count_per_bundle): A_count += min(one_count_per_bundle, (A_m - one_count_per_bundle + 1)) + bits[step] += (B_count - A_count) + +print(sum(bits)) \ No newline at end of file diff --git "a/tgyuuAn/\354\212\244\355\203\235/\355\203\221 \353\263\264\352\270\260.py" "b/tgyuuAn/\354\212\244\355\203\235/\355\203\221 \353\263\264\352\270\260.py" new file mode 100644 index 00000000..2042c070 --- /dev/null +++ "b/tgyuuAn/\354\212\244\355\203\235/\355\203\221 \353\263\264\352\270\260.py" @@ -0,0 +1,50 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +N = int(input()) +building = list(map(int,input().split())) +answer_count = [0 for _ in range(N)] +answer_idx = [int(1e9) for _ in range(N)] +stack = [] +for idx in range(N-1,-1,-1): + now_height = building[idx] + if len(stack) == 0: stack.append((now_height, idx)) + else: + while stack and now_height >= stack[-1][0]: + stack.pop() + + if len(stack) > 0: + if answer_idx[idx] != int(1e9) and abs(answer_idx[idx]-idx) < abs(stack[-1][1] - idx): + stack.append((now_height, idx)) + continue + + answer_idx[idx] = stack[-1][1] + answer_count[idx] += len(stack) + + stack.append((now_height, idx)) + +stack = [] +for idx in range(N): + now_height = building[idx] + if len(stack) == 0: stack.append((now_height, idx)) + else: + while stack and now_height >= stack[-1][0]: + stack.pop() + + if len(stack) > 0: + if answer_idx[idx] != int(1e9) and abs(answer_idx[idx]-idx) < abs(stack[-1][1] - idx): + answer_count[idx] += len(stack) + stack.append((now_height, idx)) + continue + + answer_idx[idx] = stack[-1][1] + answer_count[idx] += len(stack) + + if stack and stack[-1][0] == now_height: continue + + stack.append((now_height, idx)) + +for count, idx in zip(answer_count, answer_idx): + if idx == int(1e9): print(0) + else: print(count, idx+1) \ No newline at end of file