├── PART 1 └── Chapter 01 │ └── Problem 01 │ ├── 2.py │ ├── 4.swift │ ├── 3.kt │ └── 1.cpp ├── PART 3 ├── Problem 14 │ ├── 2.py │ └── 1.cpp ├── Problem 02 │ └── 1.py ├── Problem 11 │ ├── 1.py │ └── 2.py ├── Problem 13 │ ├── 2.py │ └── 1.cpp ├── Problem 19 │ └── 1.py ├── Problem 09 │ └── 1.py ├── Problem 05 │ └── 1.py ├── Problem 01 │ └── 1.py ├── Problem 08 │ ├── 2.py │ └── 1.cpp ├── Problem 06 │ ├── 3.py │ ├── 4.py │ ├── 1.cpp │ └── 2.cpp ├── Problem 03 │ ├── 2.py │ └── 1.cpp ├── Problem 10 │ └── 1.py ├── Problem 15 │ ├── 2.py │ └── 1.cpp ├── Problem 17 │ └── 1.py ├── Problem 07 │ ├── 2.py │ ├── 1.cpp │ └── 3.py ├── Problem 12 │ └── 1.py ├── Problem 16 │ └── 1.py ├── Problem 04 │ ├── 2.kt │ └── 1.py ├── Problem 20 │ └── 1.py └── Problem 18 │ └── 1.py ├── PART 2 ├── Chapter 05 │ ├── Problem 04 │ │ ├── 4.swift │ │ ├── 3.kt │ │ ├── 2.py │ │ └── 1.cpp │ ├── Problem 02 │ │ ├── 1.py │ │ ├── 2.py │ │ └── 3.swift │ ├── Problem 03 │ │ ├── 3.py │ │ ├── 2.py │ │ ├── 4.py │ │ ├── 5.kt │ │ └── 1.cpp │ ├── Problem 05 │ │ ├── 2.py │ │ ├── 3.kt │ │ └── 1.cpp │ └── Problem 01 │ │ └── 1.py ├── Chapter 02 │ ├── Problem 01 │ │ ├── 2.py │ │ ├── 3.py │ │ └── 1.cpp │ ├── Problem 05 │ │ └── 1.py │ ├── Problem 04 │ │ ├── 3.py │ │ ├── 2.py │ │ └── 1.cpp │ ├── Problem 02 │ │ ├── 1.py │ │ └── 2.kt │ ├── Problem 06 │ │ ├── 2.py │ │ ├── 1.cpp │ │ └── 3.kt │ ├── Problem 07 │ │ ├── 1.py │ │ └── 2.kt │ ├── Problem 08 │ │ ├── 2.py │ │ ├── 1.cpp │ │ └── 3.swift │ └── Problem 03 │ │ ├── 1.py │ │ ├── 2.py │ │ └── 3.kt ├── Chapter 01 │ ├── Problem 03 │ │ ├── 2.py │ │ └── 1.cpp │ ├── Problem 01 │ │ ├── 2.py │ │ └── 1.cpp │ ├── Problem 04 │ │ ├── 2.py │ │ ├── 3.py │ │ └── 1.cpp │ ├── Problem 06 │ │ └── 1.py │ ├── Problem 05 │ │ ├── 3.kt │ │ ├── 2.py │ │ └── 1.cpp │ └── Problem 02 │ │ └── 1.py ├── Practice 08 │ ├── 2.py │ └── 1.cpp ├── Practice 03 │ ├── 3.py │ ├── 1.cpp │ └── 2.cpp ├── Practice 09 │ └── 1.py ├── Practice 01 │ ├── 3.py │ ├── 2.py │ └── 1.cpp ├── Practice 02 │ └── 1.py ├── Practice 07 │ └── 1.py ├── Practice 06 │ ├── 1.py │ ├── 3.kt │ └── 2.py ├── Practice 04 │ └── 1.py └── Practice 05 │ ├── 1.py │ └── 2.kt ├── PART 4 ├── Chapter 02 │ ├── Problem 08 │ │ └── 1.py │ ├── Problem 03 │ │ └── 1.py │ ├── Problem 04 │ │ └── 1.py │ ├── Problem 06 │ │ ├── 2.py │ │ └── 1.cpp │ ├── Problem 01 │ │ └── 1.py │ ├── Problem 02 │ │ └── 1.py │ ├── Problem 05 │ │ └── 1.py │ ├── Problem 07 │ │ ├── 2.py │ │ └── 1.cpp │ └── Problem 09 │ │ └── 1.py └── Chapter 01 │ ├── Problem 03 │ ├── 2.py │ └── 1.cpp │ ├── Problem 02 │ ├── 2.py │ └── 1.cpp │ ├── Problem 04 │ └── 1.py │ ├── Problem 07 │ └── 1.py │ ├── Problem 05 │ └── 1.py │ ├── Problem 08 │ ├── 1.py │ └── 2.py │ ├── Problem 01 │ ├── 2.py │ └── 1.cpp │ └── Problem 06 │ └── 1.py ├── .gitignore └── README.md /PART 1/Chapter 01/Problem 01/2.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | print(a + b) -------------------------------------------------------------------------------- /PART 1/Chapter 01/Problem 01/4.swift: -------------------------------------------------------------------------------- 1 | let 입력 = readLine()!.split(separator: " ").map{Int($0)!} 2 | print(입력[0] + 입력[1]) -------------------------------------------------------------------------------- /PART 1/Chapter 01/Problem 01/3.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main(args: Array) = with(Scanner(System.`in`)) { 4 | println(nextInt() + nextInt()) 5 | } -------------------------------------------------------------------------------- /PART 3/Problem 14/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | A = sorted(map(int, input().split())) 3 | B = sorted(map(int, input().split()), reverse=True) 4 | print(sum(A[i] * B[i] for i in range(N))) 5 | -------------------------------------------------------------------------------- /PART 1/Chapter 01/Problem 01/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int A, B; 6 | 7 | int main() { 8 | scanf("%d %d", &A, &B); 9 | printf("%d\n", A + B); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /PART 3/Problem 02/1.py: -------------------------------------------------------------------------------- 1 | tc = 1 2 | while True: 3 | L, P, V = map(int, input().split()) 4 | if L == 0: 5 | break 6 | 7 | print(f'Case {tc}: {V // P * L + min(V % P, L)}') 8 | 9 | tc += 1 10 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 04/4.swift: -------------------------------------------------------------------------------- 1 | let n = Int(readLine()!)! 2 | var dp = [Int](repeating: 1, count: 2) 3 | if n > 1 { 4 | for i in 2...n { 5 | dp.append((dp[i - 1] + dp[i - 2]) % 10007) 6 | } 7 | } 8 | 9 | print(dp[n]) -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 01/2.py: -------------------------------------------------------------------------------- 1 | from itertools import combinations 2 | 3 | for i in combinations([int(input()) for _ in range(9)], 7): 4 | if sum(i) == 100: 5 | for j in sorted(i): 6 | print(j) 7 | 8 | break 9 | -------------------------------------------------------------------------------- /PART 3/Problem 11/1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | A = list(map(int, input().split())) 3 | B = sorted([(val, i) for i, val in enumerate(A)]) 4 | P = [-1] * N 5 | for i, pair in enumerate(B): 6 | P[pair[1]] = i 7 | 8 | print(" ".join(map(str, P))) 9 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 08/1.py: -------------------------------------------------------------------------------- 1 | def solution(s): 2 | numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] 3 | for i in range(10): 4 | s = s.replace(numbers[i], str(i)) 5 | 6 | return int(s) 7 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 03/2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dq = deque() 4 | for i in range(1, int(input()) + 1): 5 | dq.append(i) 6 | 7 | while len(dq) > 1: 8 | dq.popleft() 9 | dq.append(dq.popleft()) 10 | 11 | print(dq.pop()) 12 | -------------------------------------------------------------------------------- /PART 3/Problem 13/2.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | 3 | N = int(input()) 4 | A = list(map(int, input().split())) 5 | ans = -1 6 | for arr in set(permutations(A, N)): 7 | ans = max(ans, sum(abs(arr[i - 1] - arr[i]) for i in range(1, N))) 8 | 9 | print(ans) 10 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 01/2.py: -------------------------------------------------------------------------------- 1 | N, K = map(int, input().split()) 2 | peo = [i for i in range(1, N + 1)] 3 | pt = 0 4 | ans = [] 5 | for _ in range(N): 6 | pt += K - 1 7 | pt %= len(peo) 8 | ans.append(peo.pop(pt)) 9 | 10 | print(f"<{', '.join(map(str, ans))}>") 11 | -------------------------------------------------------------------------------- /PART 3/Problem 19/1.py: -------------------------------------------------------------------------------- 1 | ans = 0 2 | for _ in range(int(input())): 3 | stk = [] 4 | for ch in input(): 5 | if not stk or stk[-1] != ch: 6 | stk.append(ch) 7 | else: 8 | stk.pop() 9 | 10 | ans += len(stk) == 0 11 | 12 | print(ans) 13 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 04/2.py: -------------------------------------------------------------------------------- 1 | import sys, heapq 2 | 3 | input = sys.stdin.readline 4 | hq = [] 5 | for _ in range(int(input())): 6 | x = int(input()) 7 | if x == 0: 8 | print(heapq.heappop(hq)[1] if len(hq) else 0) 9 | else: 10 | heapq.heappush(hq, (abs(x), x)) -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 04/3.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main() = with(Scanner(System.`in`)) { 4 | val n = nextInt() 5 | val dp = IntArray(3) { it } 6 | for (i in 3..n) 7 | dp[i % 3] = (dp[(i - 1) % 3] + dp[(i - 2) % 3]) % 10007 8 | 9 | println(dp[n % 3]) 10 | } 11 | -------------------------------------------------------------------------------- /PART 2/Practice 08/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | dp = [i for i in range(N + 1)] 3 | for i in range(4, N + 1): 4 | for j in range(1, i): 5 | if i < j * j: 6 | break 7 | 8 | if dp[i] > dp[i - j * j] + 1: 9 | dp[i] = dp[i - j * j] + 1 10 | 11 | print(dp[N]) 12 | -------------------------------------------------------------------------------- /PART 2/Practice 03/3.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | hq = [] 4 | N = int(input()) 5 | for _ in range(N): 6 | for i in map(int, input().split()): 7 | if len(hq) >= N: 8 | heapq.heappushpop(hq, i) 9 | else: 10 | heapq.heappush(hq, i) 11 | 12 | print(heapq.heappop(hq)) 13 | -------------------------------------------------------------------------------- /PART 2/Practice 09/1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | A = list(map(int, input().split())) 3 | cache = [0] * N 4 | cache[0] = A[0] 5 | for i in range(1, N): 6 | for j in range(i): 7 | if A[j] < A[i]: 8 | cache[i] = max(cache[i], cache[j]) 9 | 10 | cache[i] += A[i] 11 | 12 | print(max(cache)) 13 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 06/1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | input = sys.stdin.readline 4 | s = set() 5 | for _ in range(int(input())): 6 | name, el = input().split() 7 | if el == 'enter': 8 | s.add(name) 9 | else: 10 | s.remove(name) 11 | 12 | for name in sorted(s, reverse=True): 13 | print(name) 14 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 05/1.py: -------------------------------------------------------------------------------- 1 | coord = [False] * 1001 2 | N, L = map(int, input().split()) 3 | for i in map(int, input().split()): 4 | coord[i] = True 5 | 6 | ans = 0 7 | x = 0 8 | while x <= 1000: 9 | if coord[x]: 10 | ans += 1 11 | x += L 12 | else: 13 | x += 1 14 | 15 | print(ans) 16 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 02/1.py: -------------------------------------------------------------------------------- 1 | from bisect import bisect_left, bisect_right 2 | 3 | N = int(input()) 4 | cards = sorted(map(int, input().split())) 5 | M = int(input()) 6 | ans = [] 7 | for i in map(int, input().split()): 8 | ans.append(bisect_right(cards, i) - bisect_left(cards, i)) 9 | 10 | print(' '.join(map(str, ans))) 11 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 04/2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | sys.setrecursionlimit(10 ** 6) 4 | n = int(input()) 5 | cache = [0] * (n + 1) 6 | 7 | 8 | def f(n): 9 | if cache[n]: 10 | return cache[n] 11 | 12 | cache[n] = n if n <= 2 else (f(n - 1) + f(n - 2)) % 10007 13 | 14 | return cache[n] 15 | 16 | 17 | print(f(n)) 18 | -------------------------------------------------------------------------------- /PART 3/Problem 09/1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | arr = list(map(int, input().split())) 3 | stk = [] # index 저장 4 | ans = [-1 for _ in range(N)] 5 | for i in range(N): 6 | while stk and arr[stk[-1]] < arr[i]: 7 | ans[stk[-1]] = arr[i] 8 | stk.pop(-1) 9 | 10 | stk.append(i) 11 | 12 | print(' '.join(map(str, ans))) 13 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 05/3.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main() = with(Scanner(System.`in`)) { 4 | val mp = emptyMap().toSortedMap() 5 | repeat(nextInt()) { 6 | val k = next() 7 | mp[k]?.let { mp.put(k, mp[k]!! + 1) } ?: mp.put(k, 1) 8 | } 9 | 10 | println(mp.maxByOrNull { it.value }!!.key) 11 | } -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 04/3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | input = sys.stdin.readline 4 | meetings = [tuple(map(int, input().split())) for _ in range(int(input()))] 5 | meetings.sort(key=lambda x: (x[1], x[0])) 6 | t = 0 7 | ans = 0 8 | for start, end in meetings: 9 | if t <= start: 10 | ans += 1 11 | t = end 12 | 13 | print(ans) 14 | -------------------------------------------------------------------------------- /PART 3/Problem 11/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | A = list(map(int, input().split())) 3 | B = sorted(A) 4 | chk = [False] * N 5 | P = [] 6 | for i in A: 7 | for j in range(B.index(i), N): 8 | if i == B[j] and not chk[j]: 9 | chk[j] = True 10 | P.append(j) 11 | break 12 | 13 | print(" ".join(map(str, P))) 14 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 02/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | cards = {} 3 | for i in map(int, input().split()): 4 | if i in cards: 5 | cards[i] += 1 6 | else: 7 | cards[i] = 1 8 | 9 | M = int(input()) 10 | ans = [] 11 | for i in map(int, input().split()): 12 | ans.append(cards[i] if i in cards else 0) 13 | 14 | print(' '.join(map(str, ans))) 15 | -------------------------------------------------------------------------------- /PART 3/Problem 05/1.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | arr = [list(map(int, input())) for _ in range(n)] 3 | ans = max(arr[0]) 4 | for i in range(1, n): 5 | for j in range(1, m): 6 | if arr[i][j] == 1: 7 | arr[i][j] = min(arr[i - 1][j - 1], arr[i - 1][j], arr[i][j - 1]) + 1 8 | 9 | ans = max(ans, max(arr[i])) 10 | 11 | print(ans ** 2) 12 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 05/2.py: -------------------------------------------------------------------------------- 1 | books = dict() 2 | for _ in range(int(input())): 3 | name = input() 4 | if name in books: 5 | books[name] += 1 6 | else: 7 | books[name] = 1 8 | 9 | max_val = max(books.values()) 10 | arr = [] 11 | for k, v in books.items(): 12 | if v == max_val: 13 | arr.append(k) 14 | 15 | arr.sort() 16 | print(arr[0]) 17 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 04/2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | input = sys.stdin.readline 4 | meetings = [] 5 | for _ in range(int(input())): 6 | start, end = map(int, input().split()) 7 | meetings.append((end, start)) 8 | 9 | meetings.sort() 10 | t = 0 11 | ans = 0 12 | for end, start in meetings: 13 | if t <= start: 14 | ans += 1 15 | t = end 16 | 17 | print(ans) 18 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 02/1.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | stk = [] 3 | ans = 'YES' 4 | for ch in input(): 5 | if ch == '(': 6 | stk.append(ch) 7 | else: 8 | if len(stk) > 0: 9 | stk.pop() 10 | else: 11 | ans = 'NO' 12 | 13 | if len(stk) > 0: 14 | ans = 'NO' 15 | 16 | print(ans) 17 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 01/3.py: -------------------------------------------------------------------------------- 1 | h = [int(input()) for _ in range(9)] 2 | tot = sum(h) 3 | 4 | 5 | def solve(): 6 | for i in range(8): 7 | for j in range(i + 1, 9): 8 | if tot - h[i] - h[j] == 100: 9 | for k in h: 10 | if k != h[i] and k != h[j]: 11 | print(k) 12 | 13 | return 14 | 15 | 16 | solve() 17 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 02/1.py: -------------------------------------------------------------------------------- 1 | T = [n * (n + 1) // 2 for n in range(46)] 2 | 3 | 4 | def is_possible(K): 5 | for i in range(1, 46): 6 | for j in range(i, 46): 7 | for k in range(j, 46): 8 | if T[i] + T[j] + T[k] == K: 9 | return 1 10 | 11 | return 0 12 | 13 | 14 | for _ in range(int(input())): 15 | print(is_possible(int(input()))) 16 | -------------------------------------------------------------------------------- /PART 2/Practice 03/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int N, arr[1500 * 1500]; 7 | 8 | int main() { 9 | scanf("%d", &N); 10 | for (int i = 0; i < N; ++i) 11 | for (int j = 0; j < N; ++j) 12 | scanf("%d", &arr[i * N + j]); 13 | 14 | sort(arr, arr + N * N); 15 | printf("%d\n", arr[N * N - N]); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 03/1.py: -------------------------------------------------------------------------------- 1 | def solution(N, stages): 2 | a = [0] * (N + 2) 3 | b = [0] * (N + 2) 4 | for stage in stages: 5 | a[stage] += 1 6 | b[stage] += 1 7 | 8 | for i in range(N, -1, -1): 9 | b[i] += b[i + 1] 10 | 11 | arr = [(a[i] / b[i] if b[i] else 0, i) for i in range(1, N + 1)] 12 | arr.sort(key=lambda x: (-x[0], x[1])) 13 | 14 | return [i[1] for i in arr] 15 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 03/3.py: -------------------------------------------------------------------------------- 1 | INF = 987654321 2 | N = int(input()) 3 | dp = [INF] * (N + 1) 4 | dp[1] = 0 5 | for i in range(2, N + 1): 6 | if i % 6 == 0: 7 | dp[i] = min(dp[i // 3], dp[i // 2]) + 1 8 | elif i % 3 == 0: 9 | dp[i] = min(dp[i // 3], dp[i - 1]) + 1 10 | elif i % 2 == 0: 11 | dp[i] = min(dp[i // 2], dp[i - 1]) + 1 12 | else: 13 | dp[i] = dp[i - 1] + 1 14 | 15 | print(dp[N]) 16 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 04/1.py: -------------------------------------------------------------------------------- 1 | from itertools import groupby 2 | 3 | 4 | def solution(s): 5 | ans = len(s) 6 | for n in range(1, len(s) // 2 + 1): 7 | res = '' 8 | for cutted, group in groupby([s[i:i + n] for i in range(0, len(s), n)]): 9 | dup_n = len(list(group)) 10 | res += str(dup_n) + cutted if dup_n >= 2 else cutted 11 | 12 | ans = min(ans, len(res)) 13 | 14 | return ans -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 03/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int N; 7 | queue q; 8 | 9 | int main() { 10 | scanf("%d", &N); 11 | 12 | for (int i = 1; i <= N; ++i) 13 | q.push(i); 14 | 15 | while (q.size() > 1) { 16 | q.pop(); 17 | q.push(q.front()); 18 | q.pop(); 19 | } 20 | 21 | printf("%d\n", q.front()); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 04/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MOD = 10007; 6 | 7 | int n, cache[1001] = {0, 1, 2}; 8 | 9 | int f(int i) { 10 | if (i < 3) return cache[i]; 11 | 12 | for (int j = 3; j <= i; ++j) 13 | cache[j] = (cache[j - 1] + cache[j - 2]) % MOD; 14 | 15 | return cache[i]; 16 | } 17 | 18 | int main() { 19 | scanf("%d", &n); 20 | 21 | printf("%d\n", f(n)); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 06/2.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | from itertools import combinations 3 | 4 | 5 | def solution(orders, course): 6 | ans = [] 7 | for n in course: 8 | menus = [] 9 | for order in orders: 10 | menus += combinations(sorted(order), n) 11 | 12 | res = Counter(menus).most_common() 13 | for menu, cnt in res: 14 | if cnt >= 2: 15 | ans.append(''.join(menu)) 16 | 17 | return sorted(ans) -------------------------------------------------------------------------------- /PART 3/Problem 01/1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | input = sys.stdin.readline 4 | N, P = map(int, input().split()) 5 | ans = 0 6 | stk = [[] for _ in range(7)] 7 | for _ in range(N): 8 | line, p = map(int, input().split()) 9 | while stk[line] and stk[line][-1] > p: 10 | stk[line].pop() 11 | ans += 1 12 | 13 | # 이미 해당 프렛을 누르고 있다면 continue 14 | if stk[line] and stk[line][-1] == p: 15 | continue 16 | 17 | stk[line].append(p) 18 | ans += 1 19 | 20 | print(ans) 21 | -------------------------------------------------------------------------------- /PART 3/Problem 08/2.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | alphabet_cnt = Counter(input()) 4 | if sum(cnt % 2 for cnt in alphabet_cnt.values()) > 1: 5 | print("I'm Sorry Hansoo") 6 | else: 7 | half = '' 8 | for ch, cnt in sorted(alphabet_cnt.items()): 9 | half += ch * (cnt // 2) 10 | 11 | ans = half 12 | for ch, cnt in alphabet_cnt.items(): 13 | if cnt % 2: 14 | ans += ch 15 | break 16 | 17 | ans += ''.join(reversed(half)) 18 | print(ans) 19 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 01/1.py: -------------------------------------------------------------------------------- 1 | def solution(dart_result): 2 | sdt = ' SDT' 3 | ans = [] 4 | idx = 0 5 | for i, c in enumerate(dart_result): 6 | if c in sdt: 7 | ans.append(int(dart_result[idx:i]) ** sdt.index(c)) 8 | elif c == '#': 9 | ans[-1] = -ans[-1] 10 | elif c == '*': 11 | ans[-1] <<= 1 12 | if len(ans) >= 2: 13 | ans[-2] <<= 1 14 | 15 | if not c.isdecimal(): 16 | idx = i + 1 17 | 18 | return sum(ans) -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 02/1.py: -------------------------------------------------------------------------------- 1 | def solution(record): 2 | nickname = dict() 3 | for log in record: 4 | words = log.split() 5 | if words[0] in ['Enter', 'Change']: 6 | nickname[words[1]] = words[2] 7 | 8 | ans = [] 9 | for log in record: 10 | words = log.split() 11 | if words[0] == 'Enter': 12 | ans.append(f'{nickname[words[1]]}님이 들어왔습니다.') 13 | elif words[0] == 'Leave': 14 | ans.append(f'{nickname[words[1]]}님이 나갔습니다.') 15 | 16 | return ans -------------------------------------------------------------------------------- /PART 2/Practice 08/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int ipt, dp[100005]; 8 | 9 | int main() { 10 | scanf("%d", &ipt); 11 | 12 | for (int i = 1; i <= ipt; i++) dp[i] = i; // 1^2 + 1^2 + 1^2 + ... 13 | 14 | int lmt = sqrt(ipt + 1); 15 | for (int i = 2; i <= lmt; i++) 16 | for (int idx = i * i; idx <= ipt; idx++) 17 | dp[idx] = min(dp[idx], dp[idx - i * i] + 1); 18 | 19 | 20 | printf("%d\n", dp[ipt]); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 05/2.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | n = int(input()) 3 | sticker = [list(map(int, input().split())) for _ in range(2)] 4 | dp = [[0] * n for _ in range(2)] 5 | for i in range(2): 6 | dp[i][0] = sticker[i][0] 7 | if n > 1: 8 | dp[i][1] = sticker[i ^ 1][0] + sticker[i][1] 9 | 10 | for j in range(2, n): 11 | for i in range(2): 12 | dp[i][j] = max(dp[i ^ 1][j - 2], dp[i ^ 1][j - 1]) + sticker[i][j] 13 | 14 | print(max(dp[0][n - 1], dp[1][n - 1])) 15 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 05/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int N; 9 | map mp; 10 | 11 | int main() { 12 | scanf("%d", &N); 13 | string book; 14 | for (int i = 0; i < N; ++i) { 15 | cin >> book; 16 | ++mp[book]; 17 | } 18 | 19 | pair maxBook; 20 | for (auto b : mp) 21 | if (maxBook.second < b.second) maxBook = b; 22 | 23 | cout << maxBook.first << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /PART 2/Practice 01/3.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | stk1 = [] # 커서 왼쪽 3 | stk2 = [] # 커서 오른쪽 4 | for ch in input(): 5 | if ch == '<': 6 | if len(stk1) > 0: 7 | stk2.append(stk1.pop()) 8 | elif ch == '>': 9 | if len(stk2) > 0: 10 | stk1.append(stk2.pop()) 11 | elif ch == '-': 12 | if len(stk1) > 0: 13 | stk1.pop() 14 | else: 15 | stk1.append(ch) 16 | 17 | print(''.join(stk1) + ''.join(reversed(stk2))) 18 | -------------------------------------------------------------------------------- /PART 3/Problem 06/3.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | M = int(input()) 3 | ipt = [] 4 | for _ in range(M // 10 + 1 if M % 10 else M // 10): 5 | ipt += map(int, input().split()) 6 | 7 | arr = [] 8 | ans = [] 9 | for i, v in enumerate(ipt): 10 | arr.append(v) 11 | if (i & 1) == 0: 12 | arr.sort() 13 | ans.append(arr[i // 2]) 14 | 15 | print(len(ans)) 16 | for i in range(len(ans) // 10 + 1 if len(ans) % 10 else len(ans) // 10): 17 | print(*ans[i * 10:(i + 1) * 10]) 18 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 01/1.py: -------------------------------------------------------------------------------- 1 | N, M = map(int, input().split()) 2 | tree = list(map(int, input().split())) 3 | lo = 0 4 | hi = max(tree) 5 | mid = lo + (hi - lo) // 2 6 | 7 | 8 | def get_total_tree(h): 9 | ret = 0 10 | for t in tree: 11 | if t > h: 12 | ret += t - h 13 | 14 | return ret 15 | 16 | 17 | ans = 0 18 | while lo <= hi: 19 | if get_total_tree(mid) >= M: 20 | ans = mid 21 | lo = mid + 1 22 | else: 23 | hi = mid - 1 24 | 25 | mid = lo + (hi - lo) // 2 26 | 27 | print(ans) 28 | -------------------------------------------------------------------------------- /PART 3/Problem 14/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int N, arrA[50], arrB[50]; 7 | 8 | int main() { 9 | scanf("%d", &N); 10 | 11 | for (int i = 0; i < N; i++) scanf("%d", &arrA[i]); 12 | for (int i = 0; i < N; i++) scanf("%d", &arrB[i]); 13 | 14 | sort(arrA, arrA + N); 15 | sort(arrB, arrB + N); 16 | reverse(arrB, arrB + N); 17 | 18 | int sum = 0; 19 | 20 | for (int i = 0; i < N; i++) sum += arrA[i] * arrB[i]; 21 | 22 | printf("%d\n", sum); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /PART 2/Practice 02/1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | val = [] 3 | stk = [] 4 | s = input() 5 | for _ in range(N): 6 | val.append(int(input())) 7 | 8 | for ch in s: 9 | if ch.isalpha(): 10 | stk.append(val[ord(ch) - ord('A')]) 11 | else: 12 | b = stk.pop() 13 | a = stk.pop() 14 | if ch == '+': 15 | stk.append(a + b) 16 | elif ch == '-': 17 | stk.append(a - b) 18 | elif ch == '*': 19 | stk.append(a * b) 20 | else: 21 | stk.append(a / b) 22 | 23 | print(f'{stk[0]:.2f}') 24 | -------------------------------------------------------------------------------- /PART 3/Problem 13/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int N, arr[8], ans; 8 | 9 | int main() { 10 | scanf("%d", &N); 11 | for (int i = 0; i < N; ++i) 12 | scanf("%d", &arr[i]); 13 | 14 | sort(arr, arr + N); 15 | do { 16 | int sum = 0; 17 | for (int i = 1; i < N; ++i) 18 | sum += abs(arr[i - 1] - arr[i]); 19 | 20 | ans = max(ans, sum); 21 | } while (next_permutation(arr, arr + N)); 22 | 23 | printf("%d\n", ans); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /PART 2/Practice 01/2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | for _ in range(int(input())): 4 | dq1 = deque() # 커서 왼쪽 5 | dq2 = deque() # 커서 오른쪽 6 | for ch in input(): 7 | if ch == '<': 8 | if len(dq1) > 0: 9 | dq2.appendleft(dq1.pop()) 10 | elif ch == '>': 11 | if len(dq2) > 0: 12 | dq1.append(dq2.popleft()) 13 | elif ch == '-': 14 | if len(dq1) > 0: 15 | dq1.pop() 16 | else: 17 | dq1.append(ch) 18 | 19 | print(''.join(dq1) + ''.join(dq2)) 20 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 01/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int n[9], nn[9] = {0, 0, 1, 1, 1, 1, 1, 1, 1}; 7 | 8 | int main() { 9 | for (int i = 0; i < 9; ++i) scanf("%d", &n[i]); 10 | do { 11 | int sum = 0; 12 | for (int i = 0; i < 9; ++i) sum += n[i] * nn[i]; 13 | 14 | if (sum == 100) { 15 | for (int i = 0; i < 9; ++i) 16 | if (nn[i]) printf("%d\n", n[i]); 17 | 18 | break; 19 | } 20 | } while (next_permutation(nn, nn + 9)); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 03/2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | sys.setrecursionlimit(10 ** 6) 4 | INF = 987654321 # 무한 5 | N = int(input()) 6 | cache = [INF] * (N + 1) 7 | cache[1] = 0 8 | 9 | 10 | def dp(x): 11 | if cache[x] != INF: 12 | return cache[x] 13 | 14 | if x % 6 == 0: 15 | cache[x] = min(dp(x // 3), dp(x // 2)) + 1 16 | elif x % 3 == 0: 17 | cache[x] = min(dp(x // 3), dp(x - 1)) + 1 18 | elif x % 2 == 0: 19 | cache[x] = min(dp(x // 2), dp(x - 1)) + 1 20 | else: 21 | cache[x] = dp(x - 1) + 1 22 | 23 | return cache[x] 24 | 25 | 26 | print(dp(N)) 27 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 03/4.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | N = int(input()) 4 | dq = deque() 5 | dq.append((N, 0)) 6 | chk = [False] * (N + 1) 7 | chk[N] = True 8 | 9 | while dq: 10 | X, d = dq.popleft() 11 | if X == 1: 12 | print(d) 13 | break 14 | 15 | if X % 3 == 0 and not chk[X // 3]: 16 | dq.append((X // 3, d + 1)) 17 | chk[X // 3] = True 18 | 19 | if X % 2 == 0 and not chk[X // 2]: 20 | dq.append((X // 2, d + 1)) 21 | chk[X // 2] = True 22 | 23 | if not chk[X - 1]: 24 | dq.append((X - 1, d + 1)) 25 | chk[X - 1] = True 26 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 03/5.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | import kotlin.math.min 3 | 4 | val INF = 987654321 5 | val cache = IntArray(1_000_001) { INF } 6 | 7 | fun main() = with(Scanner(System.`in`)) { 8 | cache[1] = 0 9 | val ipt = nextInt() 10 | for (i in 1..ipt) { 11 | if (i * 3 < 1_000_001) 12 | cache[i * 3] = min(cache[i * 3], cache[i] + 1) 13 | 14 | if (i * 2 < 1_000_001) 15 | cache[i * 2] = min(cache[i * 2], cache[i] + 1) 16 | 17 | if (i + 1 < 1_000_001) 18 | cache[i + 1] = min(cache[i + 1], cache[i] + 1) 19 | } 20 | 21 | println(cache[ipt]) 22 | } -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 04/3.py: -------------------------------------------------------------------------------- 1 | import sys, heapq 2 | 3 | input = sys.stdin.readline 4 | min_heap = [] # 양수 5 | max_heap = [] # 음수 6 | for _ in range(int(input())): 7 | x = int(input()) 8 | if x > 0: 9 | heapq.heappush(min_heap, x) 10 | elif x < 0: 11 | heapq.heappush(max_heap, -x) 12 | else: 13 | if len(min_heap): 14 | if len(max_heap) == 0 or min_heap[0] < max_heap[0]: 15 | print(heapq.heappop(min_heap)) 16 | else: 17 | print(-heapq.heappop(max_heap)) 18 | else: 19 | print(-heapq.heappop(max_heap) if len(max_heap) else 0) 20 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 03/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int N, cache[1000001]; 7 | 8 | // x를 1로 만드는 연산의 최소 횟수 9 | int f(int x) { 10 | if (x == 1) return 0; 11 | 12 | if (cache[x] != -1) return cache[x]; 13 | 14 | int &res = cache[x]; 15 | 16 | res = f(x - 1) + 1; 17 | if (x % 3 == 0) res = min(res, f(x / 3) + 1); 18 | if (x % 2 == 0) res = min(res, f(x / 2) + 1); 19 | 20 | return res; 21 | } 22 | 23 | int main() { 24 | fill(cache, cache + 1000001, -1); 25 | 26 | scanf("%d", &N); 27 | 28 | printf("%d\n", f(N)); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /PART 2/Practice 07/1.py: -------------------------------------------------------------------------------- 1 | N, M = map(int, input().split()) 2 | lessons = list(map(int, input().split())) 3 | l = max(lessons) 4 | r = sum(lessons) 5 | m = (l + r) // 2 6 | ans = r 7 | 8 | 9 | def is_possible(sz): 10 | cnt = 1 11 | bluray = 0 12 | for lesson in lessons: 13 | if bluray + lesson <= sz: 14 | bluray += lesson 15 | else: 16 | cnt += 1 17 | bluray = lesson 18 | 19 | return cnt <= M 20 | 21 | 22 | while l <= r: 23 | if is_possible(m): 24 | ans = m 25 | r = m - 1 26 | else: 27 | l = m + 1 28 | 29 | m = (l + r) // 2 30 | 31 | print(ans) 32 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 02/3.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | var dic = [Int: Int]() 4 | let _ = Int(readLine()!) 5 | for i in readLine()!.split(separator: " ").map({ Int(String($0))! }) { 6 | if dic[i] == nil { 7 | dic[i] = 1 8 | } else { 9 | dic[i]! += 1 10 | } 11 | } 12 | 13 | let M = Int(readLine()!)! 14 | var ans = [Int]() 15 | for i in readLine()!.split(separator: " ").map({ Int(String($0))! }) { 16 | if dic[i] == nil { 17 | ans.append(0) 18 | } else { 19 | ans.append(dic[i]!) 20 | } 21 | } 22 | 23 | print(ans[0], terminator: "") 24 | for i in 1.. 2 | #include 3 | 4 | using namespace std; 5 | 6 | int N; 7 | priority_queue, greater > pq; 8 | 9 | int main() { 10 | scanf("%d", &N); 11 | 12 | int ipt; 13 | for (int i = 0; i < N; ++i) { 14 | scanf("%d", &ipt); 15 | pq.emplace(ipt); 16 | } 17 | 18 | for (int i = 1; i < N; ++i) 19 | for (int j = 0; j < N; ++j) { 20 | scanf("%d", &ipt); 21 | if (ipt > pq.top()) { 22 | pq.pop(); 23 | pq.emplace(ipt); 24 | } 25 | } 26 | 27 | printf("%d\n", pq.top()); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 06/2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | sys.setrecursionlimit(10 ** 6) 4 | input = sys.stdin.readline 5 | N, M = map(int, input().split()) 6 | adj = [[False] * (N + 1) for _ in range(N + 1)] 7 | for _ in range(M): 8 | a, b = map(int, input().split()) 9 | adj[a][b] = True 10 | adj[b][a] = True 11 | 12 | ans = 0 13 | chk = [False] * (N + 1) 14 | 15 | 16 | def dfs(i): 17 | for j in range(1, N + 1): 18 | if adj[i][j] and not chk[j]: 19 | chk[j] = True 20 | dfs(j) 21 | 22 | 23 | for i in range(1, N + 1): 24 | if not chk[i]: 25 | ans += 1 26 | chk[i] = True 27 | dfs(i) 28 | 29 | print(ans) 30 | -------------------------------------------------------------------------------- /PART 3/Problem 03/2.py: -------------------------------------------------------------------------------- 1 | from itertools import combinations 2 | 3 | N, M = map(int, input().split()) 4 | houses = [] 5 | chickens = [] 6 | for i in range(N): 7 | for j, v in enumerate(map(int, input().split())): 8 | if v == 1: 9 | houses.append((i, j)) 10 | elif v == 2: 11 | chickens.append((i, j)) 12 | 13 | 14 | def get_dist(a, b): 15 | return abs(a[0] - b[0]) + abs(a[1] - b[1]) 16 | 17 | 18 | ans = 2 * N * len(houses) 19 | for combi in combinations(chickens, M): 20 | tot = 0 21 | for house in houses: 22 | tot += min(get_dist(house, chicken) for chicken in combi) 23 | 24 | ans = min(ans, tot) 25 | 26 | print(ans) 27 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 01/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int N, K; 8 | scanf("%d %d", &N, &K); 9 | 10 | vector peo; 11 | for (int i = 1; i < N + 1; ++i) 12 | peo.emplace_back(i); 13 | 14 | int pt = 0; 15 | vector ans; 16 | for (int i = 0; i < N; ++i) { 17 | pt += K - 1; 18 | pt %= peo.size(); 19 | ans.emplace_back(peo[pt]); 20 | peo.erase(peo.begin() + pt); 21 | } 22 | 23 | printf("<%d", ans[0]); 24 | 25 | for (int i = 1; i < N; ++i) 26 | printf(", %d", ans[i]); 27 | 28 | printf(">\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 02/2.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | val triNum = MutableList(0) { 0 } 4 | 5 | fun main() = with(Scanner(System.`in`)) { 6 | makeTriNum() 7 | repeat(nextInt()) { 8 | println(if (isPossible(nextInt())) 1 else 0) 9 | } 10 | } 11 | 12 | fun makeTriNum() { 13 | triNum.add(1) 14 | for (i in 2 until 45) 15 | triNum.add(triNum.last() + i) 16 | } 17 | 18 | fun isPossible(n: Int): Boolean { 19 | for (t1 in triNum.indices) 20 | for (t2 in t1 until triNum.lastIndex) 21 | for (t3 in t2 until triNum.lastIndex) 22 | if (triNum[t1] + triNum[t2] + triNum[t3] == n) 23 | return true 24 | 25 | return false 26 | } -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 06/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int N, M, ans; 6 | bool G[1001][1001], chk[1001]; 7 | 8 | void dfs(int now) { 9 | chk[now] = true; 10 | for (int next = 1; next <= N; ++next) 11 | if (G[now][next] && !chk[next]) 12 | dfs(next); 13 | } 14 | 15 | int main() { 16 | scanf("%d %d", &N, &M); 17 | 18 | int u, v; 19 | for (int e = 0; e < M; ++e) { 20 | scanf("%d %d", &u, &v); 21 | G[u][v] = G[v][u]= true; 22 | } 23 | 24 | for (int i = 1; i <= N; ++i) 25 | if (!chk[i]) { 26 | ++ans; 27 | dfs(i); 28 | } 29 | 30 | printf("%d\n", ans); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /PART 3/Problem 10/1.py: -------------------------------------------------------------------------------- 1 | N, M, K = map(int, input().split()) 2 | dp = [[0] * (M + 1) for _ in range(N + 1)] 3 | for j in range(1, M + 1): 4 | dp[0][j] = 1 5 | 6 | for i in range(1, N + 1): 7 | dp[i][0] = 1 8 | for j in range(1, M + 1): 9 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 10 | 11 | 12 | def recur(n, m, k): 13 | if n == 0: 14 | print('z' * m) 15 | return 16 | elif m == 0: 17 | print('a' * n) 18 | return 19 | 20 | if dp[n - 1][m] >= k: 21 | print('a', end='') 22 | recur(n - 1, m, k) 23 | else: 24 | print('z', end='') 25 | recur(n, m - 1, k - dp[n - 1][m]) 26 | 27 | 28 | if dp[N][M] < K: 29 | print(-1) 30 | else: 31 | recur(N, M, K) 32 | -------------------------------------------------------------------------------- /PART 2/Practice 06/1.py: -------------------------------------------------------------------------------- 1 | # BOJ에서 PyPy3만 통과하며 Python3는 시간초과 발생 2 | 3 | N = int(input()) 4 | ans = 0 5 | col = [False] * N # i번째 열에 퀸을 뒀다 6 | d1 = [False] * 2 * N # \ 대각선, 우상단부터 0 7 | d2 = [False] * 2 * N # / 대각선, 좌상단부터 0 8 | 9 | 10 | # Backtracking 11 | def bt(row): 12 | global ans 13 | if row == N: 14 | ans += 1 15 | return 16 | 17 | for j in range(N): 18 | if not col[j] and not d1[row - j] and not d2[row + j]: 19 | col[j] = True 20 | d1[row - j] = True 21 | d2[row + j] = True 22 | 23 | bt(row + 1) 24 | 25 | # 원상복구 26 | d2[row + j] = False 27 | d1[row - j] = False 28 | col[j] = False 29 | 30 | 31 | bt(0) 32 | print(ans) 33 | -------------------------------------------------------------------------------- /PART 3/Problem 06/4.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | for _ in range(int(input())): 4 | M = int(input()) 5 | arr = [] 6 | for _ in range(M // 10 + 1): 7 | for i in map(int, input().split()): 8 | arr.append(i) 9 | 10 | ans = [] 11 | max_hq = [] 12 | min_hq = [] 13 | for i, val in enumerate(arr): 14 | if i % 2: 15 | heapq.heappush(min_hq, -heapq.heappushpop(max_hq, -val) if len(max_hq) and val < -max_hq[0] else val) 16 | else: 17 | heapq.heappush(max_hq, -heapq.heappushpop(min_hq, val) if len(min_hq) and val > min_hq[0] else -val) 18 | ans.append(-max_hq[0]) 19 | 20 | print((M + 1) // 2) 21 | for i in range(len(ans) // 10 + 1): 22 | print(*ans[i * 10:(i + 1) * 10]) 23 | -------------------------------------------------------------------------------- /PART 3/Problem 06/1.cpp: -------------------------------------------------------------------------------- 1 | // 홀수번째마다 정렬해서 중앙값 구하기 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int M, arr[9999], ans[5000]; 9 | 10 | int main() { 11 | int tcN; 12 | scanf("%d", &tcN); 13 | for (int tc = 0; tc < tcN; ++tc) { 14 | scanf("%d", &M); 15 | for (int i = 0; i < M; ++i) { 16 | scanf("%d", &arr[i]); 17 | if ((i & 1) == 0) { 18 | sort(arr, arr + i + 1); 19 | ans[i / 2] = arr[i / 2]; 20 | } 21 | } 22 | 23 | int sz = (M + 1) / 2; 24 | printf("%d", sz); 25 | for (int i = 0; i < sz; ++i) 26 | printf("%s%d", i % 10 ? " " : "\n", ans[i]); 27 | 28 | printf("\n"); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 07/1.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dy = (0, 1, 0, -1) 4 | dx = (1, 0, -1, 0) 5 | 6 | N, M = map(int, input().split()) 7 | board = [input() for _ in range(N)] 8 | chk = [[False] * M for _ in range(N)] 9 | dq = deque() 10 | dq.append((0, 0, 1)) 11 | chk[0][0] = True 12 | 13 | 14 | def is_valid_coord(y, x): 15 | return 0 <= y < N and 0 <= x < M 16 | 17 | 18 | while len(dq) > 0: 19 | y, x, d = dq.popleft() 20 | 21 | if y == N - 1 and x == M - 1: 22 | print(d) 23 | break 24 | 25 | for k in range(4): 26 | ny = y + dy[k] 27 | nx = x + dx[k] 28 | nd = d + 1 29 | if is_valid_coord(ny, nx) and board[ny][nx] == '1' and not chk[ny][nx]: 30 | chk[ny][nx] = True 31 | dq.append((ny, nx, nd)) 32 | -------------------------------------------------------------------------------- /PART 3/Problem 15/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | dice = list(map(int, input().split())) 3 | 4 | 5 | def get_min3(): 6 | res = 150 7 | for i in range(4): 8 | for j in range(i + 1, 5): 9 | if i + j != 5: 10 | for k in range(j + 1, 6): 11 | if j + k != 5 and k + i != 5: 12 | res = min(res, dice[i] + dice[j] + dice[k]) 13 | 14 | return res 15 | 16 | 17 | def get_min2(): 18 | res = 100 19 | for i in range(5): 20 | for j in range(i + 1, 6): 21 | if i + j != 5: 22 | res = min(res, dice[i] + dice[j]) 23 | 24 | return res 25 | 26 | 27 | if N == 1: 28 | print(sum(dice) - max(dice)) 29 | else: 30 | print(4 * get_min3() + 4 * (2 * N - 3) * get_min2() + (N - 2) * (5 * N - 6) * min(dice)) 31 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 05/1.py: -------------------------------------------------------------------------------- 1 | def is_valid(p): 2 | stk = [] 3 | for ch in p: 4 | if ch == '(': 5 | stk.append(ch) 6 | else: 7 | if stk: 8 | stk.pop() 9 | else: 10 | return False 11 | 12 | return len(stk) == 0 13 | 14 | 15 | def solution(p): 16 | if p == '': 17 | return p 18 | 19 | cnt = 0 20 | u = '' 21 | v = '' 22 | for i, ch in enumerate(p): 23 | cnt += 1 if ch == '(' else -1 24 | if cnt == 0: 25 | u = p[:i + 1] 26 | v = p[i + 1:] 27 | break 28 | 29 | if is_valid(u): 30 | return u + solution(v) 31 | 32 | res = '(' + solution(v) + ')' 33 | for ch in u[1:-1]: 34 | res += ')' if ch == '(' else '(' 35 | 36 | return res 37 | -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 05/3.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | import kotlin.math.max 3 | 4 | fun main() = with(Scanner(System.`in`)) { 5 | repeat(nextInt()) { 6 | val N = nextInt() 7 | val sticker = Array(2) { IntArray(N) { nextInt() } } 8 | val dp = Array(2) { IntArray(N) { 0 } } 9 | for (i in 0 until 2) 10 | dp[i][0] = sticker[i][0] 11 | 12 | if (N > 1) { 13 | for (i in 0 until 2) 14 | dp[i][1] = dp[i xor 1][0] + sticker[i][1] 15 | 16 | if (N > 2) { 17 | for (j in 2 until N) 18 | for (i in 0 until 2) 19 | dp[i][j] = max(dp[i xor 1][j - 2], dp[i xor 1][j - 1]) + sticker[i][j] 20 | } 21 | } 22 | 23 | println(max(dp[0][N - 1], dp[1][N - 1])) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 08/2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dy = (0, 1, 0, -1) 4 | dx = (1, 0, -1, 0) 5 | 6 | R, C = map(int, input().split()) 7 | board = [input() for _ in range(R)] 8 | chk = [[set() for _ in range(C)] for _ in range(R)] 9 | ans = 0 10 | 11 | 12 | def is_valid_coord(y, x): 13 | return 0 <= y < R and 0 <= x < C 14 | 15 | 16 | dq = deque() 17 | chk[0][0].add(board[0][0]) 18 | dq.append((0, 0, board[0][0])) 19 | while dq: 20 | y, x, s = dq.popleft() 21 | ans = max(ans, len(s)) 22 | 23 | for k in range(4): 24 | ny = y + dy[k] 25 | nx = x + dx[k] 26 | if is_valid_coord(ny, nx) and board[ny][nx] not in s: 27 | ns = s + board[ny][nx] 28 | if ns not in chk[ny][nx]: 29 | chk[ny][nx].add(ns) 30 | dq.append((ny, nx, ns)) 31 | 32 | print(ans) 33 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 04/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Meeting { 8 | public: 9 | Meeting(int l, int r) : l(l), r(r) {}; 10 | 11 | bool operator < (const Meeting &m) const { 12 | if (r != m.r) return r < m.r; 13 | 14 | return l < m.l; 15 | } 16 | 17 | int l, r; 18 | }; 19 | 20 | int N, t, ans; 21 | vector v; 22 | 23 | int main() { 24 | scanf("%d", &N); 25 | 26 | int a, b; 27 | for (int i = 0; i < N; ++i) { 28 | scanf("%d %d", &a, &b); 29 | v.emplace_back(a, b); 30 | } 31 | 32 | sort(v.begin(), v.end()); 33 | 34 | for (int i = 0; i < N; ++i) 35 | if (t <= v[i].l) { 36 | ++ans; 37 | t = v[i].r; 38 | } 39 | 40 | printf("%d\n", ans); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /PART 3/Problem 17/1.py: -------------------------------------------------------------------------------- 1 | N, K = map(int, input().split()) 2 | schedule = list(map(int, input().split())) 3 | ans = 0 4 | multi_tap = [] 5 | for i, v in enumerate(schedule): 6 | if v in multi_tap: 7 | continue 8 | elif len(multi_tap) < N: 9 | multi_tap.append(v) 10 | else: 11 | ans += 1 12 | is_found = False 13 | nxt = (-1, 0) 14 | for j, val in enumerate(multi_tap): 15 | if val in schedule[i + 1:]: # 가장 나중에 사용되는 전자기기를 찾아 갱신 16 | nxt_idx = schedule[i + 1:].index(val) 17 | if nxt[0] < nxt_idx: 18 | nxt = (nxt_idx, j) 19 | else: # 이후 사용되지 않는 전자기기가 있다면 그걸로 선택 20 | is_found = True 21 | multi_tap[j] = v 22 | break 23 | 24 | if not is_found: 25 | multi_tap[nxt[1]] = v 26 | 27 | print(ans) 28 | -------------------------------------------------------------------------------- /PART 3/Problem 07/2.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | 3 | N = int(input()) 4 | nums = list(map(int, input().split())) 5 | ops_cnt = [] 6 | for i, cnt in enumerate(map(int, input().split())): 7 | ops_cnt += [i] * cnt 8 | 9 | ans_max = -1_000_000_001 10 | ans_min = 1_000_000_001 11 | for permu in set(permutations(ops_cnt, N - 1)): 12 | res = nums[0] 13 | for i, op in enumerate(permu): 14 | if op == 0: 15 | res += nums[i + 1] 16 | elif op == 1: 17 | res -= nums[i + 1] 18 | elif op == 2: 19 | res *= nums[i + 1] 20 | else: 21 | if res >= 0: 22 | res //= nums[i + 1] 23 | else: 24 | res = -(-res // nums[i + 1]) 25 | 26 | if res > ans_max: 27 | ans_max = res 28 | 29 | if res < ans_min: 30 | ans_min = res 31 | 32 | print(ans_max) 33 | print(ans_min) 34 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 07/2.py: -------------------------------------------------------------------------------- 1 | from bisect import bisect_left 2 | 3 | 4 | def solution(infos, queries): 5 | db = {} 6 | for info in infos: 7 | s = info.split() 8 | for i in [s[0], '-']: 9 | for j in [s[1], '-']: 10 | for k in [s[2], '-']: 11 | for l in [s[3], '-']: 12 | key = (i, j, k, l) 13 | val = int(s[4]) 14 | if key in db: 15 | db[key].append(val) 16 | else: 17 | db[key] = [val] 18 | 19 | for key in db.keys(): 20 | db[key].sort() 21 | 22 | ans = [] 23 | for query in queries: 24 | q = query.split() 25 | key = (q[0], q[2], q[4], q[6]) 26 | ans.append(len(db[key]) - bisect_left(db[key], int(q[7])) if key in db else 0) 27 | 28 | return ans -------------------------------------------------------------------------------- /PART 3/Problem 12/1.py: -------------------------------------------------------------------------------- 1 | INF = 9876543210 2 | cache = [INF] * 10_000_001 # 초기값: INF, 임시마크: 0 3 | A, B, K = map(int, input().split()) 4 | 5 | 6 | def S(n): 7 | return sum(int(ch) ** K for ch in str(n)) 8 | 9 | 10 | def dfs(n): 11 | if cache[n] == INF: # 최초 방문 노드 12 | cache[n] = 0 # 임시마크 해두고 탐색 시작 13 | cache[n] = min(n, dfs(S(n))) 14 | else: 15 | # 이미 탐색 완료한 노드라면 바로 반환 16 | if cache[n]: 17 | return cache[n] 18 | 19 | # 탐색 중인 노드 재방문. 즉, cycle 발견. 다시 cycle 돌면서 최솟값을 찾는다 20 | m = n 21 | nn = S(n) 22 | while nn != n: 23 | m = min(m, nn) 24 | nn = S(nn) 25 | 26 | # cycle 노드들에 최솟값을 갱신해준다 27 | cache[n] = m 28 | nn = S(n) 29 | while nn != n: 30 | cache[nn] = m 31 | nn = S(nn) 32 | 33 | return cache[n] 34 | 35 | 36 | print(sum(dfs(i) for i in range(A, B + 1))) 37 | -------------------------------------------------------------------------------- /PART 3/Problem 08/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | string s; 8 | int cnt[27]; 9 | int len; 10 | 11 | int main() { 12 | cin >> s; 13 | len = s.length(); 14 | for (int i = 0; i < len; i++) cnt[s[i] - 'A']++; 15 | 16 | int oddN = 0; 17 | for (int i = 0; i < 26; i++) 18 | if (cnt[i] % 2) oddN++; 19 | 20 | if (oddN > 1) printf("I'm Sorry Hansoo\n"); 21 | else { 22 | string tmp; 23 | for (int i = 0; i < 26; i++) 24 | for (int j = 0; j < cnt[i] / 2; j++) tmp += 'A' + i; 25 | 26 | string ans = tmp; 27 | for (int i = 0; i < 26; i++) 28 | if (cnt[i] % 2) { 29 | ans += 'A' + i; 30 | break; 31 | } 32 | 33 | reverse(tmp.begin(), tmp.end()); 34 | ans += tmp; 35 | 36 | cout << ans << endl; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /PART 2/Chapter 05/Problem 05/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int n, arr[2][100001], c[2][100001]; 7 | 8 | void dp() { 9 | c[0][0] = arr[0][0]; 10 | c[1][0] = arr[1][0]; 11 | 12 | if (n > 1) { 13 | for (int i = 0; i < 2; ++i) 14 | c[i][1] = c[(i + 1) % 2][0] + arr[i][1]; 15 | 16 | for (int j = 2; j < n; ++j) 17 | for (int i = 0; i < 2; ++i) 18 | c[i][j] = max(c[(i + 1) % 2][j - 1], c[(i + 1) % 2][j - 2]) + arr[i][j]; 19 | } 20 | } 21 | 22 | int main() { 23 | int tcN; 24 | scanf("%d", &tcN); 25 | for (int tc = 0; tc < tcN; ++tc) { 26 | scanf("%d", &n); 27 | for (int i = 0; i < 2; ++i) 28 | for (int j = 0; j < n; ++j) 29 | scanf("%d", &arr[i][j]); 30 | 31 | dp(); 32 | 33 | printf("%d\n", max(c[0][n - 1], c[1][n - 1])); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /PART 3/Problem 16/1.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | N = len(s) if len(s) <= 9 else (len(s) - 9) // 2 + 9 3 | chk = [False] * (N + 1) 4 | is_found_ans = False 5 | 6 | 7 | def backtracking(idx, arr): 8 | global is_found_ans 9 | if idx >= len(s): 10 | is_found_ans = True 11 | print(*arr) 12 | return 13 | 14 | n = int(s[idx]) 15 | if n <= N and not chk[n]: 16 | chk[n] = True 17 | narr = arr[:] 18 | narr.append(n) 19 | backtracking(idx + 1, narr) 20 | if is_found_ans: 21 | return 22 | 23 | chk[n] = False 24 | 25 | if idx + 1 < len(s): 26 | n = int(s[idx:idx + 2]) 27 | if n <= N and not chk[n]: 28 | chk[n] = True 29 | narr = arr[:] 30 | narr.append(n) 31 | backtracking(idx + 2, narr) 32 | if is_found_ans: 33 | return 34 | 35 | chk[n] = False 36 | 37 | 38 | backtracking(0, []) 39 | -------------------------------------------------------------------------------- /PART 2/Practice 01/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | string lg; // log 9 | list pw; 10 | list::iterator cur; // 커서 11 | 12 | int main() { 13 | int tcN; 14 | scanf("%d", &tcN); 15 | for (int tc = 0; tc < tcN; ++tc) { 16 | pw.clear(); 17 | cur = pw.begin(); 18 | cin >> lg; 19 | for (char ch : lg) 20 | if (ch == '<') { 21 | if (cur != pw.begin()) --cur; 22 | } else if (ch == '>') { 23 | if (cur != pw.end()) ++cur; 24 | } else if (ch == '-') { 25 | if (cur != pw.begin()) { 26 | auto bef = cur; 27 | --bef; 28 | pw.erase(bef); 29 | } 30 | } else pw.insert(cur, ch); 31 | 32 | for (char ch : pw) printf("%c", ch); 33 | printf("\n"); 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /PART 2/Practice 04/1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(10 ** 6) 3 | 4 | dy = (0, 1, 0, -1) 5 | dx = (1, 0, -1, 0) 6 | 7 | N, M, K = map(int, input().split()) 8 | board = [['.'] * M for _ in range(N)] 9 | for _ in range(K): 10 | y, x = map(int, input().split()) 11 | board[y - 1][x - 1] = '#' 12 | 13 | visited = [[False] * M for _ in range(N)] 14 | sz = 0 15 | ans = 0 16 | 17 | 18 | def isValidCoord(y, x): 19 | return 0 <= y < N and 0 <= x < M 20 | 21 | 22 | def dfs(y, x): 23 | global ans, sz 24 | 25 | visited[y][x] = True 26 | sz += 1 27 | ans = max(ans, sz) 28 | 29 | for k in range(4): 30 | ny = y + dy[k] 31 | nx = x + dx[k] 32 | if isValidCoord(ny, nx) and not visited[ny][nx] and board[ny][nx] == '#': 33 | dfs(ny, nx) 34 | 35 | 36 | for i in range(N): 37 | for j in range(M): 38 | if not visited[i][j] and board[i][j] == '#': 39 | sz = 0 40 | dfs(i, j) 41 | 42 | print(ans) 43 | -------------------------------------------------------------------------------- /PART 2/Practice 05/1.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | import sys 3 | 4 | input = sys.stdin.readline 5 | dy = (-1, -2, -2, -1, 1, 2, 2, 1) 6 | dx = (-2, -1, 1, 2, 2, 1, -1, -2) 7 | l = 0 8 | 9 | 10 | def is_valid_coord(y, x): 11 | return 0 <= y < l and 0 <= x < l 12 | 13 | 14 | for _ in range(int(input())): 15 | l = int(input()) 16 | dq = deque() 17 | visited = [[False] * l for _ in range(l)] 18 | sy, sx = map(int, input().split()) 19 | gy, gx = map(int, input().split()) 20 | 21 | visited[sy][sx] = True 22 | dq.append((sy, sx, 0)) 23 | while dq: 24 | y, x, d = dq.popleft() 25 | if y == gy and x == gx: 26 | print(d) 27 | break 28 | 29 | for k in range(8): 30 | ny = y + dy[k] 31 | nx = x + dx[k] 32 | nd = d + 1 33 | if is_valid_coord(ny, nx) and not visited[ny][nx]: 34 | visited[ny][nx] = True 35 | dq.append((ny, nx, nd)) 36 | -------------------------------------------------------------------------------- /PART 3/Problem 07/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int N, arr[11], minAns = 1e9, maxAns = -1e9; 8 | vector v; 9 | 10 | int main() { 11 | scanf("%d", &N); 12 | for (int i = 0; i < N; ++i) scanf("%d", &arr[i]); 13 | 14 | int op; 15 | for (int i = 0; i < 4; ++i) { 16 | scanf("%d", &op); 17 | for (int j = 0; j < op; ++j) v.emplace_back(i); 18 | } 19 | 20 | do { 21 | int res = arr[0]; 22 | for (int i = 0; i < N - 1; ++i) 23 | if (v[i] == 0) res += arr[i + 1]; 24 | else if (v[i] == 1) res -= arr[i + 1]; 25 | else if (v[i] == 2) res *= arr[i + 1]; 26 | else if (v[i] == 3) res /= arr[i + 1]; 27 | 28 | maxAns = max(maxAns, res); 29 | minAns = min(minAns, res); 30 | } while (next_permutation(v.begin(), v.end())); 31 | 32 | printf("%d\n%d\n", maxAns, minAns); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /PART 3/Problem 07/3.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | N = int(input()) 4 | nums = list(map(int, input().split())) 5 | ops_cnt = list(map(int, input().split())) 6 | ans_max = -1_000_000_001 7 | ans_min = 1_000_000_001 8 | dq = deque() 9 | dq.append((nums[0], 1, ops_cnt[0], ops_cnt[1], ops_cnt[2], ops_cnt[3])) 10 | while dq: 11 | res, n, plus, minus, multi, div = dq.popleft() 12 | if n == N: 13 | ans_max = max(ans_max, res) 14 | ans_min = min(ans_min, res) 15 | continue 16 | 17 | if plus: 18 | dq.append((res + nums[n], n + 1, plus - 1, minus, multi, div)) 19 | 20 | if minus: 21 | dq.append((res - nums[n], n + 1, plus, minus - 1, multi, div)) 22 | 23 | if multi: 24 | dq.append((res * nums[n], n + 1, plus, minus, multi - 1, div)) 25 | 26 | if div: 27 | next_res = res // nums[n] if res >= 0 else -(-res // nums[n]) 28 | dq.append((next_res, n + 1, plus, minus, multi, div - 1)) 29 | 30 | print(ans_max) 31 | print(ans_min) 32 | -------------------------------------------------------------------------------- /PART 2/Practice 06/3.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | var N = 0 4 | var cnt = 0 5 | val board = Array(20) { Array(20) { false } } 6 | val isOnQueenInCol = Array(20) { false } 7 | 8 | fun main() = with(Scanner(System.`in`)) { 9 | N = nextInt() 10 | backTracking(0) 11 | println(cnt) 12 | } 13 | 14 | fun backTracking(i: Int) { 15 | if (i >= N) { 16 | ++cnt 17 | return 18 | } 19 | 20 | for (j in 0 until N) { 21 | if (!isOnQueenInCol[j] && !isOnQueenInDiagonal(i, j)) { 22 | board[i][j] = true 23 | isOnQueenInCol[j] = true 24 | backTracking(i + 1) 25 | isOnQueenInCol[j] = false 26 | board[i][j] = false 27 | } 28 | } 29 | } 30 | 31 | // y, x 기준으로 ↖, ↗ 방향으로 퀸이 있는지 32 | fun isOnQueenInDiagonal(y: Int, x: Int): Boolean { 33 | for (i in 1..y) 34 | if (0 <= x - i && board[y - i][x - i]) return true; 35 | else if (x + i < N && board[y - i][x + i]) return true; 36 | 37 | return false; 38 | } -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 03/2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dy = (0, 1, 0, -1) 4 | dx = (1, 0, -1, 0) 5 | 6 | N = int(input()) 7 | apples = sorted(tuple(map(int, input().split())) for _ in range(int(input()))) 8 | mv = [0] * 10001 9 | for _ in range(int(input())): 10 | X, C = input().split() 11 | mv[int(X)] = C 12 | 13 | snake = deque() 14 | snake.append((1, 1)) 15 | d = 0 # 방향 16 | t = 0 # 시간 17 | 18 | 19 | def is_valid_coord(y, x): 20 | return 1 <= y <= N and 1 <= x <= N 21 | 22 | 23 | while True: 24 | t += 1 25 | y, x = snake[0] 26 | ny = y + dy[d] 27 | nx = x + dx[d] 28 | if is_valid_coord(ny, nx) and (ny, nx) not in snake: 29 | snake.appendleft((ny, nx)) 30 | else: 31 | break 32 | 33 | if (ny, nx) in apples: 34 | apples.remove((ny, nx)) 35 | else: 36 | snake.pop() 37 | 38 | if mv[t]: 39 | if mv[t] == 'D': 40 | d = (d + 1) % 4 41 | else: 42 | d = (d + 3) % 4 43 | 44 | print(t) 45 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 08/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int dy[4] = {0, 1, 0, -1}; 9 | const int dx[4] = {1, 0, -1, 0}; 10 | int R, C, ans; 11 | string board[20]; 12 | bool chk[26]; 13 | 14 | bool isValidCoord(int y, int x) { 15 | return 0 <= y && y < R && 0 <= x && x < C; 16 | } 17 | 18 | void recur(int y, int x, int l) { 19 | chk[board[y][x] - 'A'] = true; 20 | ans = max(ans, l); 21 | 22 | for (int k = 0; k < 4; ++k) { 23 | int ny = y + dy[k]; 24 | int nx = x + dx[k]; 25 | int nl = l + 1; 26 | if (isValidCoord(ny, nx) && !chk[board[ny][nx] - 'A']) 27 | recur(ny, nx, nl); 28 | } 29 | 30 | chk[board[y][x] - 'A'] = false; 31 | } 32 | 33 | int main() { 34 | scanf("%d %d", &R, &C); 35 | for (int i = 0; i < R; ++i) 36 | cin >> board[i]; 37 | 38 | recur(0, 0, 1); 39 | 40 | printf("%d\n", ans); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /PART 3/Problem 04/2.kt: -------------------------------------------------------------------------------- 1 | // Floyd-Warshall 2 | 3 | import java.util.* 4 | import kotlin.math.min 5 | 6 | val INF = 987654321 7 | 8 | fun main() = with(Scanner(System.`in`)) { 9 | val N = nextInt() 10 | val gr = List(N) { MutableList(N) { INF } } 11 | for (i in 0 until nextInt()) { 12 | val a = nextInt() - 1 13 | val b = nextInt() - 1 14 | gr[a][b] = 1 15 | gr[b][a] = 1 16 | } 17 | 18 | for (k in 0 until N) 19 | for (i in 0 until N) 20 | if (gr[i][k] < INF) 21 | for (j in 0 until N) 22 | if (i != j && gr[k][j] < INF) 23 | gr[i][j] = min(gr[i][j], gr[i][k] + gr[k][j]) 24 | 25 | val kevinBacon = MutableList(N) {0} 26 | for (i in 0 until N) 27 | for (j in 0 until N) 28 | if (i != j) kevinBacon[i] += gr[i][j] 29 | 30 | for (i in 0 until N) 31 | if (kevinBacon[i] == kevinBacon.minOrNull()) { 32 | println(i + 1) 33 | break 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /PART 3/Problem 04/1.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | N, M = map(int, input().split()) 4 | gr = [[False] * N for _ in range(N)] 5 | for _ in range(M): 6 | a, b = map(int, input().split()) 7 | gr[a - 1][b - 1] = gr[b - 1][a - 1] = True 8 | 9 | ans = -1 10 | ans_tot = 987654321 11 | dist = [[0] * N for _ in range(N)] 12 | 13 | 14 | def bfs(start, goal): 15 | chk = [False] * N 16 | dq = deque() 17 | chk[start] = True 18 | dq.append((start, 0)) 19 | while dq: 20 | now, d = dq.popleft() 21 | if now == goal: 22 | return d 23 | 24 | for nxt in range(N): 25 | if gr[now][nxt] and not chk[nxt]: 26 | chk[nxt] = True 27 | dq.append((nxt, d + 1)) 28 | 29 | 30 | for i in range(N): 31 | tot = 0 32 | for j in range(N): 33 | if i != j: 34 | if dist[i][j] == 0: 35 | dist[i][j] = dist[j][i] = bfs(i, j) 36 | 37 | tot += dist[i][j] 38 | 39 | if ans_tot > tot: 40 | ans = i 41 | ans_tot = tot 42 | 43 | print(ans + 1) 44 | -------------------------------------------------------------------------------- /PART 2/Practice 06/2.py: -------------------------------------------------------------------------------- 1 | # BOJ에서 Python3로 통과 2 | 3 | N = int(input()) 4 | ans = 0 5 | col = [False] * N # i번째 열에 퀸을 뒀는지 6 | d1 = [False] * 2 * N # \ 대각선, 우상단부터 0 7 | d2 = [False] * 2 * N # / 대각선, 좌상단부터 0 8 | 9 | 10 | def backtracking(row): 11 | global ans 12 | if row == N: 13 | ans += 1 14 | return 15 | 16 | for j in range(N if row else N // 2): 17 | if not col[j] and not d1[row - j] and not d2[row + j]: 18 | col[j] = True 19 | d1[row - j] = True 20 | d2[row + j] = True 21 | 22 | backtracking(row + 1) 23 | 24 | d2[row + j] = False 25 | d1[row - j] = False 26 | col[j] = False 27 | 28 | 29 | if N % 2: 30 | # 홀수일 때 첫번째 행에 퀸을 왼쪽 절반만 둬보고 그 때의 경우의 수를 2배로 취해준다 (정가운데 제외) 31 | backtracking(0) 32 | ans *= 2 33 | 34 | # 첫번째 행의 정가운데에 퀸을 뒀을 때의 경우의 수를 구해서 더해준다 35 | j = N // 2 36 | col[j] = d1[-j] = d2[j] = True 37 | backtracking(1) 38 | 39 | print(ans) 40 | else: 41 | # 짝수일 때 첫번째 행에 퀸을 왼쪽 절반만 둬보고 그 떄의 경우의 수를 2배로 취해준다 42 | backtracking(0) 43 | print(ans * 2) 44 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 07/2.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | val dy = arrayOf(0, 1, 0, -1) 4 | val dx = arrayOf(1, 0, -1, 0) 5 | var N = 0 6 | var M = 0 7 | 8 | fun main() = with(Scanner(System.`in`)) { 9 | N = nextInt() 10 | M = nextInt() 11 | val board = Array(N) { next() } 12 | val chk = MutableList(N) { MutableList(M) { false } } 13 | val q: Queue = LinkedList() 14 | q.add(State(0, 0, 1)) 15 | chk[0][0] = true 16 | while (!q.isEmpty()) { 17 | val now = q.poll() 18 | if (now.y == N - 1 && now.x == M - 1) { 19 | println(now.d) 20 | break 21 | } 22 | 23 | for (k in 0 until 4) { 24 | val ny = now.y + dy[k] 25 | val nx = now.x + dx[k] 26 | val nd = now.d + 1 27 | if (isValidCoord(ny, nx) && board[ny][nx] == '1' && !chk[ny][nx]) { 28 | chk[ny][nx] = true 29 | q.add(State(ny, nx, nd)) 30 | } 31 | } 32 | } 33 | } 34 | 35 | fun isValidCoord(y: Int, x: Int) = y in 0 until N && x in 0 until M 36 | 37 | data class State(val y: Int, val x: Int, val d: Int) 38 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 03/1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | board = [list(input()) for _ in range(N)] 3 | ans = 1 4 | 5 | 6 | def search(): 7 | global ans 8 | for i in range(N): 9 | cnt = 1 10 | for j in range(1, N): 11 | if board[i][j - 1] == board[i][j]: 12 | cnt += 1 13 | ans = max(ans, cnt) 14 | else: 15 | cnt = 1 16 | 17 | for j in range(N): 18 | cnt = 1 19 | for i in range(1, N): 20 | if board[i - 1][j] == board[i][j]: 21 | cnt += 1 22 | ans = max(ans, cnt) 23 | else: 24 | cnt = 1 25 | 26 | 27 | for i in range(N): 28 | for j in range(N): 29 | if j + 1 < N: 30 | board[i][j], board[i][j + 1] = board[i][j + 1], board[i][j] 31 | search() 32 | board[i][j], board[i][j + 1] = board[i][j + 1], board[i][j] # 원상복구 33 | 34 | if i + 1 < N: 35 | board[i][j], board[i + 1][j] = board[i + 1][j], board[i][j] 36 | search() 37 | board[i][j], board[i + 1][j] = board[i + 1][j], board[i][j] # 원상복구 38 | 39 | print(ans) 40 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 09/1.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dy = (0, 1, 0, -1) 4 | dx = (1, 0, -1, 0) 5 | 6 | 7 | def is_valid_coord(y, x): 8 | return 0 <= y < 5 and 0 <= x < 5 9 | 10 | 11 | def bfs(sy, sx, place): 12 | chk = [[False] * 5 for _ in range(5)] 13 | chk[sy][sx] = True 14 | dq = deque() 15 | dq.append((sy, sx, 0)) 16 | while dq: 17 | y, x, d = dq.popleft() 18 | if d and place[y][x] == 'P': 19 | return False 20 | 21 | if d < 2: 22 | nd = d + 1 23 | for k in range(4): 24 | ny = y + dy[k] 25 | nx = x + dx[k] 26 | if is_valid_coord(ny, nx) and place[ny][nx] != 'X' and not chk[ny][nx]: 27 | chk[ny][nx] = True 28 | dq.append((ny, nx, nd)) 29 | 30 | return True 31 | 32 | 33 | def is_ok(place): 34 | for i in range(5): 35 | for j in range(5): 36 | if place[i][j] == 'P': 37 | if not bfs(i, j, place): 38 | return 0 39 | 40 | return 1 41 | 42 | 43 | def solution(places): 44 | return [is_ok(place) for place in places] 45 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 06/3.kt: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader 2 | import java.io.BufferedWriter 3 | import java.io.InputStreamReader 4 | import java.io.OutputStreamWriter 5 | 6 | var N = 0 7 | var M = 0 8 | var gr = Array(0) { Array(0) { false } } 9 | var chk = Array(0) { false } 10 | var ans = 0 11 | 12 | fun main() = with(BufferedReader(InputStreamReader(System.`in`))) { 13 | BufferedWriter(OutputStreamWriter(System.out)).use { bw -> 14 | val nm = readLine().split(" ").map { it.toInt() } 15 | N = nm[0] 16 | M = nm[1] 17 | gr = Array(N) { Array(N) { false } } 18 | chk = Array(N) { false } 19 | repeat(M) { 20 | val (u, v) = readLine().split(" ").map { it.toInt() - 1 } 21 | gr[u][v] = true 22 | gr[v][u] = true 23 | } 24 | 25 | for (i in 0 until N) 26 | if (!chk[i]) { 27 | ++ans 28 | dfs(i) 29 | } 30 | 31 | bw.write("${ans}\n") 32 | } 33 | } 34 | 35 | fun dfs(i: Int) { 36 | if (chk[i]) return 37 | 38 | chk[i] = true 39 | 40 | for (j in 0 until N) 41 | if (gr[i][j] && !chk[j]) 42 | dfs(j) 43 | } 44 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 03/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | board = list(list(input()) for _ in range(N)) 3 | ans = 1 4 | 5 | 6 | def search(y, x): 7 | global ans 8 | for i in range(y, y + 2): 9 | if i < N: 10 | cnt = 1 11 | for j in range(1, N): 12 | if board[i][j - 1] == board[i][j]: 13 | cnt += 1 14 | ans = max(ans, cnt) 15 | else: 16 | cnt = 1 17 | 18 | for j in range(x, x + 2): 19 | if j < N: 20 | cnt = 1 21 | for i in range(1, N): 22 | if board[i - 1][j] == board[i][j]: 23 | cnt += 1 24 | ans = max(ans, cnt) 25 | else: 26 | cnt = 1 27 | 28 | 29 | for i in range(N): 30 | for j in range(N): 31 | if j + 1 < N: 32 | board[i][j], board[i][j + 1] = board[i][j + 1], board[i][j] 33 | search(i, j) 34 | board[i][j], board[i][j + 1] = board[i][j + 1], board[i][j] 35 | 36 | if i + 1 < N: 37 | board[i][j], board[i + 1][j] = board[i + 1][j], board[i][j] 38 | search(i, j) 39 | board[i][j], board[i + 1][j] = board[i + 1][j], board[i][j] 40 | 41 | print(ans) 42 | -------------------------------------------------------------------------------- /PART 2/Practice 05/2.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | val dy = arrayOf(-1, -2, -2, -1, 1, 2, 2, 1) 4 | val dx = arrayOf(-2, -1, 1, 2, 2, 1, -1, -2) 5 | var l = 0 6 | 7 | fun main() = with(Scanner(System.`in`)) { 8 | repeat(nextInt()) { 9 | l = nextInt() 10 | val chk = MutableList(l) { MutableList(l) { false } } 11 | val q: Queue = LinkedList() 12 | val sy = nextInt() 13 | val sx = nextInt() 14 | val gy = nextInt() 15 | val gx = nextInt() 16 | chk[sy][sx] = true 17 | q.add(State(sy, sx, 0)) 18 | while (!q.isEmpty()) { 19 | val now = q.poll() 20 | if (now.y == gy && now.x == gx) { 21 | println(now.d) 22 | break 23 | } 24 | 25 | for (k in 0 until 8) { 26 | val ny = now.y + dy[k] 27 | val nx = now.x + dx[k] 28 | val nd = now.d + 1 29 | if (isValidCoord(ny, nx) && !chk[ny][nx]) { 30 | chk[ny][nx] = true 31 | q.add(State(ny, nx, nd)) 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | fun isValidCoord(y: Int, x: Int) = y in 0 until l && x in 0 until l 39 | 40 | data class State(val y: Int, val x: Int, val d: Int) 41 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 02/2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | board = [list(map(int, input().split())) for _ in range(N)] 3 | ans = 0 4 | 5 | 6 | def rotate_board(b): 7 | ret = [[0] * N for _ in range(N)] 8 | for i in range(N): 9 | for j in range(N): 10 | ret[i][j] = b[j][N - 1 - i] 11 | 12 | return ret 13 | 14 | 15 | def push_left(b): 16 | ret = [[0] * N for _ in range(N)] 17 | for i, r in enumerate(b): 18 | row = r[:] 19 | while 0 in row: 20 | row.remove(0) 21 | 22 | nrow = [] 23 | j = 0 24 | while j < len(row): 25 | if j + 1 < len(row) and row[j] == row[j + 1]: 26 | nrow.append(row[j] << 1) 27 | j += 2 28 | else: 29 | nrow.append(row[j]) 30 | j += 1 31 | 32 | for j in range(len(nrow)): 33 | ret[i][j] = nrow[j] 34 | 35 | return ret 36 | 37 | 38 | def dfs(b, d): 39 | global ans 40 | for i in range(N): 41 | for j in range(N): 42 | ans = max(ans, b[i][j]) 43 | 44 | if d < 5: 45 | nd = d + 1 46 | dfs(push_left(b), nd) 47 | for k in range(3): 48 | b = rotate_board(b) 49 | dfs(push_left(b), nd) 50 | 51 | 52 | dfs(board, 0) 53 | print(ans) 54 | -------------------------------------------------------------------------------- /PART 2/Chapter 01/Problem 04/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int N; 8 | priority_queue, greater<> > a; // 양수 저장할 최소힙 9 | priority_queue b; // 음수 저장할 최대힙 10 | 11 | int main() { 12 | scanf("%d", &N); 13 | 14 | int x; 15 | for (int i = 0; i < N; ++i) { 16 | scanf("%d", &x); 17 | if (x > 0) 18 | a.emplace(x); 19 | else if (x < 0) 20 | b.emplace(x); 21 | else { // x == 0 22 | if (a.empty()) { 23 | if (b.empty()) 24 | printf("0\n"); 25 | else { 26 | printf("%d\n", b.top()); 27 | b.pop(); 28 | } 29 | } else { 30 | if (b.empty()) { 31 | printf("%d\n", a.top()); 32 | a.pop(); 33 | } else { 34 | if (a.top() < abs(b.top())) { 35 | printf("%d\n", a.top()); 36 | a.pop(); 37 | } else { 38 | printf("%d\n", b.top()); 39 | b.pop(); 40 | } 41 | } 42 | } 43 | } 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /PART 3/Problem 03/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int N, M, houseSz, chickenSz, ans = 987654321; 9 | vector > house, chicken; 10 | 11 | int getDistance(int i, int j) { 12 | return abs(house[i].first - chicken[j].first) + abs(house[i].second - chicken[j].second); 13 | } 14 | 15 | int main() { 16 | scanf("%d %d", &N, &M); 17 | 18 | int ipt; 19 | for (int i = 0; i < N; ++i) 20 | for (int j = 0; j < N; ++j) { 21 | scanf("%d", &ipt); 22 | if (ipt == 1) house.emplace_back(i, j); 23 | else if (ipt == 2) chicken.emplace_back(i, j); 24 | } 25 | 26 | houseSz = house.size(); 27 | chickenSz = chicken.size(); 28 | vector v(chickenSz, 0); 29 | for (int i = 0; i < M; ++i) v[v.size() - 1 - i] = 1; 30 | 31 | do { 32 | int chickenRoad = 0; 33 | for (int i = 0; i < houseSz; ++i) { 34 | int cr = 987654321; 35 | for (int j = 0; j < chickenSz; ++j) 36 | if (v[j]) 37 | cr = min(cr, getDistance(i, j)); 38 | 39 | chickenRoad += cr; 40 | } 41 | 42 | ans = min(ans, chickenRoad); 43 | } while (next_permutation(v.begin(), v.end())); 44 | 45 | printf("%d\n", ans); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 08/3.swift: -------------------------------------------------------------------------------- 1 | let dy = [0, 1, 0, -1] 2 | let dx = [1, 0, -1, 0] 3 | 4 | let rc = readLine()!.split(separator: " ").map { 5 | Int(String($0))! 6 | } 7 | let R = rc[0], C = rc[1] 8 | let asciiA = UnicodeScalar("A").value 9 | var chk = [Bool](repeating: false, count: 26) 10 | var board: [[Int]] = [] 11 | for _ in 0..= maximum { 34 | return 35 | } 36 | } 37 | 38 | for k in 0..<4 { 39 | let ny = y + dy[k] 40 | let nx = x + dx[k] 41 | if 0..= maximum { 46 | return 47 | } 48 | } 49 | } 50 | } 51 | } 52 | 53 | backtracking(0, 0, 1, 1 << board[0][0]) 54 | print(ans) -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 04/1.py: -------------------------------------------------------------------------------- 1 | dy = (0, 0, 0, -1, 1) 2 | dx = (0, 1, -1, 0, 0) 3 | 4 | N, M, y, x, K = map(int, input().split()) 5 | board = [list(map(int, input().split())) for _ in range(N)] 6 | dice = [0, 0, 0, 0, 0, 0, 0] 7 | 8 | 9 | def is_valid_coord(y, x): 10 | return 0 <= y < N and 0 <= x < M 11 | 12 | 13 | for cmd in list(map(int, input().split())): 14 | ny = y + dy[cmd] 15 | nx = x + dx[cmd] 16 | if not is_valid_coord(ny, nx): 17 | continue 18 | 19 | next_dice = dice[:] 20 | if cmd == 1: # 동쪽 21 | next_dice[1] = dice[4] 22 | next_dice[3] = dice[1] 23 | next_dice[6] = dice[3] 24 | next_dice[4] = dice[6] 25 | elif cmd == 2: # 서쪽 26 | next_dice[1] = dice[3] 27 | next_dice[3] = dice[6] 28 | next_dice[6] = dice[4] 29 | next_dice[4] = dice[1] 30 | elif cmd == 3: # 북쪽 31 | next_dice[1] = dice[5] 32 | next_dice[2] = dice[1] 33 | next_dice[6] = dice[2] 34 | next_dice[5] = dice[6] 35 | else: # 남쪽 36 | next_dice[1] = dice[2] 37 | next_dice[2] = dice[6] 38 | next_dice[6] = dice[5] 39 | next_dice[5] = dice[1] 40 | 41 | dice = next_dice[:] 42 | if board[ny][nx] == 0: 43 | board[ny][nx] = dice[6] 44 | else: 45 | dice[6] = board[ny][nx] 46 | board[ny][nx] = 0 47 | 48 | y = ny 49 | x = nx 50 | print(dice[1]) 51 | -------------------------------------------------------------------------------- /PART 3/Problem 20/1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | board = [list(map(int, input().split())) for _ in range(N)] 3 | acc = [[0] * N for _ in range(N)] 4 | ans = 0 5 | 6 | 7 | def get_area(a, b, c, d): 8 | ret = acc[c][d] 9 | if a: 10 | ret -= acc[a - 1][d] 11 | 12 | if b: 13 | ret -= acc[c][b - 1] 14 | 15 | if a and b: 16 | ret += acc[a - 1][b - 1] 17 | 18 | return ret 19 | 20 | 21 | def f(): 22 | global ans 23 | acc[0][0] = board[0][0] 24 | for j in range(1, N): 25 | acc[0][j] = acc[0][j - 1] + board[0][j] 26 | 27 | for i in range(1, N): 28 | acc[i][0] = acc[i - 1][0] + board[i][0] 29 | for j in range(1, N): 30 | acc[i][j] = acc[i][j - 1] + acc[i - 1][j] - acc[i - 1][j - 1] + board[i][j] 31 | 32 | for y in range(1, N): 33 | for x in range(1, N): 34 | cnt = dict() 35 | for i in range(y): 36 | for j in range(x): 37 | area = get_area(i, j, y - 1, x - 1) 38 | if area in cnt: 39 | cnt[area] += 1 40 | else: 41 | cnt[area] = 1 42 | 43 | for i in range(y, N): 44 | for j in range(x, N): 45 | area = get_area(y, x, i, j) 46 | if area in cnt: 47 | ans += cnt[area] 48 | 49 | 50 | f() 51 | for i in range(N): 52 | board[i].reverse() 53 | 54 | f() 55 | 56 | print(ans) 57 | -------------------------------------------------------------------------------- /PART 2/Chapter 02/Problem 03/3.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | import kotlin.math.max 3 | 4 | var N = 0 5 | var ans = 0 6 | var board = Array(0) { StringBuilder() } 7 | 8 | fun main() = with(Scanner(System.`in`)) { 9 | N = nextLine().toInt() 10 | board = Array(N) { StringBuilder(nextLine()) } 11 | for (i in 0 until N) 12 | for (j in 0 until N) { 13 | if (i + 1 < N) { 14 | board[i][j] = board[i + 1][j].also { board[i + 1][j] = board[i][j] } 15 | getMaxCandies() 16 | board[i][j] = board[i + 1][j].also { board[i + 1][j] = board[i][j] } 17 | } 18 | 19 | if (j + 1 < N) { 20 | board[i][j] = board[i][j + 1].also { board[i][j + 1] = board[i][j] } 21 | getMaxCandies() 22 | board[i][j] = board[i][j + 1].also { board[i][j + 1] = board[i][j] } 23 | } 24 | } 25 | 26 | println(ans) 27 | } 28 | 29 | fun getMaxCandies() { 30 | for (i in 0 until N) { 31 | var tot = 1 32 | for (j in 1 until N) 33 | if (board[i][j - 1] == board[i][j]) { 34 | ++tot 35 | ans = max(ans, tot) 36 | } else 37 | tot = 1 38 | } 39 | 40 | for (j in 0 until N) { 41 | var tot = 1 42 | for (i in 1 until N) 43 | if (board[i - 1][j] == board[i][j]) { 44 | ++tot 45 | ans = max(ans, tot) 46 | } else 47 | tot = 1 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 06/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | vector solution(vector orders, vector course) { 9 | vector answer; 10 | for (int n: course) { 11 | map cnt; 12 | for (string order: orders) { 13 | sort(order.begin(), order.end()); 14 | 15 | if (order.length() >= n) { 16 | vector v(order.length(), 0); 17 | for (int i = 0; i < n; ++i) 18 | v[order.length() - 1 - i] = 1; 19 | 20 | do { 21 | string combination; 22 | for (int i = 0; i < order.length(); ++i) 23 | if (v[i]) 24 | combination += order[i]; 25 | 26 | if (cnt.find(combination) == cnt.end()) 27 | cnt[combination] = 1; 28 | else 29 | cnt[combination] += 1; 30 | } while (next_permutation(v.begin(), v.end())); 31 | } 32 | } 33 | 34 | int maxVal = 0; 35 | for (const auto &p: cnt) 36 | maxVal = max(maxVal, p.second); 37 | 38 | if (maxVal >= 2) 39 | for (const auto &p: cnt) 40 | if (p.second == maxVal) 41 | answer.emplace_back(p.first); 42 | } 43 | 44 | sort(answer.begin(), answer.end()); 45 | return answer; 46 | } -------------------------------------------------------------------------------- /PART 4/Chapter 02/Problem 07/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | vector split(string s) { 10 | istringstream ss(s); 11 | string buf; 12 | vector res; 13 | while (getline(ss, buf, ' ')) 14 | res.emplace_back(buf); 15 | 16 | return res; 17 | } 18 | 19 | vector solution(vector infos, vector queries) { 20 | map > mp; 21 | for (const string &info: infos) { 22 | vector s = split(info); 23 | for (const string &s0: vector{s[0], "-"}) 24 | for (const string &s1: vector{s[1], "-"}) 25 | for (const string &s2: vector{s[2], "-"}) 26 | for (const string &s3: vector{s[3], "-"}) 27 | mp[s0 + s1 + s2 + s3].emplace_back(atoi(s[4].c_str())); 28 | } 29 | 30 | for (auto it = mp.begin(); it != mp.end(); ++it) 31 | sort(it->second.begin(), it->second.end()); 32 | 33 | vector answer; 34 | for (const string &query: queries) { 35 | vector s = split(query); 36 | string q = s[0] + s[2] + s[4] + s[6]; 37 | if (mp.find(q) == mp.end()) 38 | answer.emplace_back(0); 39 | else { 40 | auto v = mp[q]; 41 | answer.emplace_back(v.end() - lower_bound(v.begin(), v.end(), atoi(s[7].c_str()))); 42 | } 43 | } 44 | 45 | return answer; 46 | } -------------------------------------------------------------------------------- /PART 3/Problem 15/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | long long N, dice[6]; 7 | 8 | long long getMin1() { 9 | long long minVal = 50; 10 | for (int i = 0; i < 6; i++) minVal = min(minVal, dice[i]); 11 | 12 | return minVal; 13 | } 14 | 15 | long long getMin2() { 16 | long long res = 100; 17 | for (int i = 0; i < 5; i++) 18 | for (int j = i + 1; j < 6; j++) 19 | if (i + j != 5) 20 | res = min(res, dice[i] + dice[j]); 21 | 22 | return res; 23 | } 24 | 25 | long long getMin3() { 26 | long long res = 150; 27 | for (int i = 0; i < 4; i++) 28 | for (int j = i + 1; j < 5; j++) 29 | if (i + j != 5) 30 | for (int k = j + 1; k < 6; k++) 31 | if (j + k != 5 && i + k != 5) 32 | res = min(res, dice[i] + dice[j] + dice[k]); 33 | 34 | return res; 35 | } 36 | 37 | int main() { 38 | scanf("%lld", &N); 39 | for (int i = 0; i < 6; i++) scanf("%lld", &dice[i]); 40 | 41 | if (N == 1) { 42 | long long maxVal = 0ll; 43 | long long sum = 0ll; 44 | for (int i = 0; i < 6; i++) { 45 | sum += dice[i]; 46 | maxVal = max(maxVal, dice[i]); 47 | } 48 | 49 | printf("%lld\n", sum - maxVal); 50 | } else { 51 | long long ans = getMin3() * 4ll; 52 | ans += getMin2() * 4ll * (2ll * N - 3ll); 53 | ans += getMin1() * (N - 2ll) * (5ll * N - 6ll); 54 | printf("%lld\n", ans); 55 | } 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /PART 3/Problem 18/1.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dz = (0, 0, 0, 0, 1, -1) 4 | dy = (0, 1, 0, -1, 0, 0) 5 | dx = (1, 0, -1, 0, 0, 0) 6 | 7 | while True: 8 | L, R, C = map(int, input().split()) 9 | if L == 0: 10 | break 11 | 12 | b = [] 13 | for _ in range(L): 14 | b.append([input() for _ in range(R)]) 15 | input() # blank line 16 | 17 | 18 | def get_start(): 19 | for i in range(L): 20 | for j in range(R): 21 | for k in range(C): 22 | if b[i][j][k] == 'S': 23 | return i, j, k 24 | 25 | 26 | def is_valid_coord(z, y, x): 27 | return 0 <= z < L and 0 <= y < R and 0 <= x < C 28 | 29 | 30 | def bfs(sz, sy, sx): 31 | chk = [[[False] * C for _ in range(R)] for _ in range(L)] 32 | chk[sz][sy][sx] = True 33 | dq = deque() 34 | dq.append((sz, sy, sx, 0)) 35 | while dq: 36 | z, y, x, d = dq.popleft() 37 | 38 | if b[z][y][x] == 'E': 39 | return d 40 | 41 | for k in range(6): 42 | nz = z + dz[k] 43 | ny = y + dy[k] 44 | nx = x + dx[k] 45 | nd = d + 1 46 | if is_valid_coord(nz, ny, nx) and b[nz][ny][nx] != '#' and not chk[nz][ny][nx]: 47 | chk[nz][ny][nx] = True 48 | dq.append((nz, ny, nx, nd)) 49 | 50 | return 0 51 | 52 | 53 | sz, sy, sx = get_start() 54 | ans = bfs(sz, sy, sx) 55 | print(f'Escaped in {ans} minute(s).' if ans else 'Trapped!') 56 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 07/1.py: -------------------------------------------------------------------------------- 1 | dy = (-1, -1, 1, 1) 2 | dx = (-1, 1, -1, 1) 3 | ddy = (0, -1, -1, -1, 0, 1, 1, 1) 4 | ddx = (-1, -1, 0, 1, 1, 1, 0, -1) 5 | 6 | N, M = map(int, input().split()) 7 | A = [list(map(int, input().split())) for _ in range(N)] 8 | clouds = [[[False] * N for _ in range(N)] for _ in range(2)] 9 | c = 0 10 | for i in range(N - 2, N): 11 | for j in range(2): 12 | clouds[c][i][j] = True 13 | 14 | 15 | def is_valid_coord(y, x): 16 | return 0 <= y < N and 0 <= x < N 17 | 18 | 19 | for _ in range(M): 20 | d, s = map(int, input().split()) 21 | for y in range(N): 22 | for x in range(N): 23 | if clouds[c][y][x]: 24 | ny = (y + ddy[d - 1] * s + N * 50) % N 25 | nx = (x + ddx[d - 1] * s + N * 50) % N 26 | clouds[c ^ 1][ny][nx] = True 27 | A[ny][nx] += 1 28 | 29 | for y in range(N): 30 | for x in range(N): 31 | if clouds[c ^ 1][y][x]: 32 | for k in range(4): 33 | ny = y + dy[k] 34 | nx = x + dx[k] 35 | if is_valid_coord(ny, nx) and A[ny][nx]: 36 | A[y][x] += 1 37 | 38 | for y in range(N): 39 | for x in range(N): 40 | if not clouds[c ^ 1][y][x] and A[y][x] >= 2: 41 | A[y][x] -= 2 42 | clouds[c][y][x] = True 43 | else: 44 | clouds[c][y][x] = False 45 | 46 | for y in range(N): 47 | for x in range(N): 48 | clouds[c ^ 1][y][x] = False 49 | 50 | print(sum(sum(i) for i in A)) 51 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 05/1.py: -------------------------------------------------------------------------------- 1 | dy = (0, 1, 0, -1) 2 | dx = (1, 0, -1, 0) 3 | 4 | N = int(input()) 5 | students = [tuple(map(int, input().split())) for _ in range(N ** 2)] 6 | board = [[[0] * 5 for _ in range(N)] for _ in range(N)] 7 | 8 | 9 | def is_valid_coord(y, x): 10 | return 0 <= y < N and 0 <= x < N 11 | 12 | 13 | for student in students: 14 | num, fav = student[0], student[1:] 15 | seats = [[[0] * 2 for _ in range(N)] for _ in range(N)] # 각 빈칸들에 대해서 인접칸에 좋아하는 학생 수의 합, 빈 칸의 합 저장 16 | for y in range(N): 17 | for x in range(N): 18 | if board[y][x][0] == 0: 19 | for k in range(4): 20 | ny = y + dy[k] 21 | nx = x + dx[k] 22 | if is_valid_coord(ny, nx): 23 | if board[ny][nx][0] == 0: 24 | seats[y][x][1] += 1 25 | elif board[ny][nx][0] in fav: 26 | seats[y][x][0] += 1 27 | 28 | candi = [] 29 | for y in range(N): 30 | for x in range(N): 31 | if board[y][x][0] == 0: 32 | candi.append((seats[y][x][0], seats[y][x][1], y, x)) 33 | 34 | candi.sort(key=lambda x: (-x[0], -x[1], x[2], x[3])) 35 | board[candi[0][2]][candi[0][3]] = student 36 | 37 | ans = 0 38 | for y in range(N): 39 | for x in range(N): 40 | cnt = 0 41 | for k in range(4): 42 | ny = y + dy[k] 43 | nx = x + dx[k] 44 | if is_valid_coord(ny, nx) and board[ny][nx][0] in board[y][x][1:]: 45 | cnt += 1 46 | 47 | if cnt > 0: 48 | ans += 10 ** (cnt - 1) 49 | 50 | print(ans) 51 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 03/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int dy[4] = {0, 1, 0, -1}; 7 | const int dx[4] = {1, 0, -1, 0}; 8 | 9 | struct coord { 10 | int y; 11 | int x; 12 | }; 13 | 14 | int N, K, L, board[105][105], dir[10005], d; 15 | deque dq; 16 | 17 | bool isValidYx(int y, int x) { 18 | return 0 < y && y <= N && 0 < x && x <= N; 19 | } 20 | 21 | bool isCollideWithSelf(int y, int x) { 22 | for (auto cell: dq) 23 | if (y == cell.y && x == cell.x) 24 | return true; 25 | 26 | return false; 27 | } 28 | 29 | int main() { 30 | dq.emplace_back(coord{1, 1}); 31 | 32 | scanf("%d%d", &N, &K); 33 | int yy, xx; 34 | for (int i = 0; i < K; ++i) { 35 | scanf("%d %d", &yy, &xx); 36 | board[yy][xx] = 1; 37 | } 38 | 39 | scanf("%d", &L); 40 | int tt; 41 | char dd; 42 | for (int i = 0; i < L; ++i) { 43 | scanf("%d %c", &tt, &dd); 44 | dir[tt] = dd; 45 | } 46 | 47 | int sec; 48 | for (sec = 1;; ++sec) { 49 | int ny = dq.front().y + dy[d]; 50 | int nx = dq.front().x + dx[d]; 51 | 52 | if (!isValidYx(ny, nx)) 53 | break; 54 | 55 | if (isCollideWithSelf(ny, nx)) 56 | break; 57 | 58 | dq.emplace_front(coord{ny, nx}); 59 | 60 | if (board[ny][nx] == 1) 61 | board[ny][nx] = 0; 62 | else 63 | dq.pop_back(); 64 | 65 | if (dir[sec] == 'D') 66 | d = (d + 1) % 4; 67 | else if (dir[sec] == 'L') 68 | d = (d + 3) % 4; 69 | } 70 | 71 | printf("%d\n", sec); 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 08/1.py: -------------------------------------------------------------------------------- 1 | from itertools import groupby, chain 2 | 3 | dy = (0, 1, 0, -1) 4 | dx = (-1, 0, 1, 0) 5 | 6 | N, M = map(int, input().split()) 7 | beads = [list(map(int, input().split())) for _ in range(N)] 8 | ans = 0 9 | 10 | 11 | def make_coords(): 12 | ret = [(N // 2, N // 2)] 13 | d = 0 14 | for s in range(1, N + 1): 15 | for _ in range(2): 16 | for _ in range(s): 17 | y, x = ret[-1] 18 | y += dy[d] 19 | x += dx[d] 20 | ret.append((y, x)) 21 | if len(ret) >= N * N: 22 | return ret 23 | 24 | d = (d + 1) % 4 25 | 26 | 27 | coords = make_coords() 28 | for _ in range(M): 29 | d, s = map(int, input().split()) 30 | if d == 1: 31 | d = 3 32 | elif d == 2: 33 | d = 1 34 | elif d == 3: 35 | d = 0 36 | else: 37 | d = 2 38 | 39 | # 구슬 파괴 40 | y, x = coords[0] 41 | for i in range(1, s + 1): 42 | y += dy[d] 43 | x += dx[d] 44 | beads[y][x] = 0 45 | 46 | # 구슬 이동 및 폭발 47 | arr = [[] for _ in range(2)] 48 | b = 0 49 | for y, x in coords: 50 | if beads[y][x]: 51 | arr[b].append(beads[y][x]) 52 | 53 | while True: 54 | is_boomed = False 55 | for bead, gr in groupby(arr[b]): 56 | tmp = list(gr) 57 | if len(tmp) >= 4: 58 | is_boomed = True 59 | ans += bead * len(tmp) 60 | else: 61 | arr[b ^ 1] += tmp 62 | 63 | arr[b].clear() 64 | b ^= 1 65 | 66 | if not is_boomed: 67 | break 68 | 69 | for i in range(N): 70 | for j in range(N): 71 | beads[i][j] = 0 72 | 73 | # 구슬 변화 74 | nxt = chain.from_iterable((len(list(gr)), bead) for bead, gr in groupby(arr[b])) 75 | for (y, x), val in zip(coords[1:], nxt): 76 | beads[y][x] = val 77 | 78 | print(ans) 79 | -------------------------------------------------------------------------------- /PART 3/Problem 06/2.cpp: -------------------------------------------------------------------------------- 1 | /* 연결리스트와 포인터를 이용한 방법 2 | 연결 리스트를 항상 오름차순으로 연결 리스트를 유지한다. (정렬한 것과 동일함) 3 | 매번 2개의 값을 입력받아 연결리스트 내 어디에 이 값을 어디에 넣을지 위치를 찾아 정렬이 유지되도록 두 값을 넣는다. 4 | it은 항상 중앙값을 가리키고 있는 iterator이다. 위에서 2개의 값을 넣으면서 이 it의 위치를 조정한다. */ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int M, ans[5000]; 12 | list l; 13 | list::iterator it; 14 | 15 | int main() { 16 | int tcN; 17 | scanf("%d", &tcN); 18 | 19 | int ipt1, ipt2; 20 | for (int tc = 0; tc < tcN; ++tc) { 21 | l.clear(); 22 | scanf("%d", &M); 23 | 24 | // 첫번째 수 25 | scanf("%d", &ipt1); 26 | l.emplace_back(ipt1); 27 | ans[0] = ipt1; 28 | it = l.begin(); 29 | 30 | // 2개씩 입력받아 반복 31 | int repeatN = (M - 1) / 2; 32 | for (int i = 1, move = 0; i <= repeatN; ++i, move = 0) { 33 | scanf("%d %d", &ipt1, &ipt2); 34 | if (ipt1 >= *it) ++move; 35 | else --move; 36 | 37 | if (ipt2 >= *it) ++move; 38 | else --move; 39 | 40 | if (*(l.rbegin()) <= ipt1) 41 | l.emplace_back(ipt1); 42 | else 43 | for (auto jt = l.begin(); jt != l.end(); ++jt) 44 | if (ipt1 < *jt) { 45 | l.emplace(jt, ipt1); 46 | break; 47 | } 48 | 49 | if (*(l.rbegin()) <= ipt2) 50 | l.emplace_back(ipt2); 51 | else 52 | for (auto jt = l.begin(); jt != l.end(); ++jt) 53 | if (ipt2 < *jt) { 54 | l.emplace(jt, ipt2); 55 | break; 56 | } 57 | 58 | if (move > 0) ++it; 59 | else if (move < 0) --it; 60 | 61 | ans[i] = *it; 62 | } 63 | 64 | printf("%d", repeatN + 1); 65 | for (int i = 0; i <= repeatN; ++i) 66 | printf("%s%d", i % 10 ? " " : "\n", ans[i]); 67 | 68 | printf("\n"); 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Intellij+all template 2 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 3 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 4 | 5 | # User-specific stuff 6 | .idea/**/workspace.xml 7 | .idea/**/tasks.xml 8 | .idea/**/usage.statistics.xml 9 | .idea/**/dictionaries 10 | .idea/**/shelf 11 | 12 | # AWS User-specific 13 | .idea/**/aws.xml 14 | 15 | # Generated files 16 | .idea/**/contentModel.xml 17 | 18 | # Sensitive or high-churn files 19 | .idea/**/dataSources/ 20 | .idea/**/dataSources.ids 21 | .idea/**/dataSources.local.xml 22 | .idea/**/sqlDataSources.xml 23 | .idea/**/dynamic.xml 24 | .idea/**/uiDesigner.xml 25 | .idea/**/dbnavigator.xml 26 | 27 | # Gradle 28 | .idea/**/gradle.xml 29 | .idea/**/libraries 30 | 31 | # Gradle and Maven with auto-import 32 | # When using Gradle or Maven with auto-import, you should exclude module files, 33 | # since they will be recreated, and may cause churn. Uncomment if using 34 | # auto-import. 35 | # .idea/artifacts 36 | # .idea/compiler.xml 37 | # .idea/jarRepositories.xml 38 | # .idea/modules.xml 39 | # .idea/*.iml 40 | # .idea/modules 41 | # *.iml 42 | # *.ipr 43 | 44 | # CMake 45 | cmake-build-*/ 46 | 47 | # Mongo Explorer plugin 48 | .idea/**/mongoSettings.xml 49 | 50 | # File-based project format 51 | *.iws 52 | 53 | # IntelliJ 54 | out/ 55 | 56 | # mpeltonen/sbt-idea plugin 57 | .idea_modules/ 58 | 59 | # JIRA plugin 60 | atlassian-ide-plugin.xml 61 | 62 | # Cursive Clojure plugin 63 | .idea/replstate.xml 64 | 65 | # SonarLint plugin 66 | .idea/sonarlint/ 67 | 68 | # Crashlytics plugin (for Android Studio and IntelliJ) 69 | com_crashlytics_export_strings.xml 70 | crashlytics.properties 71 | crashlytics-build.properties 72 | fabric.properties 73 | 74 | # Editor-based Rest Client 75 | .idea/httpRequests 76 | 77 | # Android studio 3.1+ serialized cache file 78 | .idea/caches/build_file_checksums.ser 79 | 80 | /.idea/ 81 | /PART 2/Chapter 01/Chapter 01.iml 82 | /PART 2/Chapter 02/Chapter 02.iml 83 | /PART 2/Chapter 05/Chapter 05.iml 84 | /PART 1/PART 1.iml 85 | /PART 2/PART 2.iml 86 | /PART 3/PART 3.iml 87 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 02/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int N, arr[25][25], ans; 8 | 9 | vector > rotateArr(vector > v) { 10 | vector > ret(N); 11 | for (int i = 0; i < N; i++) 12 | ret[i] = vector(N, 0); 13 | 14 | for (int i = 0; i < N; i++) 15 | for (int j = 0; j < N; j++) 16 | ret[j][N - 1 - i] = v[i][j]; 17 | 18 | return ret; 19 | } 20 | 21 | vector > mv(int dir, vector > v) { 22 | // 원하는 방향값만큼 회전시키고서 밀어주는 걸로 대신한다 23 | for (int i = 0; i < dir; i++) 24 | v = rotateArr(v); 25 | 26 | vector > ret(N); 27 | for (int i = 0; i < N; i++) { 28 | // 순서대로 나열한 후 0만 삭제 29 | vector deletedZero = v[i]; 30 | for (int j = N - 1; j >= 0; j--) 31 | if (deletedZero[j] == 0) 32 | deletedZero.erase(deletedZero.begin() + j); 33 | 34 | // 바로 옆 숫자랑 같으면 2 곱한 수를, 아니라면 그냥 자기자신 그대로 35 | int sz = deletedZero.size(); 36 | for (int j = 0; j < sz; j++) 37 | if (j < sz - 1 && deletedZero[j] == deletedZero[j + 1]) { 38 | ret[i].emplace_back(deletedZero[j] * 2); 39 | j++; 40 | } else 41 | ret[i].emplace_back(deletedZero[j]); 42 | 43 | // 남은 자리는 0으로 채운다 44 | int rest = N - ret[i].size(); 45 | for (int j = 0; j < rest; j++) 46 | ret[i].emplace_back(0); 47 | } 48 | 49 | return ret; 50 | } 51 | 52 | void dfs(int mvCnt, vector > v) { 53 | if (mvCnt == 5) { 54 | for (int i = 0; i < N; i++) 55 | for (int j = 0; j < N; j++) 56 | ans = max(ans, v[i][j]); 57 | 58 | return; 59 | } 60 | 61 | for (int dir = 0; dir < 4; dir++) 62 | dfs(mvCnt + 1, mv(dir, v)); 63 | } 64 | 65 | int main() { 66 | scanf("%d", &N); 67 | for (int i = 0; i < N; i++) 68 | for (int j = 0; j < N; j++) 69 | scanf("%d", &arr[i][j]); 70 | 71 | vector > v(N); 72 | for (int i = 0; i < N; i++) 73 | for (int j = 0; j < N; j++) 74 | v[i].emplace_back(arr[i][j]); 75 | 76 | dfs(0, v); 77 | printf("%d\n", ans); 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 01/2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dy = (0, 1, 0, -1) 4 | dx = (1, 0, -1, 0) 5 | 6 | N, M = map(int, input().split()) 7 | board = [list(input()) for _ in range(N)] 8 | sry, srx, sby, sbx = -1, -1, -1, -1 9 | for i in range(N): 10 | for j in range(M): 11 | if board[i][j] == 'R': 12 | sry, srx = i, j 13 | elif board[i][j] == 'B': 14 | sby, sbx = i, j 15 | 16 | 17 | def go_next(ry, rx, by, bx, k): 18 | nry, nrx, nby, nbx = ry, rx, by, bx 19 | 20 | while True: 21 | nry = nry + dy[k] 22 | nrx = nrx + dx[k] 23 | if board[nry][nrx] == 'O': 24 | nry, nrx = -1, -1 25 | break 26 | elif board[nry][nrx] == '#': 27 | nry = nry - dy[k] 28 | nrx = nrx - dx[k] 29 | break 30 | 31 | while True: 32 | nby = nby + dy[k] 33 | nbx = nbx + dx[k] 34 | if board[nby][nbx] == 'O': 35 | nby, nbx = -1, -1 36 | break 37 | elif board[nby][nbx] == '#': 38 | nby = nby - dy[k] 39 | nbx = nbx - dx[k] 40 | break 41 | 42 | # 구슬들이 구멍에 빠지지 않았으면서 충돌했을 경우 43 | if nry != -1 and nry == nby and nrx == nbx: 44 | if k == 0: 45 | if rx < bx: 46 | nrx -= 1 47 | else: 48 | nbx -= 1 49 | elif k == 1: 50 | if ry < by: 51 | nry -= 1 52 | else: 53 | nby -= 1 54 | elif k == 2: 55 | if bx < rx: 56 | nrx += 1 57 | else: 58 | nbx += 1 59 | else: 60 | if by < ry: 61 | nry += 1 62 | else: 63 | nby += 1 64 | 65 | return nry, nrx, nby, nbx 66 | 67 | 68 | def bfs(): 69 | chk = set() 70 | dq = deque() 71 | chk.add((sry, srx, sby, sbx)) 72 | dq.append((sry, srx, sby, sbx, 0)) 73 | while dq: 74 | ry, rx, by, bx, d = dq.popleft() 75 | if by == -1 and bx == -1: # 파란 구슬이 구멍에 빠지면 실패 76 | continue 77 | 78 | if ry == -1 and rx == -1: 79 | return d 80 | 81 | if d < 10: 82 | nd = d + 1 83 | for k in range(4): 84 | nry, nrx, nby, nbx = go_next(ry, rx, by, bx, k) 85 | if (nry, nrx, nby, nbx) not in chk: 86 | chk.add((nry, nrx, nby, nbx)) 87 | dq.append((nry, nrx, nby, nbx, nd)) 88 | 89 | return -1 90 | 91 | 92 | print(bfs()) 93 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 01/1.cpp: -------------------------------------------------------------------------------- 1 | // C++ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int dy[4] = {0, 1, 0, -1}; 10 | const int dx[4] = {1, 0, -1, 0}; 11 | 12 | class State { 13 | public: 14 | State(int ry, int rx, int by, int bx, int m) : ry(ry), rx(rx), by(by), bx(bx), m(m) {}; 15 | 16 | int ry, rx, by, bx, m; 17 | }; 18 | 19 | int N, M, sry, srx, sby, sbx, oy, ox; // 시작 구슬 좌표들과 구멍 좌표 20 | bool chk[10][10][10][10]; // 방문 체크 21 | string board[10]; 22 | 23 | State goNext(const State &now, int k) { 24 | int nry = now.ry, nrx = now.rx; 25 | int nby = now.by, nbx = now.bx; 26 | 27 | for (int mv = 0; mv < 10; mv++) { 28 | nry += dy[k]; 29 | nrx += dx[k]; 30 | if (board[nry][nrx] == '#') { 31 | nry -= dy[k]; 32 | nrx -= dx[k]; 33 | break; 34 | } else if (board[nry][nrx] == 'O') break; 35 | } 36 | 37 | for (int mv = 0; mv < 10; mv++) { 38 | nby += dy[k]; 39 | nbx += dx[k]; 40 | if (board[nby][nbx] == '#') { 41 | nby -= dy[k]; 42 | nbx -= dx[k]; 43 | break; 44 | } else if (board[nby][nbx] == 'O') break; 45 | } 46 | 47 | // 구슬들이 구멍에 빠지지 않았으면서 충돌했을 경우 48 | if (nry == nby && nrx == nbx && (nry != oy || nrx != ox)) { 49 | switch (k) { 50 | case 0: 51 | if (now.rx < now.bx) --nrx; 52 | else --nbx; 53 | break; 54 | case 1: 55 | if (now.ry < now.by) --nry; 56 | else --nby; 57 | break; 58 | case 2: 59 | if (now.rx > now.bx) ++nrx; 60 | else ++nbx; 61 | break; 62 | case 3: 63 | if (now.ry > now.by) ++nry; 64 | else ++nby; 65 | break; 66 | } 67 | } 68 | 69 | return {nry, nrx, nby, nbx, now.m + 1}; 70 | } 71 | 72 | int bfs() { 73 | queue q; 74 | q.emplace(sry, srx, sby, sbx, 0); 75 | chk[sry][srx][sby][sbx] = true; 76 | while (!q.empty()) { 77 | State now = q.front(); 78 | q.pop(); 79 | 80 | if (now.by == oy && now.bx == ox) continue; 81 | 82 | if (now.ry == oy && now.rx == ox) return now.m; 83 | 84 | if (now.m < 10) 85 | for (int k = 0; k < 4; k++) { 86 | State ns = goNext(now, k); 87 | if (!chk[ns.ry][ns.rx][ns.by][ns.bx]) { 88 | q.emplace(ns.ry, ns.rx, ns.by, ns.bx, ns.m); 89 | chk[ns.ry][ns.rx][ns.by][ns.bx] = true; 90 | } 91 | } 92 | } 93 | 94 | return -1; 95 | } 96 | 97 | int main() { 98 | scanf("%d %d", &N, &M); 99 | for (int i = 0; i < N; i++) { 100 | cin >> board[i]; 101 | for (int j = 0; j < M; ++j) 102 | if (board[i][j] == 'R') sry = i, srx = j; 103 | else if (board[i][j] == 'B') sby = i, sbx = j; 104 | else if (board[i][j] == 'O') oy = i, ox = j; 105 | } 106 | 107 | printf("%d\n", bfs()); 108 | 109 | return 0; 110 | } -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 08/2.py: -------------------------------------------------------------------------------- 1 | dy = (0, 1, 0, -1) 2 | dx = (-1, 0, 1, 0) 3 | 4 | N, M = map(int, input().split()) 5 | beads = [[[0] * N for _ in range(N)] for _ in range(2)] 6 | b = 0 7 | for i in range(N): 8 | beads[b][i] = list(map(int, input().split())) 9 | 10 | coords = [(N // 2, N // 2)] 11 | dr = 0 12 | idx = 1 13 | for dist in range(1, N): 14 | for _ in range(2): 15 | for _ in range(dist): 16 | ny = coords[-1][0] + dy[dr] 17 | nx = coords[-1][1] + dx[dr] 18 | coords.append((ny, nx)) 19 | idx += 1 20 | 21 | dr = (dr + 1) % 4 22 | 23 | for _ in range(N - 1): 24 | ny = coords[-1][0] + dy[dr] 25 | nx = coords[-1][1] + dx[dr] 26 | coords.append((ny, nx)) 27 | idx += 1 28 | 29 | dr = (dr + 1) % 4 30 | 31 | ans = 0 32 | for _ in range(M): 33 | # 1. 구슬 파괴 34 | d, s = map(int, input().split()) 35 | if d == 1: 36 | d = 3 37 | elif d == 2: 38 | d = 1 39 | elif d == 3: 40 | d = 0 41 | else: 42 | d = 2 43 | 44 | y, x = coords[0] 45 | for _ in range(1, s + 1): 46 | y = y + dy[d] 47 | x = x + dx[d] 48 | beads[b][y][x] = 0 49 | 50 | while True: 51 | for i in range(N): 52 | for j in range(N): 53 | beads[b ^ 1][i][j] = 0 54 | 55 | # 2. 빈 칸 구슬 이동 56 | idx = 1 57 | ny, nx = coords[idx] 58 | for i in range(1, N ** 2): 59 | y, x = coords[i] 60 | if beads[b][y][x]: 61 | beads[b ^ 1][ny][nx] = beads[b][y][x] 62 | idx += 1 63 | ny, nx = coords[idx] 64 | 65 | b ^= 1 66 | 67 | # 3. 구슬 연속 4개 폭발 68 | is_there_move = False 69 | y, x = coords[1] 70 | bead = beads[b][y][x] 71 | cnt = 1 72 | for i in range(2, N ** 2): 73 | y, x = coords[i] 74 | if beads[b][y][x] == bead: 75 | cnt += 1 76 | else: 77 | if cnt >= 4: 78 | for j in range(i - 1, i - 1 - cnt, -1): 79 | ny, nx = coords[j] 80 | ans += beads[b][ny][nx] 81 | beads[b][ny][nx] = 0 82 | is_there_move = True 83 | 84 | bead = beads[b][y][x] 85 | cnt = 1 86 | 87 | if bead == 0: 88 | break 89 | 90 | if not is_there_move: 91 | break 92 | 93 | for i in range(N): 94 | for j in range(N): 95 | beads[b ^ 1][i][j] = 0 96 | 97 | # 4. 구슬이 변화하는 단계 98 | y, x = coords[1] 99 | bead = beads[b][y][x] 100 | cnt = 1 101 | idx = 1 102 | ny, nx = coords[idx] 103 | for i in range(2, N ** 2): 104 | y, x = coords[i] 105 | if beads[b][y][x] == bead: 106 | cnt += 1 107 | else: 108 | beads[b ^ 1][ny][nx] = cnt 109 | idx += 1 110 | if idx < len(coords): 111 | ny, nx = coords[idx] 112 | else: 113 | break 114 | 115 | beads[b ^ 1][ny][nx] = bead 116 | idx += 1 117 | if idx < len(coords): 118 | ny, nx = coords[idx] 119 | else: 120 | break 121 | 122 | bead = beads[b][y][x] 123 | cnt = 1 124 | 125 | if bead == 0: 126 | break 127 | 128 | b ^= 1 129 | 130 | print(ans) 131 | -------------------------------------------------------------------------------- /PART 4/Chapter 01/Problem 06/1.py: -------------------------------------------------------------------------------- 1 | dy = (0, 1, 0, -1) 2 | dx = (1, 0, -1, 0) 3 | 4 | N, M = map(int, input().split()) 5 | board = [list(map(int, input().split())) for _ in range(N)] 6 | score = 0 7 | groups = [[0] * N for _ in range(N)] 8 | group_idx = 0 9 | sz = 0 10 | rainbow = 0 11 | block_color = 0 12 | 13 | 14 | def is_valid_coord(y, x): 15 | return 0 <= y < N and 0 <= x < N 16 | 17 | 18 | def recur(y, x): 19 | global sz, rainbow 20 | for k in range(4): 21 | ny = y + dy[k] 22 | nx = x + dx[k] 23 | if is_valid_coord(ny, nx) and groups[ny][nx] == 0: 24 | if board[ny][nx] == 0: 25 | sz += 1 26 | rainbow += 1 27 | groups[ny][nx] = group_idx 28 | recur(ny, nx) 29 | elif board[ny][nx] == block_color: 30 | sz += 1 31 | groups[ny][nx] = group_idx 32 | recur(ny, nx) 33 | 34 | 35 | def recur_remove(y, x): 36 | for k in range(4): 37 | ny = y + dy[k] 38 | nx = x + dx[k] 39 | if is_valid_coord(ny, nx) and board[ny][nx] >= 0 and (board[ny][nx] == 0 or groups[ny][nx] == group_idx): 40 | board[ny][nx] = -2 41 | recur_remove(ny, nx) 42 | 43 | 44 | def pull_down(): 45 | for j in range(N): 46 | for i in range(N - 1, -1, -1): 47 | if board[i][j] == -2: 48 | for k in range(i - 1, -1, -1): 49 | if board[k][j] == -1: 50 | break 51 | 52 | if board[k][j] >= 0: 53 | board[i][j] = board[k][j] 54 | board[k][j] = -2 55 | break 56 | 57 | 58 | def rotate(): 59 | next_board = [[0] * N for _ in range(N)] 60 | for i in range(N): 61 | for j in range(N): 62 | next_board[i][j] = board[j][N - 1 - i] 63 | 64 | for i in range(N): 65 | for j in range(N): 66 | board[i][j] = next_board[i][j] 67 | 68 | 69 | while True: 70 | groups = [[0] * N for _ in range(N)] 71 | group_idx = 0 72 | candi = [] 73 | for i in range(N): 74 | for j in range(N): 75 | if groups[i][j] == 0: 76 | if board[i][j] < 0: 77 | groups[i][j] = board[i][j] 78 | continue 79 | 80 | if board[i][j] > 0: 81 | # 블록 개수가 2보다 크거나 같은지 체크 82 | cnt = 1 83 | for k in range(4): 84 | ni = i + dy[k] 85 | nj = j + dx[k] 86 | if is_valid_coord(ni, nj) and (board[ni][nj] == 0 or board[ni][nj] == board[i][j]): 87 | cnt += 1 88 | 89 | if cnt < 2: 90 | continue 91 | 92 | group_idx += 1 93 | sz = 0 94 | rainbow = 0 95 | block_color = board[i][j] 96 | groups[i][j] = group_idx 97 | sz += 1 98 | recur(i, j) 99 | candi.append((sz, rainbow, i, j, group_idx)) 100 | 101 | # 무지개 블록은 탐색기록 초기화 102 | for y in range(N): 103 | for x in range(N): 104 | if board[y][x] == 0: 105 | groups[y][x] = 0 106 | 107 | if group_idx == 0: 108 | break 109 | 110 | candi.sort(key=lambda x: (-x[0], -x[1], -x[2], -x[3])) 111 | 112 | score += candi[0][0] ** 2 113 | 114 | group_idx = candi[0][4] 115 | board[candi[0][2]][candi[0][3]] = -2 116 | recur_remove(candi[0][2], candi[0][3]) 117 | 118 | pull_down() 119 | rotate() 120 | pull_down() 121 | 122 | print(score) 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 한권으로 합격하는 취업 코딩테스트 2 | 3 | [<한권으로 합격하는 취업 코딩테스트>](https://search.shopping.naver.com/book/catalog/38507250642) 정오표 및 코드 4 | 5 | thumbnail 6 | 7 | - 책 구매 링크: [시대에듀](https://www.sdedu.co.kr/book/item.php?it_id=1666771947), [NAVER 쇼핑](https://search.shopping.naver.com/book/catalog/38507250642), [YES24](https://www.yes24.com/Product/Goods/117863776), [교보문고](https://product.kyobobook.co.kr/detail/S000201209716), [알라딘](http://aladin.kr/p/nzaIK), [영풍문고](https://www.ypbooks.co.kr/book.yp?bookcd=101226136), [11번가](http://www.11st.co.kr/products/5587601859), [공부서점](https://book.conects.com/product/bookDetail?goods_id=0100023660479) 8 | - 각 문제를 C++, Python, Kotlin, Swift로 푼 각각의 코드를 제공 예정입니다. 9 | 10 | *** 11 | 12 | ## PART 1 코딩테스트 준비 어떻게 해야 하나요? 13 | 14 | ### CHAPTER 01 코딩테스트란? 15 | 16 | ##### 알고리즘 대회와 기업 코딩테스트 17 | 18 | ##### 코딩테스트 대비하기 19 | 20 | - 기본예제 [A+B](https://boj.kr/1000) 21 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%201/Chapter%2001/Problem%2001/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%201/Chapter%2001/Problem%2001/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%201/Chapter%2001/Problem%2001/3.kt), [Swift](https://github.com/ydh0213/coding-test-book/blob/main/PART%201/Chapter%2001/Problem%2001/4.swift) 22 | 23 | ##### 언어 고르기 24 | 25 | ### CHAPTER 02 코딩테스트 출제 경향 26 | 27 | ##### 기업별 출제경향 28 | 29 | ### CHAPTER 03 코딩테스트 채점 기준 30 | 31 | ##### 시간 복잡도 32 | 33 | ##### 공간 복잡도 34 | 35 | ### CHAPTER 04 문제 해결 시작하기 36 | 37 | ##### 코딩 테스트에 필요한 프로그래밍 기본 지식 38 | 39 | ##### 입출력 40 | 41 | ##### 빠른 입출력 42 | 43 | ##### 자료형 44 | 45 | ##### 변수명 46 | 47 | ##### 리스트 컴프리헨션 List Comprehension 48 | 49 | ##### 삼항 연산자 50 | 51 | ## PART 2 알고리즘 유형분석 52 | 53 | ### CHAPTER 01 자료구조 54 | 55 | ##### 코딩 테스트에 필요한 자료구조 56 | 57 | ##### 배열 Array 58 | 59 | ##### 연결 리스트 Linked List 60 | 61 | ##### 배열과 연결 리스트 62 | 63 | - 필수예제 [요세푸스 문제 0](https://boj.kr/11866) 64 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2001/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2001/2.py), Kotlin, Swift 65 | 66 | ##### 스택 Stack 67 | 68 | - 필수예제 [괄호](https://boj.kr/9012) 69 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2002/1.py), Kotlin, Swift 70 | 71 | ##### 큐 Queue 72 | 73 | - 필수예제 [카드2](https://boj.kr/2164) 74 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2003/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2003/2.py), Kotlin, Swift 75 | 76 | ##### 우선순위 큐 Priority Queue 77 | 78 | ##### 최대힙과 최소힙 79 | 80 | - 필수예제 [절댓값 힙](https://boj.kr/11286) 81 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2004/1.cpp), [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2004/2.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2004/3.py), Kotlin, Swift 82 | 83 | ##### 맵 Map 84 | 85 | - 필수예제 [베스트셀러](https://boj.kr/1302) 86 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2005/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2005/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2005/3.kt), Swift 87 | 88 | ##### 집합 Set 89 | 90 | - 필수예제 [회사에 있는 사람](https://boj.kr/7785) 91 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2001/Problem%2006/1.py), Kotlin, Swift 92 | 93 | --- 94 | 95 | - 연습문제 01 [키로거](https://boj.kr/5397) 96 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2001/1.cpp), [Python(덱)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2001/2.py), [Python(스택)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2001/3.py), Kotlin, Swift 97 | - 연습문제 02 [후위표기식2](https://boj.kr/1935) 98 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2002/1.py), Kotlin, Swift 99 | - 연습문제 03 [N번째 큰 수](http://boj.kr/2075) 100 | [C++(정렬)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2003/1.cpp), [C++(우선순위 큐)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2003/2.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2003/3.py), Kotlin, Swift 101 | 102 | --- 103 | 104 | ### CHAPTER 02 완전탐색 105 | 106 | ##### 브루트 포스 Brute-force 107 | 108 | ##### 순열 permutation 109 | 110 | ##### 조합 combination 111 | 112 | - 필수예제 [백설 공주와 일곱 난쟁이](https://boj.kr/3040) 113 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2001/1.cpp), [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2001/2.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2001/3.py), Kotlin, Swift 114 | - 필수예제 [유레카 이론](https://boj.kr/10448) 115 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2002/1.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2002/2.kt), Swift 116 | - 필수예제 [사탕 게임](https://boj.kr/3085) 117 | C++, [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2003/1.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2003/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2003/3.kt), Swift 118 | 119 | ### CHAPTER 03 탐욕법 120 | 121 | - 필수예제 [회의실 배정](https://boj.kr/1931) 122 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2004/1.cpp), [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2004/2.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2004/3.py), Kotlin, Swift 123 | - 필수예제 [수리공 항승](https://boj.kr/1449) 124 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2005/1.py), Kotlin, Swift 125 | 126 | ### CHAPTER 04 DFS, BFS, 백트래킹 127 | 128 | ##### 그래프 Graph 129 | 130 | ##### 그래프의 방향성 131 | 132 | ##### 그래프의 순환성 133 | 134 | ##### 그래프의 연결 요소 Connected Component 135 | 136 | ##### 트리 Tree 137 | 138 | ##### 그래프를 코드로 나타내는 방법 139 | 140 | 1. 인접 행렬 Adjacency Matrix 141 | 2. 인접 리스트 Adjacency List 142 | 143 | ##### 인접 행렬과 인접 리스트 144 | 145 | ##### DFS Depth First Search 146 | 147 | ##### BFS Breadth First Search 148 | 149 | ##### DFS/BFS의 시간 복잡도 150 | 151 | ##### 길찾기 문제 152 | 153 | - 필수예제 [연결 요소의 개수](https://boj.kr/11724) 154 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2006/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2006/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2006/3.kt), Swift 155 | - 필수예제 [미로 탐색](https://boj.kr/2178) 156 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2007/1.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2007/2.kt), Swift 157 | 158 | ##### 백트래킹 Backtracking 159 | 160 | - 필수예제 [알파벳](https://boj.kr/1987) 161 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2008/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2008/2.py), Kotlin, [Swift](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2002/Problem%2008/3.swift) 162 | 163 | --- 164 | 165 | - 연습문제 04 [음식물 피하기](http://boj.kr/1743) 166 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2004/1.py), Kotlin, Swift 167 | - 연습문제 05 [나이트의 이동](https://boj.kr/7562) 168 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2005/1.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2005/2.kt), Swift 169 | - 연습문제 06 [N-Queen](https://boj.kr/9663) 170 | C++, [PyPy3](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2006/1.py), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2006/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2006/3.kt), Swift 171 | 172 | --- 173 | 174 | ### CHAPTER 05 이분탐색 175 | 176 | ##### 선형 탐색 Linear Search 177 | 178 | ##### 이분 탐색 Binary Search 179 | 180 | ##### 매개변수 탐색 Parametric Search 181 | 182 | - 필수예제 [나무 자르기](https://boj.kr/2805) 183 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2001/1.py), Kotlin, Swift 184 | - 필수예제 [숫자 카드 2](https://boj.kr/10816) 185 | C++, [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2002/1.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2002/2.py), Kotlin, [Swift](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2002/3.swift) 186 | 187 | ### CHAPTER 06 동적계획법 188 | 189 | ##### 메모이제이션 Memoization 190 | 191 | ##### 타뷸레이션 Tabulation 192 | 193 | ##### Top-down vs Bottom-up 194 | 195 | - 필수예제 [1로 만들기](https://boj.kr/1463) 196 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2003/1.cpp), [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2003/2.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2003/3.py), [Python(방법3)](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2003/4.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2003/5.kt), Swift 197 | - 필수예제 [2×n 타일링](http://boj.kr/11726) 198 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2004/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2004/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2004/3.kt), [Swift](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2004/4.swift) 199 | - 필수예제 [스티커](https://boj.kr/9465) 200 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2005/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2005/2.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Chapter%2005/Problem%2005/3.kt), Swift 201 | 202 | --- 203 | 204 | - 연습문제 07 [기타레슨](https://boj.kr/2343) 205 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2007/1.py), Kotlin, Swift 206 | - 연습문제 08 [제곱수의 합](https://boj.kr/1699) 207 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2008/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2008/2.py), Kotlin, Swift 208 | - 연습문제 09 [가장 큰 증가 부분 수열](http://boj.kr/11055) 209 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%202/Practice%2009/1.py), Kotlin, Swift 210 | 211 | --- 212 | 213 | ## PART 3 알고리즘 핵심문제 20 214 | 215 | - 핵심문제 01 [외계인의 기타 연주](https://boj.kr/2841) 216 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2001/1.py), Kotlin, Swift 217 | - 핵심문제 02 [캠핑](http://boj.kr/4796) 218 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2002/1.py), Kotlin, Swift 219 | - 핵심문제 03 [치킨 배달](http://boj.kr/15686) 220 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2003/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2003/2.py), Kotlin, Swift 221 | - 핵심문제 04 [케빈 베이컨의 6단계 법칙](http://boj.kr/1389) 222 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2004/1.py), [Kotlin](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2004/2.kt), Swift 223 | - 핵심문제 05 [가장 큰 정사각형](http://boj.kr/1915) 224 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2005/1.py), Kotlin, Swift 225 | - 핵심문제 06 [중앙값 구하기](https://boj.kr/2696) 226 | [C++(정렬)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2006/1.cpp), [C++(연결리스트)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2006/2.cpp), [Python(정렬)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2006/3.py), [Python(우선순위 큐)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2006/4.py), Kotlin, Swift 227 | - 핵심문제 07 [연산자 끼워 넣기](http://boj.kr/14888) 228 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2007/1.cpp), [Python(순열, 집합)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2007/2.py), [Python(BFS)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2007/3.py), Kotlin, Swift 229 | - 핵심문제 08 [팰린드롬 만들기](https://boj.kr/1213) 230 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2008/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2008/2.py), Kotlin, Swift 231 | - 핵심문제 09 [오큰수](https://boj.kr/17298) 232 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2009/1.py), Kotlin, Swift 233 | - 핵심문제 10 [사전](https://boj.kr/1256) 234 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2010/1.py), Kotlin, Swift 235 | - 핵심문제 11 [수열 정렬](https://boj.kr/1015) 236 | C++, [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2011/1.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2011/2.py), Kotlin, Swift 237 | - 핵심문제 12 [숫자](https://boj.kr/1131) 238 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2012/1.py), Kotlin, Swift 239 | - 핵심문제 13 [차이를 최대로](https://boj.kr/10819) 240 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2013/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2013/2.py), Kotlin, Swift 241 | - 핵심문제 14 [보물](https://boj.kr/1026) 242 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2014/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2014/2.py), Kotlin, Swift 243 | - 핵심문제 15 [주사위](https://boj.kr/1041) 244 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2015/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2015/2.py), Kotlin, Swift 245 | - 핵심문제 16 [순열장난](https://boj.kr/10597) 246 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2016/1.py), Kotlin, Swift 247 | - 핵심문제 17 [멀티탭 스케줄링](https://boj.kr/1700) 248 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2017/1.py), Kotlin, Swift 249 | - 핵심문제 18 [상범 빌딩](https://boj.kr/6593) 250 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2018/1.py), Kotlin, Swift 251 | - 핵심문제 19 [좋은 단어](https://boj.kr/3986) 252 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2019/1.py), Kotlin, Swift 253 | - 핵심문제 20 [귀농](https://boj.kr/1184) 254 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%203/Problem%2020/1.py), Kotlin, Swift 255 | 256 | ## PART 4 삼성ㆍ카카오 기출문제 257 | 258 | ### CHAPTER 01 삼성전자 역량테스트 259 | 260 | - 기출문제 01 [구슬 탈출 2](http://boj.kr/13460) 261 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2001/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2001/2.py), Kotlin, Swift 262 | - 기출문제 02 [2048(Easy)](https://boj.kr/12100) 263 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2002/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2002/2.py), Kotlin, Swift 264 | - 기출문제 03 [뱀](http://boj.kr/3190) 265 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2003/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2003/2.py), Kotlin, Swift 266 | - 기출문제 04 [주사위 굴리기](http://boj.kr/14499) 267 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2004/1.py), Kotlin, Swift 268 | - 기출문제 05 [상어 초등학교](http://boj.kr/21608) 269 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2005/1.py), Kotlin, Swift 270 | - 기출문제 06 [상어 중학교](http://boj.kr/21609) 271 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2006/1.py), Kotlin, Swift 272 | - 기출문제 07 [마법사 상어와 비바라기](http://boj.kr/21610) 273 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2007/1.py), Kotlin, Swift 274 | - 기출문제 08 [마법사 상어와 블리자드](http://boj.kr/21611) 275 | C++, [Python(방법1)](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2008/1.py), [Python(방법2)](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2001/Problem%2008/2.py), Kotlin, Swift 276 | 277 | ### CHAPTER 02 카카오 블라인드 코딩테스트 278 | 279 | - 기출문제 01 [다트 게임](https://programmers.co.kr/learn/courses/30/lessons/17682) 280 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2001/1.py), Kotlin, Swift 281 | - 기출문제 02 [오픈채팅방](https://programmers.co.kr/learn/courses/30/lessons/42888) 282 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2002/1.py), Kotlin, Swift 283 | - 기출문제 03 [실패율](https://programmers.co.kr/learn/courses/30/lessons/42889) 284 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2003/1.py), Kotlin, Swift 285 | - 기출문제 04 [문자열 압축](https://programmers.co.kr/learn/courses/30/lessons/60057) 286 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2004/1.py), Kotlin, Swift 287 | - 기출문제 05 [괄호 변환](https://programmers.co.kr/learn/courses/30/lessons/60058) 288 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2005/1.py), Kotlin, Swift 289 | - 기출문제 06 [메뉴 리뉴얼](https://programmers.co.kr/learn/courses/30/lessons/72411) 290 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2006/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2006/2.py), Kotlin, Swift 291 | - 기출문제 07 [순위 검색](https://programmers.co.kr/learn/courses/30/lessons/72412) 292 | [C++](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2007/1.cpp), [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2007/2.py), Kotlin, Swift 293 | - 기출문제 08 [숫자 문자열과 영단어](https://programmers.co.kr/learn/courses/30/lessons/81301) 294 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2008/1.py), Kotlin, Swift 295 | - 기출문제 09 [거리두기 확인하기](https://programmers.co.kr/learn/courses/30/lessons/81302) 296 | C++, [Python](https://github.com/ydh0213/coding-test-book/blob/main/PART%204/Chapter%2002/Problem%2009/1.py), Kotlin, Swift 297 | --------------------------------------------------------------------------------