From 43841807ded4fb1026ff938d38d832ca4246dc7c Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 10 Mar 2022 15:43:46 +0900 Subject: [PATCH 01/55] =?UTF-8?q?=EB=89=B4=EC=8A=A4=20=ED=8B=80=EB=9F=AC?= =?UTF-8?q?=EC=8A=A4=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\355\230\270 \353\263\200\355\231\230.py" | 34 +++++++++++++++++++ ...54\354\212\244\355\204\260\353\247\201.py" | 29 ++++++++++++++++ ...4 \353\246\254\353\211\264\354\226\274.py" | 25 ++++++++++++++ ...4 \353\202\230\353\210\204\352\270\260.py" | 27 +++++++++++++++ ...4\354\210\230 \354\204\270\352\270\260.py" | 18 ++++++++++ 5 files changed, 133 insertions(+) create mode 100644 "programmers/kakao/\352\264\204\355\230\270 \353\263\200\355\231\230.py" create mode 100644 "programmers/kakao/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" create mode 100644 "programmers/kakao/\353\251\224\353\211\264 \353\246\254\353\211\264\354\226\274.py" create mode 100644 "programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.py" create mode 100644 "programmers/\354\277\274\353\223\234\354\225\225\354\266\225 \355\233\204 \352\260\234\354\210\230 \354\204\270\352\270\260.py" diff --git "a/programmers/kakao/\352\264\204\355\230\270 \353\263\200\355\231\230.py" "b/programmers/kakao/\352\264\204\355\230\270 \353\263\200\355\231\230.py" new file mode 100644 index 0000000..8c345ba --- /dev/null +++ "b/programmers/kakao/\352\264\204\355\230\270 \353\263\200\355\231\230.py" @@ -0,0 +1,34 @@ +from collections import deque + + +def solution(p): + if p == '': + return p + u, v = '', '' + l, r = 0, 0 + for i, c in enumerate(p): + if c == '(': + l += 1 + else: + r += 1 + if l == r: + u = p[:i + 1] + v = p[i + 1:] + break + q = deque([]) + for c in u: + q.append(c) + while len(q) > 1: + b, a = q.pop(), q.pop() + if a != '(' or b != ')': + q.append(a), q.append(b) + break + if not q: + return u + solution(v) + x = '(' + solution(v) + ')' + for c in u[1:len(u) - 1]: + if c == '(': + x += ')' + else: + x += '(' + return x diff --git "a/programmers/kakao/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" "b/programmers/kakao/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" new file mode 100644 index 0000000..0bcc855 --- /dev/null +++ "b/programmers/kakao/\353\211\264\354\212\244 \355\201\264\353\237\254\354\212\244\355\204\260\353\247\201.py" @@ -0,0 +1,29 @@ +import re +from collections import defaultdict + + +def solution(str1, str2): + answer = 0 + str1 = re.sub('[^a-z]', ' ', str1.lower()) + str2 = re.sub('[^a-z]', ' ', str2.lower()) + d1 = defaultdict(int) + d2 = defaultdict(int) + elements = set() + for i in range(len(str1) - 1): + tmp = str1[i:i + 2] + if not ' ' in tmp: + d1[tmp] += 1 + elements.add(tmp) + for i in range(len(str2) - 1): + tmp = str2[i:i + 2] + if not ' ' in tmp: + d2[tmp] += 1 + elements.add(tmp) + inter = 0 + union = 0 + for e in elements: + inter += min(d1[e], d2[e]) + union += max(d1[e], d2[e]) + if not inter and not union: + return 65536 + return int(inter / union * 65536) diff --git "a/programmers/kakao/\353\251\224\353\211\264 \353\246\254\353\211\264\354\226\274.py" "b/programmers/kakao/\353\251\224\353\211\264 \353\246\254\353\211\264\354\226\274.py" new file mode 100644 index 0000000..b45d57c --- /dev/null +++ "b/programmers/kakao/\353\251\224\353\211\264 \353\246\254\353\211\264\354\226\274.py" @@ -0,0 +1,25 @@ +from itertools import combinations +from collections import defaultdict + + +def solution(orders, course): + answer = [] + combi = defaultdict(int) + sorted_orders = [sorted(" ".join(order).split()) for order in orders] + for order in sorted_orders: + for n in course: + tmp = map(tuple, combinations(order, n)) + for s in tmp: + combi[s] += 1 + for n in course: + m = 0 + for k, v in combi.items(): + if len(k) == n: + m = max(m, v) + if m < 2: + continue + for k, v in combi.items(): + if len(k) == n and v == m: + answer.append("".join(k)) + answer.sort() + return answer diff --git "a/programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.py" "b/programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.py" new file mode 100644 index 0000000..d6bec3d --- /dev/null +++ "b/programmers/\354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.py" @@ -0,0 +1,27 @@ +from collections import deque + + +def solution(n, wires): + answer = 10 ** 10 + edges = [[False] * (n + 1) for _ in range(n + 1)] + for i, j in wires: + edges[i][j] = True + edges[j][i] = True + for i, j in wires: + edges[i][j] = False + edges[j][i] = False + visited = [False] * (n + 1) + q = deque([i]) + while q: + cur = q.popleft() + if visited[cur]: + continue + visited[cur] = True + for nei in range(1, n + 1): + if edges[cur][nei]: + q.append(nei) + N = visited.count(True) + answer = min(answer, abs(N - (n - N))) + edges[i][j] = True + edges[j][i] = True + return answer \ No newline at end of file diff --git "a/programmers/\354\277\274\353\223\234\354\225\225\354\266\225 \355\233\204 \352\260\234\354\210\230 \354\204\270\352\270\260.py" "b/programmers/\354\277\274\353\223\234\354\225\225\354\266\225 \355\233\204 \352\260\234\354\210\230 \354\204\270\352\270\260.py" new file mode 100644 index 0000000..3b945d6 --- /dev/null +++ "b/programmers/\354\277\274\353\223\234\354\225\225\354\266\225 \355\233\204 \352\260\234\354\210\230 \354\204\270\352\270\260.py" @@ -0,0 +1,18 @@ +def solution(arr): + narr = dfs(len(arr), arr, 0, 0) + return [narr.count(0), narr.count(1)] + + +def dfs(l, arr, x, y): + if l == 0: + return [arr[x][y]] + ll = l // 2 + nran = [(x, y), (x + ll, y), (x, y + ll), (x + ll, y + ll)] + ret = [] + for xx, yy in nran: + ret += dfs(ll, arr, xx, yy) + if ret == [1, 1, 1, 1]: + return [1] + elif ret == [0, 0, 0, 0]: + return [0] + return ret From 25c6cfa74face020eb1a76b079c66ccd56bed857 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 11 Mar 2022 15:35:56 +0900 Subject: [PATCH 02/55] =?UTF-8?q?=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=88=9C=EC=9C=84=EA=B2=80=EC=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\354\235\270\355\225\230\352\270\260.py" | 27 ++++++++++++++++ ...5 \354\265\234\353\214\200\355\231\224.py" | 22 +++++++++++++ ...4\354\234\204 \352\262\200\354\203\211.py" | 32 +++++++++++++++++++ .../kakao/\355\212\234\355\224\214.py" | 9 ++++++ 4 files changed, 90 insertions(+) create mode 100644 "programmers/kakao/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" create mode 100644 "programmers/kakao/\354\210\230\354\213\235 \354\265\234\353\214\200\355\231\224.py" create mode 100644 "programmers/kakao/\354\210\234\354\234\204 \352\262\200\354\203\211.py" create mode 100644 "programmers/kakao/\355\212\234\355\224\214.py" diff --git "a/programmers/kakao/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" "b/programmers/kakao/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" new file mode 100644 index 0000000..14055f5 --- /dev/null +++ "b/programmers/kakao/\352\261\260\353\246\254\353\221\220\352\270\260 \355\231\225\354\235\270\355\225\230\352\270\260.py" @@ -0,0 +1,27 @@ +def solution(places): + answer = [] + for place in places: + answer.append(check(place)) + return answer + + +def check(place): + for i in range(5): + for j in range(5): + if place[i][j] != 'P': + continue + if 0<=j+1<5 and place[i][j+1] == 'P': # 바로 오른쪽이 사람일 때 + return 0 + if 0<=i+1<5 and place[i+1][j] == 'P': # 바로 아래가 사람일 때 + return 0 + if 0<=i+1<5 and 0<=j+1<5 and place[i+1][j+1] == 'P': # 오른쪽 아래 대각선이 사람일 때 + if place[i+1][j] == 'O' or place[i][j+1] == 'O': + return 0 + if 0<=i+1<5 and 0<=j-1<5 and place[i+1][j-1] == 'P': # 왼쪽 아래 대각선이 사람일 때 + if place[i+1][j] == 'O' or place[i][j-1] == 'O': + return 0 + if 0<=i+2<5 and place[i+2][j] == 'P' and place[i+1][j] == 'O':# 두칸 밑이 사람일 때 + return 0 + if 0<=j+2<5 and place[i][j+2] == 'P' and place[i][j+1] == 'O':# 두칸 오른쪽이 사람일 때 + return 0 + return 1 \ No newline at end of file diff --git "a/programmers/kakao/\354\210\230\354\213\235 \354\265\234\353\214\200\355\231\224.py" "b/programmers/kakao/\354\210\230\354\213\235 \354\265\234\353\214\200\355\231\224.py" new file mode 100644 index 0000000..8ffb5a4 --- /dev/null +++ "b/programmers/kakao/\354\210\230\354\213\235 \354\265\234\353\214\200\355\231\224.py" @@ -0,0 +1,22 @@ +import re + + +def solution(expression): + ex = re.sub(r"([^0-9])", r" \1 ", expression).split() + return dfs(ex, ['*','+','-']) + + +def dfs(ex, oper): + if len(ex) == 1: + return abs(int(ex[0])) + ret = 0 + for i in range(len(oper)): + tmp = ex[:] + k = 1 + while k < len(tmp): + if tmp[k] == oper[i]: + tmp = tmp[:k-1]+[str(eval(tmp[k-1]+tmp[k]+tmp[k+1]))]+tmp[k+2:] + continue + k += 2 + ret = max(ret, dfs(tmp, oper[:i]+oper[i+1:])) + return ret \ No newline at end of file diff --git "a/programmers/kakao/\354\210\234\354\234\204 \352\262\200\354\203\211.py" "b/programmers/kakao/\354\210\234\354\234\204 \352\262\200\354\203\211.py" new file mode 100644 index 0000000..e519eee --- /dev/null +++ "b/programmers/kakao/\354\210\234\354\234\204 \352\262\200\354\203\211.py" @@ -0,0 +1,32 @@ +from collections import defaultdict + + +def solution(info, query): + answer = [] + d = defaultdict(list) + for i in range(len(info)): + info[i] = info[i].split() + info[i][4] = int(info[i][4]) + info.sort(key = lambda x: (-x[4])) + for i in info: + for l in [i[0], '-']: + for j in [i[1], '-']: + for e in [i[2], '-']: + for f in [i[3], '-']: + d[l, j, e, f].append(i[4]) + for i in range(len(query)): + query[i] = query[i].replace('and ', '').split() + query[i][4] = int(query[i][4]) + for la, j, e, f, s in query: + l, r = 0, len(d[la, j, e, f])-1 + while l <= r: + mid = (l+r)//2 + if d[la, j, e, f][mid] >= s: + l = mid+1 + else: + r = mid-1 + if l >= len(d[la, j, e, f]) or d[la, j, e, f][l] < s: + answer.append(l) + else: + answer.append(r) + return answer \ No newline at end of file diff --git "a/programmers/kakao/\355\212\234\355\224\214.py" "b/programmers/kakao/\355\212\234\355\224\214.py" new file mode 100644 index 0000000..a7da32f --- /dev/null +++ "b/programmers/kakao/\355\212\234\355\224\214.py" @@ -0,0 +1,9 @@ +def solution(s): + answer = [] + e = eval('[' + s[1:len(s) - 1] + ']') + e.sort(key=len) + cmp = set() + for i in e: + answer.append((i - cmp).pop()) + cmp = i + return answer From f0f3c3348882b867d0411ba8b63450f9931f48d5 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 11 Mar 2022 16:00:59 +0900 Subject: [PATCH 03/55] README.md --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d8d92e..827d87a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ -# Coding_Test - preparation for Coding_test +# Algorithm Study +*** + +사용 언어는 주로 Python + + 1. /baekjoon: 백준 플랫폼 문제 모음 + 2. /programmers: 프로그래머스 플랫폼 문제 From e74cf3a7871fcd74d1a52be90391051fde528d17 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 11 Mar 2022 16:08:23 +0900 Subject: [PATCH 04/55] README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 827d87a..ead6be7 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,7 @@ 사용 언어는 주로 Python - 1. /baekjoon: 백준 플랫폼 문제 모음 +BFS, DFS, 그리디, DP, 이분탐색 등 다양한 알고리즘 문제 풀이 +###문제 + 1. /baekjoon: 백준 플랫폼 문제 2. /programmers: 프로그래머스 플랫폼 문제 From c6d9b64eaefc9772197db8855b430bd9ba781d75 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 11 Mar 2022 16:09:41 +0900 Subject: [PATCH 05/55] README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ead6be7..9967ecb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ 사용 언어는 주로 Python -BFS, DFS, 그리디, DP, 이분탐색 등 다양한 알고리즘 문제 풀이 +BFS, DFS, 그리디, DP, 이분탐색 등 다양한 알고리즘 문제 풀이 + ###문제 1. /baekjoon: 백준 플랫폼 문제 2. /programmers: 프로그래머스 플랫폼 문제 From 2920ef8bf532fdfbe3173ed7c18954c71bdfdbeb Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 11 Mar 2022 16:11:17 +0900 Subject: [PATCH 06/55] README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9967ecb..1512670 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,8 @@ BFS, DFS, 그리디, DP, 이분탐색 등 다양한 알고리즘 문제 풀이 ###문제 + 1. /baekjoon: 백준 플랫폼 문제 + 2. /programmers: 프로그래머스 플랫폼 문제 +*** \ No newline at end of file From fb055ff357f761e013f861831bd69ffd6c88b8df Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 11 Mar 2022 16:11:46 +0900 Subject: [PATCH 07/55] README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1512670..c603307 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ BFS, DFS, 그리디, DP, 이분탐색 등 다양한 알고리즘 문제 풀이 -###문제 +### 문제 1. /baekjoon: 백준 플랫폼 문제 From 679417f0091b93773ec1be33eb4b68762cdeb499 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sat, 12 Mar 2022 23:50:18 +0900 Subject: [PATCH 08/55] =?UTF-8?q?=ED=9B=84=EB=B3=B4=ED=82=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "programmers/kakao/\355\233\204\353\263\264\355\202\244.py" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "programmers/kakao/\355\233\204\353\263\264\355\202\244.py" diff --git "a/programmers/kakao/\355\233\204\353\263\264\355\202\244.py" "b/programmers/kakao/\355\233\204\353\263\264\355\202\244.py" new file mode 100644 index 0000000..e69de29 From 60f643f32ddedea0c35e1ac35f716900d0f4fe2e Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 13 Mar 2022 00:17:36 +0900 Subject: [PATCH 09/55] =?UTF-8?q?=ED=9B=84=EB=B3=B4=ED=82=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\355\233\204\353\263\264\355\202\244.py" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/programmers/kakao/\355\233\204\353\263\264\355\202\244.py" "b/programmers/kakao/\355\233\204\353\263\264\355\202\244.py" index e69de29..aecf098 100644 --- "a/programmers/kakao/\355\233\204\353\263\264\355\202\244.py" +++ "b/programmers/kakao/\355\233\204\353\263\264\355\202\244.py" @@ -0,0 +1,24 @@ +def solution(relation): + answer = 0 + trn = list(zip(*relation)) + candis = [] + for i in range(1, 2 ** len(trn)): + tmp = set() + for j in range(len(trn)): + if i & 1: + tmp.add(j) + i >>= 1 + if len(tmp) == 0: + continue + for c in candis: + if c & tmp == c: + break + else: + cols = [] + for col in tmp: + cols.append(trn[col]) + candi = list(zip(*cols)) + if len(candi) == len(set(candi)): + answer += 1 + candis.append(tmp) + return answer From 54c2fc28641fe3c950d2863b850bf17cf9822cdb Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 13 Mar 2022 17:35:00 +0900 Subject: [PATCH 10/55] =?UTF-8?q?=ED=94=84=EB=A0=8C=EC=A6=884=EB=B8=94?= =?UTF-8?q?=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++++ .idea/Coding_Test.iml | 8 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ .idea/misc.xml | 4 +++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 ++++ ...4\354\246\2104\353\270\224\353\241\235.py" | 28 +++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Coding_Test.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 "programmers/kakao/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.py" diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/Coding_Test.iml b/.idea/Coding_Test.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/Coding_Test.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7ba73c2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d19d702 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/programmers/kakao/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.py" "b/programmers/kakao/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.py" new file mode 100644 index 0000000..b0265ee --- /dev/null +++ "b/programmers/kakao/\355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.py" @@ -0,0 +1,28 @@ +def solution(m, n, board): + answer = 0 + for i in range(m): + board[i] = " ".join(board[i]).split() + board = list(map(list, zip(*board))) + res = 1 + while res: + res = find_block(board, m, n) + answer += res + return answer + + +def find_block(board, m, n): + empty = set() + for i in range(n - 1): + for j in range(m - 1): + if not board[i][j]: + continue + if board[i][j] == board[i][j + 1] and board[i][j] == board[i + 1][j] and board[i][j] == board[i + 1][j + 1]: + empty.add((i, j)) + empty.add((i + 1, j)) + empty.add((i, j + 1)) + empty.add((i + 1, j + 1)) + for i, j in empty: + board[i][j] = 0 + for b in board: + b.sort(key=lambda x: 0 if x == 0 else 1) + return len(empty) From a500acb5995a671ec066b92be5733679d3b5fa32 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 13 Mar 2022 17:46:16 +0900 Subject: [PATCH 11/55] =?UTF-8?q?.idea=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -- .idea/Coding_Test.iml | 8 -- .../inspectionProfiles/profiles_settings.xml | 6 - .idea/misc.xml | 4 - .idea/modules.xml | 8 -- .idea/vcs.xml | 6 - .idea/workspace.xml | 103 ++++++++++++++++++ 7 files changed, 103 insertions(+), 40 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/Coding_Test.iml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/Coding_Test.iml b/.idea/Coding_Test.iml deleted file mode 100644 index d0876a7..0000000 --- a/.idea/Coding_Test.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 7ba73c2..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index d19d702..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..377e65e --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1646376000578 + + + + + + + + + + + + \ No newline at end of file From 17a1c5fe63b3698ee1a034e2bbcf03204662f3cb Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 13 Mar 2022 17:47:35 +0900 Subject: [PATCH 12/55] .idea --- .idea/workspace.xml | 103 -------------------------------------------- 1 file changed, 103 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 377e65e..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,103 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1646376000578 - - - - - - - - - - - - \ No newline at end of file From f0c08689b5f42f66d6132183a88a3b3c642ce42b Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 13 Mar 2022 18:18:21 +0900 Subject: [PATCH 13/55] =?UTF-8?q?=EC=BA=90=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kakao/\354\272\220\354\213\234.py" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "programmers/kakao/\354\272\220\354\213\234.py" diff --git "a/programmers/kakao/\354\272\220\354\213\234.py" "b/programmers/kakao/\354\272\220\354\213\234.py" new file mode 100644 index 0000000..c8a01bc --- /dev/null +++ "b/programmers/kakao/\354\272\220\354\213\234.py" @@ -0,0 +1,18 @@ +def solution(cacheSize, cities): + if cacheSize == 0: + return len(cities) * 5 + answer = 0 + cache = [] + for city in cities: + if city.lower() in cache: + answer += 1 + cache.remove(city.lower()) + cache.append(city.lower()) + else: + answer += 5 + if len(cache) < cacheSize: + cache.append(city.lower()) + else: + cache.pop(0) + cache.append(city.lower()) + return answer From 29d32177bf7803990059953d392ea22d45939ed1 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 13 Mar 2022 19:03:46 +0900 Subject: [PATCH 14/55] =?UTF-8?q?=EB=B0=A9=EA=B8=88=EA=B7=B8=EA=B3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...51\352\270\210\352\267\270\352\263\241.py" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "programmers/kakao/\353\260\251\352\270\210\352\267\270\352\263\241.py" diff --git "a/programmers/kakao/\353\260\251\352\270\210\352\267\270\352\263\241.py" "b/programmers/kakao/\353\260\251\352\270\210\352\267\270\352\263\241.py" new file mode 100644 index 0000000..466bd1c --- /dev/null +++ "b/programmers/kakao/\353\260\251\352\270\210\352\267\270\352\263\241.py" @@ -0,0 +1,26 @@ +import re + + +def solution(m, musicinfos): + answer = '' + ans_time = 0 + m = re.findall(r'[A-Z]#*', m) + for music in musicinfos: + music = music.split(',') + music[0] = music[0].split(':') + music[1] = music[1].split(':') + start = int(music[0][0]) * 60 + int(music[0][1]) + end = int(music[1][0]) * 60 + int(music[1][1]) + time = end - start + title = music[2] + mel = re.findall(r'[A-Z]#*', music[3]) + total_mel = mel * (time // len(mel)) + mel[:(time % len(mel))] + for i in range(time - len(m) + 1): + if total_mel[i:i + len(m)] == m: + if time > ans_time: + answer = music[2] + ans_time = time + break + if answer == '': + return "(None)" + return answer From 17287d800376c10e16931ce0e0e0fcba4e996aef Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Mon, 14 Mar 2022 22:02:42 +0900 Subject: [PATCH 15/55] =?UTF-8?q?=EC=95=95=EC=B6=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "programmers/kakao/\354\225\225\354\266\225.py" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "programmers/kakao/\354\225\225\354\266\225.py" diff --git "a/programmers/kakao/\354\225\225\354\266\225.py" "b/programmers/kakao/\354\225\225\354\266\225.py" new file mode 100644 index 0000000..60734ee --- /dev/null +++ "b/programmers/kakao/\354\225\225\354\266\225.py" @@ -0,0 +1,16 @@ +from collections import deque + + +def solution(msg): + answer = [] + d = {chr(ord('A') + i): i + 1 for i in range(26)} + msg = deque(" ".join(msg).split()) + s = '' + while msg: + s += msg.popleft() + if not s in d: + d[s] = len(d) + 1 + answer.append(d[s[:-1]]) + s = s[-1] + answer.append(d[s]) + return answer From dfdd1af1748f079dbf7a2a64e13d360a9e98e1cb Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Tue, 15 Mar 2022 20:20:16 +0900 Subject: [PATCH 16/55] 220315 --- ...0 \352\265\254\355\225\230\352\270\260.py" | 27 +++++++++++++++++++ ...4\354\210\230 \352\262\214\354\236\204.py" | 24 +++++++++++++++++ ...4\353\252\205 \354\240\225\353\240\254.py" | 13 +++++++++ 3 files changed, 64 insertions(+) create mode 100644 "programmers/kakao/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" create mode 100644 "programmers/kakao/n\354\247\204\354\210\230 \352\262\214\354\236\204.py" create mode 100644 "programmers/kakao/\355\214\214\354\235\274\353\252\205 \354\240\225\353\240\254.py" diff --git "a/programmers/kakao/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" "b/programmers/kakao/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" new file mode 100644 index 0000000..1262020 --- /dev/null +++ "b/programmers/kakao/k\354\247\204\354\210\230\354\227\220\354\204\234 \354\206\214\354\210\230 \352\260\234\354\210\230 \352\265\254\355\225\230\352\270\260.py" @@ -0,0 +1,27 @@ +import re + + +def solution(n, k): + knum = (re.sub(r'0+', r'0', convert(n, k).strip('0'))).split('0') + nums = list(map(int, knum)) + visited = [False] * (int(max(nums)**(1/2))+1) + for i in range(2, len(visited)): + if visited[i]: + continue + for j in range(i*2, len(visited), i): + visited[j] = True + rm = [] + for num in nums: + if num == 1 or (num % i == 0 and num > i): + rm.append(num) + for num in rm: + nums.remove(num) + return len(nums) + + +def convert(n, k): + ans = '' + while n: + ans = str(n%k) + ans + n //= k + return ans \ No newline at end of file diff --git "a/programmers/kakao/n\354\247\204\354\210\230 \352\262\214\354\236\204.py" "b/programmers/kakao/n\354\247\204\354\210\230 \352\262\214\354\236\204.py" new file mode 100644 index 0000000..648431c --- /dev/null +++ "b/programmers/kakao/n\354\247\204\354\210\230 \352\262\214\354\236\204.py" @@ -0,0 +1,24 @@ +def solution(n, t, m, p): + answer = '' + chrs = '0123456789ABCDEF' + num = 0 + cur = 0 + while len(answer) < t: + ret = convert_num(num, n, chrs) + num += 1 + for i in range(p, cur+len(ret)+1, m): + if i-cur-1 < 0: + continue + answer += ret[i-cur-1] + cur = (cur+len(ret)) % m + return answer[:t] + + +def convert_num(num, n, chrs): + ret = '' + while True: + ret = chrs[num % n] + ret + num //= n + if num == 0: + break + return ret \ No newline at end of file diff --git "a/programmers/kakao/\355\214\214\354\235\274\353\252\205 \354\240\225\353\240\254.py" "b/programmers/kakao/\355\214\214\354\235\274\353\252\205 \354\240\225\353\240\254.py" new file mode 100644 index 0000000..b2a4c69 --- /dev/null +++ "b/programmers/kakao/\355\214\214\354\235\274\353\252\205 \354\240\225\353\240\254.py" @@ -0,0 +1,13 @@ +import re + + +def solution(files): + answer = [] + t = [] + for f in files: + a = re.match(r'([^0-9]+)([0-9]{1,5})([0-9a-zA-Z .-]*)', f) + t.append(a.groups()) + t.sort(key=lambda x: (x[0].lower(), int(x[1]))) + for s in t: + answer.append(s[0] + s[1] + s[2]) + return answer From 3dc1af93d988772ec9d9826f67083503e97d1187 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Wed, 16 Mar 2022 02:19:26 +0900 Subject: [PATCH 17/55] =?UTF-8?q?=EC=A3=BC=EC=B0=A8=20=EC=9A=94=EA=B8=88?= =?UTF-8?q?=20=EA=B3=84=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\352\270\210 \352\263\204\354\202\260.py" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "programmers/kakao/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" diff --git "a/programmers/kakao/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" "b/programmers/kakao/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" new file mode 100644 index 0000000..6d40022 --- /dev/null +++ "b/programmers/kakao/\354\243\274\354\260\250 \354\232\224\352\270\210 \352\263\204\354\202\260.py" @@ -0,0 +1,23 @@ +from collections import defaultdict +from math import ceil + + +def solution(fees, records): + d = defaultdict(list) + btime, bfee, utime, ufee = fees + for record in records: + time, car, inout = record.split() + hh, mm = time.split(':') + hh, mm = int(hh) * 60, int(mm) + d[car].append(hh + mm) + ans = [] + for car in sorted(d.keys()): + if len(d[car]) % 2 == 1: + d[car].append(23 * 60 + 59) + time = 0 + while d[car]: + b, a = d[car].pop(), d[car].pop() + time += b - a + fee = bfee + ceil(max(0, (time - btime)) / utime) * ufee + ans.append(fee) + return ans From d80faf62fd237591b7e5e3e6610f2c9747a057a2 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Wed, 16 Mar 2022 21:00:32 +0900 Subject: [PATCH 18/55] =?UTF-8?q?=EC=96=91=EA=B6=81=EB=8C=80=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...21\352\266\201\353\214\200\355\232\214.py" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "programmers/kakao/\354\226\221\352\266\201\353\214\200\355\232\214.py" diff --git "a/programmers/kakao/\354\226\221\352\266\201\353\214\200\355\232\214.py" "b/programmers/kakao/\354\226\221\352\266\201\353\214\200\355\232\214.py" new file mode 100644 index 0000000..4791953 --- /dev/null +++ "b/programmers/kakao/\354\226\221\352\266\201\353\214\200\355\232\214.py" @@ -0,0 +1,30 @@ +def solution(n, info): + apeach = sum([10 - i for i in range(11) if info[i]]) + score = [(10 - i) * 2 if info[i] else 10 - i for i in range(11)] + req = [i + 1 for i in info] + + def dfs(n, depth, arr): + if depth == 10: + ret = arr[:] + ret[10] += n + return ret, sum([score[i] for i in range(11) if ret[i]]) + s1 = 0 + if n >= req[depth]: + tmp_arr = arr[:] + tmp_arr[depth] = req[depth] + arr1, s1 = dfs(n - req[depth], depth + 1, tmp_arr) + arr2, s2 = dfs(n, depth + 1, arr[:]) + if s1 > s2: + return arr1, s1 + elif s1 == s2: + for j in range(10, -1, -1): + if arr1[j] > arr2[j]: + return arr1, s1 + elif arr1[j] < arr2[j]: + return arr2, s2 + return arr2, s2 + + answer, ryan = dfs(n, 0, [0] * 11) + if apeach >= ryan: + return [-1] + return answer From bab5734fcc1757c65661f76f7860da59ba86e450 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 17 Mar 2022 03:57:38 +0900 Subject: [PATCH 19/55] =?UTF-8?q?dp=202=EB=AC=B8=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/11053.py | 15 +++++++++++++++ baekjoon/23630.py | 10 ++++++++++ 2 files changed, 25 insertions(+) create mode 100644 baekjoon/11053.py create mode 100644 baekjoon/23630.py diff --git a/baekjoon/11053.py b/baekjoon/11053.py new file mode 100644 index 0000000..96add99 --- /dev/null +++ b/baekjoon/11053.py @@ -0,0 +1,15 @@ +n = int(input()) +m = [0] + list(map(int, input().split())) +dp = [0] +s = [0] +for i in range(1, n+1): + if m[i] > s[-1]: + dp.append(len(s)) + s.append(m[i]) + else: + for j in range(i): + if s[j] >= m[i]: + s[j] = m[i] + dp.append(j) + break +print(len(s)-1) \ No newline at end of file diff --git a/baekjoon/23630.py b/baekjoon/23630.py new file mode 100644 index 0000000..8bc3c83 --- /dev/null +++ b/baekjoon/23630.py @@ -0,0 +1,10 @@ +n = int(input()) +m = list(map(int, input().split())) +cnts = [0]*20 +for num in m: + d = 1 + for i in range(20): + if num & d: + cnts[i] += 1 + d <<= 1 +max(cnts) From 934929720c6291e72b4ea43ef600f6ecc7853960 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 17 Mar 2022 18:47:35 +0900 Subject: [PATCH 20/55] =?UTF-8?q?=EC=9E=85=EA=B5=AD=EC=8B=AC=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...205\352\265\255\354\213\254\354\202\254.py" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "programmers/\354\236\205\352\265\255\354\213\254\354\202\254.py" diff --git "a/programmers/\354\236\205\352\265\255\354\213\254\354\202\254.py" "b/programmers/\354\236\205\352\265\255\354\213\254\354\202\254.py" new file mode 100644 index 0000000..7826bec --- /dev/null +++ "b/programmers/\354\236\205\352\265\255\354\213\254\354\202\254.py" @@ -0,0 +1,18 @@ +def solution(n, times): + l, r = 0, 10 ** 14 + while l <= r: + mid = (l + r) // 2 + if check(n, mid, times): + r = mid - 1 + else: + l = mid + 1 + return l + + +def check(n, t, times): + p = 0 + for time in times: + p += t // time + if p >= n: + return True + return False From 1c798a0d025f22e1f7a6bb6670ae96f9f9ee1e34 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 18 Mar 2022 18:47:28 +0900 Subject: [PATCH 21/55] =?UTF-8?q?N=EC=9C=BC=EB=A1=9C=20=ED=91=9C=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\241\234 \355\221\234\355\230\204.py" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "programmers/N\354\234\274\353\241\234 \355\221\234\355\230\204.py" diff --git "a/programmers/N\354\234\274\353\241\234 \355\221\234\355\230\204.py" "b/programmers/N\354\234\274\353\241\234 \355\221\234\355\230\204.py" new file mode 100644 index 0000000..45cce83 --- /dev/null +++ "b/programmers/N\354\234\274\353\241\234 \355\221\234\355\230\204.py" @@ -0,0 +1,21 @@ +def solution(N, number): + answer = 0 + dp = [] + for i in range(8): + tmp = [int(str(N)*(i+1))] + if dp: + for j in range(i): + for k in dp[j]: + for l in dp[i-j-1]: + if l != 0: + tmp.append(k//l) + if k != 0: + tmp.append(l//k) + tmp.append(k*l) + tmp.append(k+l) + tmp.append(l-k) + tmp.append(k-l) + dp.append(list(set(tmp))) + if number in dp[-1]: + return i+1 + return -1 \ No newline at end of file From 4559ab17ebeea2ea737936346e62c56ff23a0fbe Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sat, 19 Mar 2022 22:55:18 +0900 Subject: [PATCH 22/55] =?UTF-8?q?=EA=B0=80=EC=9E=A5=20=EB=A8=BC=20?= =?UTF-8?q?=EB=85=B8=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... \353\250\274 \353\205\270\353\223\234.py" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "programmers/\352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.py" diff --git "a/programmers/\352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.py" "b/programmers/\352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.py" new file mode 100644 index 0000000..f3c60b2 --- /dev/null +++ "b/programmers/\352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.py" @@ -0,0 +1,25 @@ +from heapq import heappush, heappop +from collections import defaultdict + + +def solution(n, edge): + answer = 0 + q = [(0, 1)] + neis = defaultdict(list) + for a, b in edge: + neis[a].append(b) + neis[b].append(a) + dist = [float('inf') for _ in range(n + 1)] + while q: + d, cur = heappop(q) + if d >= dist[cur]: + continue + dist[cur] = d + for nei in neis[cur]: + heappush(q, (d + 1, nei)) + dist[0] = 0 + m = max(dist) + for i in range(1, n + 1): + if dist[i] == m: + answer += 1 + return answer From 1ada95d992dea5e84222a3e072e46ffa1a9a12e6 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 20 Mar 2022 02:17:54 +0900 Subject: [PATCH 23/55] N-Queen --- programmers/N-Queen.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 programmers/N-Queen.py diff --git a/programmers/N-Queen.py b/programmers/N-Queen.py new file mode 100644 index 0000000..f7948a2 --- /dev/null +++ b/programmers/N-Queen.py @@ -0,0 +1,22 @@ +def solution(n): + answer = 0 + chess = [[0] * n for _ in range(n)] + return dfs(0, n, chess) + + +def dfs(row, n, chess): + if row == n: + return 1 + ret = 0 + for i in range(n): + if chess[row][i]: + continue + chesstmp = [chess[j][:] for j in range(n)] + for j in range(row + 1, n): + chesstmp[j][i] = 1 + if 0 <= i + j - row < n: + chesstmp[j][i + j - row] = 1 + if 0 <= i - j + row < n: + chesstmp[j][i - j + row] = 1 + ret += dfs(row + 1, n, chesstmp) + return ret From a2eb7a3b8b5a7c799fbd50f91816a67e6255e417 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Mon, 21 Mar 2022 20:00:40 +0900 Subject: [PATCH 24/55] =?UTF-8?q?=EC=88=9C=EC=9C=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "programmers/\354\210\234\354\234\204.py" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "programmers/\354\210\234\354\234\204.py" diff --git "a/programmers/\354\210\234\354\234\204.py" "b/programmers/\354\210\234\354\234\204.py" new file mode 100644 index 0000000..e69de29 From 699693d1c9dd04924107bac7c435e58680b852bd Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Tue, 22 Mar 2022 23:41:11 +0900 Subject: [PATCH 25/55] 16235 --- baekjoon/16235.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 baekjoon/16235.py diff --git a/baekjoon/16235.py b/baekjoon/16235.py new file mode 100644 index 0000000..fdbf548 --- /dev/null +++ b/baekjoon/16235.py @@ -0,0 +1,38 @@ +N, M, K = map(int, input().split()) +A = [] +namu = [[[] for _ in range(N)] for i in range(N)] +land = [[5] * N for _ in range(N)] +for i in range(N): + A.append(list(map(int, input().split()))) +for i in range(M): + x, y, z = map(int, input().split()) + namu[x - 1][y - 1].append(z) +mv = [(0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)] +for _ in range(K): + # spring + for i in range(N): + for j in range(N): + namu[i][j].sort() # 어린 순 + for k in range(len(namu[i][j])): + if namu[i][j][k] <= land[i][j]: + land[i][j] -= namu[i][j][k] + namu[i][j][k] += 1 + else: + for l in range(k, len(namu[i][j])): # summer + land[i][j] += namu[i][j].pop() // 2 + break + for i in range(N): # fall + for j in range(N): + for k in range(len(namu[i][j])): + if namu[i][j][k] % 5 == 0: + for x, y in mv: + if 0 <= i + x < N and 0 <= j + y < N: + namu[i + x][j + y].append(1) + for i in range(N): # winter + for j in range(N): + land[i][j] += A[i][j] +answer = 0 +for i in range(N): + for j in range(N): + answer += len(namu[i][j]) +print(answer) \ No newline at end of file From 1ebd8af21f4de4336e1d3d44c4e9d192b993473a Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Wed, 23 Mar 2022 03:28:51 +0900 Subject: [PATCH 26/55] 16235 --- baekjoon/16236.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 baekjoon/16236.py diff --git a/baekjoon/16236.py b/baekjoon/16236.py new file mode 100644 index 0000000..e69de29 From cc364185384fc4f493088097a854296eb38099f8 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 24 Mar 2022 23:07:30 +0900 Subject: [PATCH 27/55] =?UTF-8?q?=ED=95=98=EB=85=B8=EC=9D=B4=EC=9D=98=20?= =?UTF-8?q?=ED=83=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\270\354\235\264\354\235\230 \355\203\221.py" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 "programmers/\355\225\230\353\205\270\354\235\264\354\235\230 \355\203\221.py" diff --git "a/programmers/\355\225\230\353\205\270\354\235\264\354\235\230 \355\203\221.py" "b/programmers/\355\225\230\353\205\270\354\235\264\354\235\230 \355\203\221.py" new file mode 100644 index 0000000..1bde489 --- /dev/null +++ "b/programmers/\355\225\230\353\205\270\354\235\264\354\235\230 \355\203\221.py" @@ -0,0 +1,15 @@ +global answer +def solution(n): + global answer + answer = [] + hanoi(n, 1, 2, 3) + return answer +def hanoi(n, src, mid, des): + if n == 1: + global answer + answer.append([src, des]) + return + hanoi(n-1, src, des, mid) + hanoi(1, src, mid, des) + hanoi(n-1, mid, src, des) + return \ No newline at end of file From 009f95022e06a3b24679a26e4bd860085d900416 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 25 Mar 2022 01:35:45 +0900 Subject: [PATCH 28/55] 16236 --- baekjoon/16235.py | 52 ++++++++++++++++++++++++++---------------- baekjoon/16236.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 19 deletions(-) diff --git a/baekjoon/16235.py b/baekjoon/16235.py index fdbf548..14e6fad 100644 --- a/baekjoon/16235.py +++ b/baekjoon/16235.py @@ -1,38 +1,52 @@ N, M, K = map(int, input().split()) A = [] -namu = [[[] for _ in range(N)] for i in range(N)] +namu = [[{} for _ in range(N)] for i in range(N)] land = [[5] * N for _ in range(N)] for i in range(N): A.append(list(map(int, input().split()))) for i in range(M): x, y, z = map(int, input().split()) - namu[x - 1][y - 1].append(z) + if z in namu[x - 1][y - 1]: + namu[x - 1][y - 1][z] += 1 + else: + namu[x - 1][y - 1][z] = 1 mv = [(0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)] + for _ in range(K): # spring for i in range(N): for j in range(N): - namu[i][j].sort() # 어린 순 - for k in range(len(namu[i][j])): - if namu[i][j][k] <= land[i][j]: - land[i][j] -= namu[i][j][k] - namu[i][j][k] += 1 + tmp = {} + die = 0 + for age, cnt in sorted(namu[i][j].items()): + l = (land[i][j] // age) + if cnt <= l: + land[i][j] -= age * cnt + tmp[age + 1] = cnt + elif 1 <= l: + land[i][j] -= l * age + tmp[age + 1] = l + die += (age // 2) * (cnt - l) else: - for l in range(k, len(namu[i][j])): # summer - land[i][j] += namu[i][j].pop() // 2 - break + die += (age // 2) * cnt + namu[i][j] = tmp + land[i][j] += die for i in range(N): # fall for j in range(N): - for k in range(len(namu[i][j])): - if namu[i][j][k] % 5 == 0: + for age, cnt in namu[i][j].items(): + if age % 5 == 0: for x, y in mv: - if 0 <= i + x < N and 0 <= j + y < N: - namu[i + x][j + y].append(1) - for i in range(N): # winter - for j in range(N): - land[i][j] += A[i][j] + ii = i + x + jj = j + y + if 0 <= ii < N and 0 <= jj < N: + if namu[ii][jj].get(1): + namu[ii][jj][1] += cnt + else: + namu[ii][jj][1] = cnt + land[i][j] += A[i][j] # winter answer = 0 for i in range(N): for j in range(N): - answer += len(namu[i][j]) -print(answer) \ No newline at end of file + for k in namu[i][j].values(): + answer += k +print(answer) diff --git a/baekjoon/16236.py b/baekjoon/16236.py index e69de29..0d3fc97 100644 --- a/baekjoon/16236.py +++ b/baekjoon/16236.py @@ -0,0 +1,57 @@ +from collections import deque +from sys import stdin +N = int(stdin.readline()) +fishes = [] +for i in range(N): + col = list(map(int, stdin.readline().split())) + for j, fish in enumerate(col): + if fish == 9: + baby = [i, j] + col[j] = 0 + fishes.append(col) +mv = [(0, 1), (1, 0), (0, -1), (-1, 0)] +bsize = 2 +bcnt = 0 + +def bfs(): + visited = [[False] * N for _ in range(N)] + q = deque([(0, baby[0], baby[1])]) + maxp = 10000 + candi = [] + while q: + p, x, y = q.popleft() + if visited[x][y]: + continue + if maxp < p: + break + elif 0 < fishes[x][y] < bsize: + maxp = p + candi.append((x, y, p)) + visited[x][y] = True + for i, j in mv: + xx = x + i + yy = y + j + if 0 <= xx < N and 0 <= yy < N and fishes[xx][yy] <= bsize and not visited[xx][yy]: + q.append((p+1, xx, yy)) + if not candi: + return False + candi.sort() + return candi[0] + + +answer = 0 +while True: + ret = bfs() + if not ret: + break + x, y, p = ret + answer += p + baby = [x, y] + fishes[x][y] = 0 + bcnt += 1 + if bcnt == bsize: + bsize += 1 + bcnt = 0 + + +print(answer) From 16d17878ae3e923fa6ce2ada7168b04815c61fb8 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 25 Mar 2022 02:04:25 +0900 Subject: [PATCH 29/55] 16236 --- baekjoon/16236.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/baekjoon/16236.py b/baekjoon/16236.py index 0d3fc97..cf5a69f 100644 --- a/baekjoon/16236.py +++ b/baekjoon/16236.py @@ -13,6 +13,7 @@ bsize = 2 bcnt = 0 + def bfs(): visited = [[False] * N for _ in range(N)] q = deque([(0, baby[0], baby[1])]) @@ -54,4 +55,4 @@ def bfs(): bcnt = 0 -print(answer) +print(answer) \ No newline at end of file From 8c69a19400a7617d30d0460e901e7efca075822a Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sat, 26 Mar 2022 03:06:07 +0900 Subject: [PATCH 30/55] 17140 --- baekjoon/17140.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 baekjoon/17140.py diff --git a/baekjoon/17140.py b/baekjoon/17140.py new file mode 100644 index 0000000..1c79b79 --- /dev/null +++ b/baekjoon/17140.py @@ -0,0 +1,42 @@ +from collections import defaultdict +R, C, K = map(int, input().split()) +R, C = R - 1, C - 1 +A = [list(map(int, input().split())) for _ in range(3)] + +def oper(A): + l = 0 + for i in range(len(A)): + cnt_dict = defaultdict(int) + for a in A[i]: + if a != 0: + cnt_dict[a] += 1 + cnt_pairs = [(c, n) for n, c in cnt_dict.items()] + cnt_pairs.sort() + A[i] = [] + for c, n in cnt_pairs: + A[i].append(n) + A[i].append(c) + if len(A[i]) > 100: + break + l = max(l, len(A[i])) + for row in A: + for j in range(max(0, l-len(row))): + row.append(0) + return + + +for i in range(100): + if 0 <= R < len(A) and 0 <= C < len(A[0]) and A[R][C] == K: + print(i) + break + if len(A) >= len(A[0]): + oper(A) + else: + A = list(map(list, zip(*A))) + oper(A) + A = list(map(list, zip(*A))) +else: + if 0 <= R < len(A) and 0 <= C < len(A[0]) and A[R][C] == K: + print(100) + else: + print(-1) \ No newline at end of file From 4c436b7145d68334acb2f1ad9b2fde6a7b0f09ef Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 27 Mar 2022 04:13:28 +0900 Subject: [PATCH 31/55] 17779 --- baekjoon/17779.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 baekjoon/17779.py diff --git a/baekjoon/17779.py b/baekjoon/17779.py new file mode 100644 index 0000000..09ebe1f --- /dev/null +++ b/baekjoon/17779.py @@ -0,0 +1,37 @@ +N = int(input()) +A = [list(map(int, input().split())) for _ in range(N)] +total = 0 +for i in range(N): + total += sum(A[i]) + +def divide(x, y, d1, d2): + popul = [0, 0, 0, 0] + lx, ly = x+d1, y-d1 + rx, ry = x+d2, y+d2 + bx, by = x+d1+d2, y-d1+d2 + for i in range(x): + popul[0] += sum(A[i][:y+1]) + popul[1] += sum(A[i][y+1:]) + for i in range(x, lx): + popul[0] += sum(A[i][:y-i+x]) + for i in range(x, rx+1): + popul[1] += sum(A[i][y+1+i-x:]) + for i in range(lx, bx+1): + popul[2] += sum(A[i][:ly-lx+i]) + for i in range(rx+1, bx+1): + popul[3] += sum(A[i][ry+rx+1-i:]) + for i in range(bx+1, N): + popul[2] += sum(A[i][:by]) + popul[3] += sum(A[i][by:]) + popul.append(total-sum(popul)) + return max(popul) - min(popul) + + +ret = float('inf') +for x in range(N - 2): + for y in range(1, N - 1): + for d1 in range(1, y): + for d2 in range(1, min(N - y, N-(x+d1))): + ret = min(ret, divide(x, y, d1, d2)) + +print(ret) From c353468c66bbcc1188b8978ffd4cffe4ac18754b Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 27 Mar 2022 20:41:55 +0900 Subject: [PATCH 32/55] 17143 --- baekjoon/17143.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 baekjoon/17143.py diff --git a/baekjoon/17143.py b/baekjoon/17143.py new file mode 100644 index 0000000..cb0d1c5 --- /dev/null +++ b/baekjoon/17143.py @@ -0,0 +1,49 @@ +R, C, M = map(int, input().split()) +sharks = [[False for i in range(C)] for _ in range(R)] +for i in range(M): + r, c, s, d, z = map(int, input().split()) + sharks[r - 1][c - 1] = (s, d - 1, z) +ans = 0 + + +def cal_mv(sharks): + new_pos = [[False for i in range(C)] for _ in range(R)] + for i in range(R): + for j in range(C): + if not sharks[i][j]: + continue + s, d, z = sharks[i][j] + if d == 0 or d == 1: + if d == 0: + p = -s + i + else: + p = s + i + d = (p // (R - 1) + d) % 2 + p %= (2 * (R - 1)) + if p > (R - 1): + p = 2 * (R - 1) - p + if not new_pos[p][j] or new_pos[p][j][2] < z: + new_pos[p][j] = (s, d, z) + else: + if d == 3: + p = -s + j + else: + p = s + j + if (p // (C - 1)) % 2: + d = (d - 1) % 2 + 2 + p %= (2 * (C - 1)) + if p > (C - 1): + p = 2 * (C - 1) - p + if not new_pos[i][p] or new_pos[i][p][2] < z: + new_pos[i][p] = (s, d, z) + return new_pos + + +for c in range(C): + for i in range(R): + if sharks[i][c]: + ans += sharks[i][c][2] + sharks[i][c] = False + break + sharks = cal_mv(sharks) +print(ans) From 6ac7da7badd85e3982dba3e3595cf4f70b0a2b91 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Mon, 28 Mar 2022 16:55:54 +0900 Subject: [PATCH 33/55] 17822, 17837 --- baekjoon/17822.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ baekjoon/17837.py | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 baekjoon/17822.py create mode 100644 baekjoon/17837.py diff --git a/baekjoon/17822.py b/baekjoon/17822.py new file mode 100644 index 0000000..f8f0913 --- /dev/null +++ b/baekjoon/17822.py @@ -0,0 +1,57 @@ +from collections import deque + + +N, M, T = map(int, input().split()) +circle = [list(map(int, input().split())) for _ in range(N)] +multiple = {i+1: [] for i in range(N)} +mv = [(1, 0), (-1, 0), (0, 1), (0, -1)] +for i in range(1, N+1): + for j in range(1, N//i + 1): + multiple[i].append(i*j-1) + + +def swap(n, d, k): + res = 0 + if d == 0: # 0 clockwise + k = M - k + for i in multiple[n]: # 숫자를 swap + circle[i] = circle[i][k:] + circle[i][:k] + candi = set() + for i in range(N): + for j in range(M): + if circle[i][j] > 0 and circle[i][j] == circle[i][j-1]: + candi.add((i, j)) + candi.add((i, j-1)) + for i in range(N-1): + for j in range(M): + if circle[i][j] > 0 and circle[i][j] == circle[i+1][j]: + candi.add((i, j)) + candi.add((i+1, j)) + for i, j in candi: + circle[i][j] = 0 + if not candi: + nums = [] + for i in range(N): + for j in range(M): + if circle[i][j] > 0: + nums.append(circle[i][j]) + mean = sum(nums) / len(nums) + for i in range(N): + for j in range(M): + if circle[i][j] > mean: + circle[i][j] -= 1 + elif 0 < circle[i][j] < mean: + circle[i][j] += 1 + for i in range(N): # 원판의 숫자를 sum + res += sum(circle[i]) + return res + + +for i in range(T): + n, d, k = map(int, input().split()) + res = swap(n, d, k) + if not res: + print(res) + break +else: + print(res) \ No newline at end of file diff --git a/baekjoon/17837.py b/baekjoon/17837.py new file mode 100644 index 0000000..aa78be7 --- /dev/null +++ b/baekjoon/17837.py @@ -0,0 +1,42 @@ +N, K = map(int, input().split()) +board = [list(map(int, input().split())) for _ in range(N)] +pos = [[[] for _ in range(N)] for _ in range(N)] +pieces = [] +t = [0, 0, 2, 1, 3] +for k in range(K): + r, c, m = map(int, input().split()) + pieces.append([r-1, c-1, t[m]]) + pos[r-1][c-1].append(k) +mv = [(0, 1), (-1, 0), (0, -1), (1, 0)] + + +def play(): + for k in range(K): + r, c, m = pieces[k] + rr, cc = r + mv[m][0], c + mv[m][1] + if not (0 <= rr < N and 0 <= cc < N) or board[rr][cc] == 2: # 밖에 나가는 것과 파란색일 case + rr, cc = rr - 2 * mv[m][0], cc - 2 * mv[m][1] + pieces[k][2] = (m + 2) % 4 + if not (0 <= rr < N and 0 <= cc < N) or board[rr][cc] == 2: # 한번더 밖에 나가는 것과 파란색일 case + pieces[k][2] = (m + 2) % 4 + continue + idx = pos[r][c].index(k) + tmp = pos[r][c][idx:] + pos[r][c] = pos[r][c][:idx] + if board[rr][cc]: + tmp = list(reversed(tmp)) + for p in tmp: + pieces[p][0] = rr + pieces[p][1] = cc + pos[rr][cc].append(p) + if len(pos[rr][cc]) > 3: + return True + return False + + +for i in range(1000): + if play(): + print(i+1) + break +else: + print(-1) \ No newline at end of file From ad42b5d3e192bd909deb6524859fe77a4a3e01ff Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Wed, 30 Mar 2022 01:17:25 +0900 Subject: [PATCH 34/55] 20061 --- baekjoon/20061.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 baekjoon/20061.py diff --git a/baekjoon/20061.py b/baekjoon/20061.py new file mode 100644 index 0000000..88b9ea4 --- /dev/null +++ b/baekjoon/20061.py @@ -0,0 +1,60 @@ +N = int(input()) +bboard = [[0] * 4 for _ in range(6)] +gboard = [[0] * 4 for _ in range(6)] +global score +score = 0 + + +def cal(t, x, y, board): + global score + if t == 1: + for i in range(2, 7): + if i >= 6 or board[i][y]: + board[i - 1][y] = 1 + break + elif t == 2: + for i in range(2, 7): + if i >= 6 or (board[i][y] or board[i][y+1]): + board[i - 1][y] = 1 + board[i - 1][y + 1] = 1 + break + else: + for i in range(2, 7): + if i >= 6 or (board[i][y] or board[i-1][y]): + board[i - 1][y] = 1 + board[i - 2][y] = 1 + break + i = 5 + while i > 1: + if board[i] == [1, 1, 1, 1]: + score += 1 + board.pop(i) + board = [[0, 0, 0, 0]] + board + i += 1 + i -= 1 + cnt = 0 + for i in range(2): + if 1 in board[i]: + cnt += 1 + for i in range(cnt): + board.pop() + board = [[0, 0, 0, 0]] + board + return board + + +for _ in range(N): + t, x, y = map(int, input().split()) + if t == 1: + gboard = cal(1, x, y, gboard) + bboard = cal(1, y, x, bboard) + else: + gboard = cal(t, x, y, gboard) + bboard = cal(t + (-2*(t%2)) + 1, y, x, bboard) + + +blocknum = 0 +for i in range(2, 6): + blocknum += sum(bboard[i]) + blocknum += sum(gboard[i]) +print(score) +print(blocknum) \ No newline at end of file From 0b34c516a2f17ffa970ae111f811fca3c865bd78 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 31 Mar 2022 01:13:38 +0900 Subject: [PATCH 35/55] 19236 --- baekjoon/19236.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 baekjoon/19236.py diff --git a/baekjoon/19236.py b/baekjoon/19236.py new file mode 100644 index 0000000..9e5b481 --- /dev/null +++ b/baekjoon/19236.py @@ -0,0 +1,56 @@ +mv = [(-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1)] +fishes = [] +orders = [0] * 17 +for i in range(4): + a, b, c, d, e, f, g, h = map(int, input().split()) + fishes.append([[a, b-1], [c, d-1], [e, f-1], [g, h-1]]) +for i in range(4): + for j in range(4): + orders[fishes[i][j][0]] = [i, j] +global ans +ans = 0 + + +def dfs(fishes, x, y, s, orders): + global ans + n, d = fishes[x][y] + s += n + fishes[x][y][0] = 17 + orders[n] = [] + for i in range(1, 17): + if not orders[i]: + continue + cx, cy = orders[i] + cn, cd = fishes[cx][cy] + for j in range(8): + nd = (cd + j) % 8 + tx, ty = cx + mv[nd][0], cy + mv[nd][1] + if 0 <= tx < 4 and 0 <= ty < 4 and fishes[tx][ty][0] <= 16: + tn, td = fishes[tx][ty] + fishes[tx][ty] = [cn, nd] + orders[cn] = [tx, ty] + fishes[cx][cy] = [tn, td] + orders[tn] = [cx, cy] + break + candi = [] + for i in range(1, 4): + xx, yy = x + i*mv[d][0], y + i*mv[d][1] + if (0 <= xx < 4 and 0 <= yy < 4) and fishes[xx][yy][0]: + candi.append((xx, yy)) + if not candi: + ans = max(ans, s) + return + for cx, cy in candi: + tmp = [] + tmp_orders = orders[:] + for i in range(4): + tmp.append([]) + for j in range(4): + tmp[-1].append(fishes[i][j][:]) + tmp[x][y][0] = 0 + dfs(tmp, cx, cy, s, tmp_orders) + return + + +dfs(fishes, 0, 0, 0, orders) +print(ans) From d8c6ec543e741deeb409679770f11b665cc1eb6c Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 1 Apr 2022 00:47:09 +0900 Subject: [PATCH 36/55] 19237 --- baekjoon/19237.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 baekjoon/19237.py diff --git a/baekjoon/19237.py b/baekjoon/19237.py new file mode 100644 index 0000000..79f9577 --- /dev/null +++ b/baekjoon/19237.py @@ -0,0 +1,73 @@ +N, M, K = map(int, input().split()) +prior = [] +sharks = {} # x, y, direction +pos = [] +# 1,2,3,4 위,아래,왼쪽,오른 +for i in range(N): + s = map(int, input().split()) + pos.append([]) + for j, n in enumerate(s): + if n: + pos[-1].append([n, K]) # [shark num, remaining time] + sharks[n] = [i, j] + else: + pos[-1].append([0, 0]) +for i, d in enumerate(list(map(int, input().split()))): + sharks[i+1].append(d-1) +for i in range(1, M+1): + prior.append([]) + for j in range(4): + prior[-1].append([]) + for d in list(map(int, input().split())): + prior[-1][-1].append(d-1) +mv = [(-1, 0), (1, 0), (0, -1), (0, 1)] + + +def mvs(sharks): + # sharks move + tmp_sharks = {} + for shark, [x, y, d] in sharks.items(): + for cur_d in prior[shark - 1][d]: + i, j = mv[cur_d] + nx, ny = x + i, y + j + if 0 <= nx < N and 0 <= ny < N and not pos[nx][ny][0]: + tmp_sharks[shark] = [nx, ny, cur_d] + break + else: + for cur_d in prior[shark - 1][d]: + i, j = mv[cur_d] + nx, ny = x + i, y + j + if 0 <= nx < N and 0 <= ny < N and pos[nx][ny][0] == shark: + tmp_sharks[shark] = [nx, ny, cur_d] + break + # calculate time + for i in range(N): + for j in range(N): + if pos[i][j][1]: + pos[i][j][1] -= 1 + if not pos[i][j][1]: + pos[i][j][0] = 0 + # check sharks pos + dels = [] + for shark, [x, y, _ ] in tmp_sharks.items(): + if not pos[x][y][0] or pos[x][y][0] == shark: + pos[x][y] = [shark, K] + else: # other shark exists + if pos[x][y][0] < shark: + dels.append(shark) + else: + dels.append(pos[x][y][0]) + pos[x][y][0] = shark + for shark in dels: + del tmp_sharks[shark] + return tmp_sharks + + +ans = 0 +while len(sharks) > 1: + if ans >= 1000: + ans = -1 + break + sharks = mvs(sharks) + ans += 1 +print(ans) \ No newline at end of file From 01435a5777eb1da9c5e2fbc2f79206087c0bda8f Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 1 Apr 2022 16:10:17 +0900 Subject: [PATCH 37/55] 19238 --- baekjoon/19238.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 baekjoon/19238.py diff --git a/baekjoon/19238.py b/baekjoon/19238.py new file mode 100644 index 0000000..7e2bf80 --- /dev/null +++ b/baekjoon/19238.py @@ -0,0 +1,63 @@ +from collections import deque + +N, M, F = map(int, input().split()) +maps = [] +for i in range(N): + maps.append(list(map(int, input().split()))) +drv = [i-1 for i in list(map(int, input().split()))] +spos = [] # start position +dpos = [] # destination +for i in range(M): + a, b, c, d = map(int, input().split()) + spos.append([a-1, b-1]) + dpos.append([c-1, d-1]) +mv = [(0, 1), (1, 0), (-1, 0), (0, -1)] + + +def spath(des, src): + q = deque([src]) + visited = [[False] * N for _ in range(N)] + candi = [] + for d in range(N*N): + next_q = deque([]) + while q: + x, y = q.popleft() + if visited[x][y]: + continue + if [x, y] in des: + candi.append([x, y, d]) + visited[x][y] = True + for i, j in mv: + xx, yy = x + i, y + j + if 0 <= xx < N and 0 <= yy < N and not visited[xx][yy] and not maps[xx][yy]: + next_q.append([xx, yy]) + if candi: + break + q = next_q + candi.sort() + if not candi: + return -1, -1, F + 1 + return candi[0] + + +while spos: + x, y, f = spath(spos, drv) + F -= f + if F < 0: + break + i = spos.index([x, y]) + drv = spos[i] + del spos[i] + des = dpos[i] + del dpos[i] + + x, y, f = spath([des], drv) + drv = des + F -= f + if F < 0: + break + F += f * 2 +if F < 0: + print(-1) +else: + print(F) \ No newline at end of file From 1f97f3fee5e1a6c4d3b70190ce9bb3e52179e3f8 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sat, 2 Apr 2022 22:03:10 +0900 Subject: [PATCH 38/55] 20055 --- baekjoon/20055.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 baekjoon/20055.py diff --git a/baekjoon/20055.py b/baekjoon/20055.py new file mode 100644 index 0000000..aa7f434 --- /dev/null +++ b/baekjoon/20055.py @@ -0,0 +1,24 @@ +N, K = map(int, input().split()) +A = list(map(int, input().split())) +robots = [] +stage = 1 +while stage: + A = A[-1:] + A[:-1] + for i in range(len(robots)): + robots[i] += 1 + if robots and robots[0] == N - 1: + robots.pop(0) + for i, robot in enumerate(robots): + if A[robot + 1]: + if not i or (i and robots[i-1] != robot+1): + A[robot+1] -= 1 + robots[i] += 1 + if robots and robots[0] == N - 1: + robots.pop(0) + if A[0]: + A[0] -= 1 + robots.append(0) + if A.count(0) >= K: + print(stage) + break + stage += 1 \ No newline at end of file From e431fbd19984966cfa1e8d33d706ef0779a4e015 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 3 Apr 2022 18:34:10 +0900 Subject: [PATCH 39/55] 20056 --- baekjoon/20056.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 baekjoon/20056.py diff --git a/baekjoon/20056.py b/baekjoon/20056.py new file mode 100644 index 0000000..6585076 --- /dev/null +++ b/baekjoon/20056.py @@ -0,0 +1,58 @@ +from collections import defaultdict + + +N, M, K = map(int, input().split()) +fballs = defaultdict(list) +maps = [[0] * N for j in range(N)] +for i in range(M): + r, c, m, s, d = map(int, input().split()) + fballs[(r-1, c - 1)].append([m, s, d]) + maps[r-1][c-1] += 1 +mv = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)] + + +def cal_mv(fballs): + ret_fballs = defaultdict(list) + for (r, c), infos in fballs.items(): + maps[r][c] -= len(infos) + for m, s, d in infos: + i, j = mv[d] + rr, cc = (s*i + r)%N, (s*j + c)%N + ret_fballs[(rr, cc)].append([m, s, d]) + maps[rr][cc] += 1 + dels = [] + for (r, c), infos in ret_fballs.items(): + l = len(infos) + if l < 2: + continue + newm, news, newd = 0, 0, [] + for m, s, d in infos: + newm += m + news += s + newd.append(d % 2) + newm //= 5 + if not newm: + dels.append((r, c)) + continue + news //= l + if newd == [0] * l or newd == [1] * l: + newd = [0, 2, 4, 6] + else: + newd = [1, 3, 5, 7] + tmp = [] + if newm: + for d in newd: + tmp.append([newm, news, d]) + ret_fballs[(r, c)] = tmp + for i in dels: + del ret_fballs[i] + return ret_fballs + + +for i in range(K): + fballs = cal_mv(fballs) +ans = 0 +for infos in fballs.values(): + for m, d, s in infos: + ans += m +print(ans) \ No newline at end of file From 324fe52d689997fbef619c9ea11c1d6529216e90 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Mon, 4 Apr 2022 16:52:42 +0900 Subject: [PATCH 40/55] 20057 --- baekjoon/20057.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 baekjoon/20057.py diff --git a/baekjoon/20057.py b/baekjoon/20057.py new file mode 100644 index 0000000..39bcb1a --- /dev/null +++ b/baekjoon/20057.py @@ -0,0 +1,59 @@ +N = int(input()) +grid = [list(map(int, input().split())) for _ in range(N)] + + +def trans(mv): + new_mv = [] + for i, j in mv: + new_mv.append((-j, i)) + return new_mv + + +mv = [(0, -1), (1, 0), (0, 1), (-1, 0)] +pos = [[(-2, -1), (-1, -2), (-1, -1), (-1, 0), (0, -3), (1, -2), (1, -1), (1, 0), (2, -1), (0, -2)]] +pos.append(trans(pos[-1])) +pos.append(trans(pos[-1])) +pos.append(trans(pos[-1])) +per = [0.02, 0.1, 0.07, 0.01, 0.05, 0.1, 0.07, 0.01, 0.02] +global ans +ans = 0 + + +def cal(x, y, dx, dy, pos): + global ans + A = grid[dx][dy] + grid[dx][dy] = 0 + total = 0 + for i in range(9): + xx, yy = pos[i] + nx, ny = x + xx, y + yy + sand = int(A * per[i]) + if 0 <= nx < N and 0 <= ny < N: + grid[nx][ny] += sand + total += sand + else: + ans += sand + total += sand + nx, ny = x + pos[9][0], y + pos[9][1] + if 0 <= nx < N and 0 <= ny < N: + grid[nx][ny] += A - total + else: + ans += A - total + return True + + +l = 0 +d = -1 +x, y = N // 2, N // 2 +for i in range(2 * (N - 1) + 1): + d = (d + 1) % 4 + if d % 2 == 0: + l += 1 + for j in range(l): + if x | y == 0: + break + dx = x + mv[d][0] + dy = y + mv[d][1] + cal(x, y, dx, dy, pos[d]) + x, y = dx, dy +print(ans) \ No newline at end of file From 8be4e1c3cad6658686db71ad122e565698798ff3 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Tue, 5 Apr 2022 03:38:14 +0900 Subject: [PATCH 41/55] 20058 --- baekjoon/20058.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 baekjoon/20058.py diff --git a/baekjoon/20058.py b/baekjoon/20058.py new file mode 100644 index 0000000..7a8f503 --- /dev/null +++ b/baekjoon/20058.py @@ -0,0 +1,60 @@ +from collections import deque + +N, Q = map(int, input().split()) +NN = 2 ** N +A = [list(map(int, input().split())) for _ in range(NN)] +L = list(map(int, input().split())) +mv = [(0, 1), (1, 0), (0, -1), (-1, 0)] + + +def rotate(l, A): + mA = [[0] * NN for _ in range(NN)] + for r in range(0, NN, l): + for c in range(0, NN, l): + for x in range(r, r + l): + for y in range(c, c + l): + #px, py = r + l // 2, c + l // 2 + #dx, dy = x - px, y - py + #nx, ny = int(px + dy), int(py - dx) + mA[r - c + y][r + c + l - x - 1] = A[x][y] + candi = [] + for x in range(NN): + for y in range(NN): + cnt = 0 + for i, j in mv: + xx, yy = x + i, y + j + if 0 <= xx < NN and 0 <= yy < NN and mA[xx][yy]: + cnt += 1 + if cnt < 3: + candi.append((x, y)) + for x, y in candi: + if mA[x][y]: + mA[x][y] -= 1 + return mA + + +for l in L: + A = rotate(2 ** l, A) +total = 0 +for a in A: + total += sum(a) +maxsize = 0 +for i in range(NN): + for j in range(NN): + if not A[i][j]: + continue + q = deque([(i, j)]) + tmpmax = 0 + while q: + x, y = q.popleft() + if not A[x][y]: + continue + A[x][y] = 0 + tmpmax += 1 + for mx, my in mv: + nx, ny = x + mx, y + my + if 0 <= nx < NN and 0 <= ny < NN and A[nx][ny]: + q.append((nx, ny)) + maxsize = max(maxsize, tmpmax) +print(total) +print(maxsize) \ No newline at end of file From 6086ca6a4cbc9eb1a44aa1071a0db95e76dfb825 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Tue, 5 Apr 2022 05:09:16 +0900 Subject: [PATCH 42/55] 21608 --- baekjoon/21608.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 baekjoon/21608.py diff --git a/baekjoon/21608.py b/baekjoon/21608.py new file mode 100644 index 0000000..20c6530 --- /dev/null +++ b/baekjoon/21608.py @@ -0,0 +1,74 @@ +from collections import defaultdict +N = int(input()) +mv = ((0, 1), (1, 0), (-1, 0), (0, -1)) +room = [[0] * N for _ in range(N)] +pos = {i:0 for i in range(1, N**2 + 1)} +dictfri = {} +for _ in range(N*N): + stus = list(map(int, input().split())) + cur = stus[0] + fris = stus[1:] + dictfri[cur] = fris + candi = defaultdict(int) + for idx, fri in enumerate(fris): + if not pos.get(fri): + continue + fx, fy = pos[fri] + for dx, dy in mv: + cx, cy = fx + dx, fy + dy + if 0 <= cx < N and 0 <= cy < N and not room[cx][cy]: + candi[(cx, cy)] += 1 + if candi: + vs = list(candi.values()) + m = max(vs) + if vs.count(m) == 1: + idx = vs.index(m) + ks = list(candi.keys()) + x, y = ks[idx] + pos[cur] = (x, y) + room[x][y] = cur + continue + else: + tmp = [] + for k, v in candi.items(): + if v == m: + tmp.append(k) + candi = [] + for i, j in tmp: + if room[i][j]: + continue + cnt = 0 + for dx, dy in mv: + x, y = i + dx, j + dy + if 0 <= x < N and 0 <= y < N and not room[x][y]: + cnt += 1 + candi.append((cnt, i, j)) + else: + candi = [] + for i in range(N): + for j in range(N): + if room[i][j]: + continue + cnt = 0 + for dx, dy in mv: + x, y = i+dx, j+dy + if 0 <= x < N and 0 <= y < N and not room[x][y]: + cnt += 1 + candi.append((cnt, i, j)) + candi.sort(key=lambda x: (-x[0], x[1], x[2])) + x, y = candi[0][1], candi[0][2] + room[x][y] = cur + pos[cur] = (x, y) +score = [0, 1, 10, 100, 1000] +ans = 0 +for i in range(N): + for j in range(N): + cnt = 0 + cur = room[i][j] + fris = dictfri[cur] + for dx, dy in mv: + x, y = i + dx, j + dy + if 0 <= x < N and 0 <= y < N and room[x][y] in fris: + cnt += 1 + ans += score[cnt] +print(ans) \ No newline at end of file From b546e1552c9d24c1c7e0a4cfda3f07ed0975d82a Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 7 Apr 2022 17:45:02 +0900 Subject: [PATCH 43/55] 21609 --- baekjoon/21609.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 baekjoon/21609.py diff --git a/baekjoon/21609.py b/baekjoon/21609.py new file mode 100644 index 0000000..d8c099a --- /dev/null +++ b/baekjoon/21609.py @@ -0,0 +1,86 @@ +from collections import deque + +N, M = map(int, input().split()) +grid = [list(map(int, input().split())) for _ in range(N)] # empty: -2 +mv = ((1, 0), (0, 1), (-1, 0), (0, -1)) +global score +score = 1 + + +def pulldown(grid): + ngrid = [[-2] * N for _ in range(N)] + for j in range(N): + idx = N - 1 + for i in range(N - 1, -1, -1): + if grid[i][j] >= 0: + ngrid[idx][j] = grid[i][j] + idx -= 1 + elif grid[i][j] == -1: + ngrid[i][j] = -1 + idx = i - 1 + return ngrid + + +def rotate(grid): + ngrid = [] + for j in range(N - 1, -1, -1): + tmp = [] + for i in range(N): + tmp.append(grid[i][j]) + ngrid.append(tmp) + return ngrid + + +def cal_score(grid): + global score + visited = [[False] * N for _ in range(N)] + maxsize = 0 + maxrainbow = 0 + large_block = [] + for i in range(N): + for j in range(N): + if visited[i][j] or grid[i][j] <= 0: + continue + q = deque([(i, j)]) + candi = [] + rainbow = 0 + size = 0 + color = grid[i][j] + while q: + x, y = q.popleft() + if visited[x][y]: + continue + visited[x][y] = True + size += 1 + candi.append((x, y)) + for dx, dy in mv: + xx, yy = x + dx, y + dy + if 0 <= xx < N and 0 <= yy < N and not visited[xx][yy] and grid[xx][yy] in [0, color]: + q.append((xx, yy)) + for x, y in candi: + if not grid[x][y]: + visited[x][y] = False + rainbow += 1 + if size > 1: + if size > maxsize: + maxsize = size + large_block = candi + maxrainbow = rainbow + elif size == maxsize: + if rainbow >= maxrainbow: + large_block = candi + maxrainbow = rainbow + for x, y in large_block: + grid[x][y] = -2 + score = maxsize ** 2 + return grid + + +total = 0 +while score: + grid = cal_score(grid) + total += score + grid = pulldown(grid) + grid = rotate(grid) + grid = pulldown(grid) +print(total) \ No newline at end of file From ffc36205af1e0f1cb338088391c5e004fe3834bb Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 8 Apr 2022 19:34:11 +0900 Subject: [PATCH 44/55] 9372 --- baekjoon/9372.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 baekjoon/9372.py diff --git a/baekjoon/9372.py b/baekjoon/9372.py new file mode 100644 index 0000000..a27a991 --- /dev/null +++ b/baekjoon/9372.py @@ -0,0 +1,33 @@ +def union(parent, u, v): + p1 = find(parent, u) + p2 = find(parent, v) + r1, r2 = rank[u], rank[v] + if r1 > r2: + parent[p2] = p1 + else: + parent[p1] = p2 + if r1 == r2: + rank[p2] += 1 + return + + +def find(parent, v): + p = parent[v] + if p == v: + return p + return find(parent, p) + + +T = int(input()) +for t in range(T): + N, M = map(int, input().split()) + parent = [i for i in range(N+1)] + rank = [0 for i in range(N+1)] + cost = 0 + for i in range(M): + a, b = map(int, input().split()) + p1, p2 = find(parent, a), find(parent, b) + if p1 != p2: + union(parent, p1, p2) + cost += 1 + print(cost) \ No newline at end of file From f034f017fc774604e7f3d39546a2ddcd84b57e86 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 8 Apr 2022 20:47:02 +0900 Subject: [PATCH 45/55] 1197 --- baekjoon/1197.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 baekjoon/1197.py diff --git a/baekjoon/1197.py b/baekjoon/1197.py new file mode 100644 index 0000000..1aea81e --- /dev/null +++ b/baekjoon/1197.py @@ -0,0 +1,41 @@ +V, E = map(int, input().split()) +cost = 0 +parent = [i for i in range(V+1)] +rank = [0 for i in range(V+1)] +edges = [] +for e in range(E): + edges.append(tuple(map(int, input().split()))) +edges.sort(key=lambda x: x[2]) + + +def find(u): + p = parent[u] + if p == u: + return u + return find(p) + + +def union(u, v): + p1 = find(u) + p2 = find(v) + r1 = rank[p1] + r2 = rank[p2] + if r1 > r2: + parent[p2] = p1 + elif r1 < r2: + parent[p1] = p2 + else: + parent[p1] = p2 + rank[p2] += 1 + return + + +l = 1 +for a, b, w in edges: + if find(a) != find(b): + union(a, b) + cost += w + l += 1 + if l == V: + break +print(cost) \ No newline at end of file From 979881970355d05237fd5a2ac698bd3008308a85 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sat, 9 Apr 2022 20:11:37 +0900 Subject: [PATCH 46/55] =?UTF-8?q?=ED=81=B0=20=EC=88=98=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\210\230 \353\247\214\353\223\244\352\270\260.py" | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 "programmers/\355\201\260 \354\210\230 \353\247\214\353\223\244\352\270\260.py" diff --git "a/programmers/\355\201\260 \354\210\230 \353\247\214\353\223\244\352\270\260.py" "b/programmers/\355\201\260 \354\210\230 \353\247\214\353\223\244\352\270\260.py" new file mode 100644 index 0000000..cf130ab --- /dev/null +++ "b/programmers/\355\201\260 \354\210\230 \353\247\214\353\223\244\352\270\260.py" @@ -0,0 +1,12 @@ +def solution(number, k): + answer = [] + num = list(map(int, number)) + for idx, n in enumerate(num): + while answer: + if answer[-1] < n and len(answer) + (len(num)-idx) > (len(num) - k): + answer.pop() + else: + break + if len(answer) + k < len(num): + answer.append(n) + return "".join(list(map(str, answer))) \ No newline at end of file From 1466b790c15a48099b58fc47258f00444e97bb37 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sun, 10 Apr 2022 20:41:52 +0900 Subject: [PATCH 47/55] =?UTF-8?q?=EC=B5=9C=EA=B3=A0=EC=9D=98=20=EC=A7=91?= =?UTF-8?q?=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\352\263\240\354\235\230 \354\247\221\355\225\251.py" | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 "programmers/\354\265\234\352\263\240\354\235\230 \354\247\221\355\225\251.py" diff --git "a/programmers/\354\265\234\352\263\240\354\235\230 \354\247\221\355\225\251.py" "b/programmers/\354\265\234\352\263\240\354\235\230 \354\247\221\355\225\251.py" new file mode 100644 index 0000000..9278eb0 --- /dev/null +++ "b/programmers/\354\265\234\352\263\240\354\235\230 \354\247\221\355\225\251.py" @@ -0,0 +1,8 @@ +def solution(n, s): + r, q = divmod(s, n) + if r < 1: + return [-1] + answer = [r] * n + for i in range(n-q, n): + answer[i] += 1 + return answer \ No newline at end of file From e63ec6e5edb8e11740aad79554acd33991d53e19 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Mon, 11 Apr 2022 19:33:30 +0900 Subject: [PATCH 48/55] =?UTF-8?q?2=20x=20n=20=ED=83=80=EC=9D=BC=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/21610.py | 0 "programmers/2 x n \355\203\200\354\235\274\353\247\201.py" | 5 +++++ .../\353\251\200\353\246\254 \353\233\260\352\270\260.py" | 0 ...204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" | 0 4 files changed, 5 insertions(+) create mode 100644 baekjoon/21610.py create mode 100644 "programmers/2 x n \355\203\200\354\235\274\353\247\201.py" create mode 100644 "programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" create mode 100644 "programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" diff --git a/baekjoon/21610.py b/baekjoon/21610.py new file mode 100644 index 0000000..e69de29 diff --git "a/programmers/2 x n \355\203\200\354\235\274\353\247\201.py" "b/programmers/2 x n \355\203\200\354\235\274\353\247\201.py" new file mode 100644 index 0000000..05b4fcb --- /dev/null +++ "b/programmers/2 x n \355\203\200\354\235\274\353\247\201.py" @@ -0,0 +1,5 @@ +def solution(n): + dp = [1, 1] + for i in range(2, n+1): + dp.append((dp[-1] + dp[-2]) % 1000000007) + return dp[n] \ No newline at end of file diff --git "a/programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" "b/programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" new file mode 100644 index 0000000..e69de29 diff --git "a/programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" "b/programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" new file mode 100644 index 0000000..e69de29 From 36adacc54ec13da9a79566c57b614cfabddf8c28 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Tue, 12 Apr 2022 00:42:59 +0900 Subject: [PATCH 49/55] =?UTF-8?q?=EC=A4=84=20=EC=84=9C=EB=8A=94=20?= =?UTF-8?q?=EB=B0=A9=EB=B2=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\234\353\212\224 \353\260\251\353\262\225.py" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" "b/programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" index e69de29..c9b06dc 100644 --- "a/programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" +++ "b/programmers/\354\244\204 \354\204\234\353\212\224 \353\260\251\353\262\225.py" @@ -0,0 +1,15 @@ +def solution(n, k): + answer = [] + numbers = [i + 1 for i in range(n)] + d = 1 + k -= 1 + for i in range(1, n+1): + d *= i + for i in range(n, 0, -1): + d //= i + r = int(k // d) + k = k % d + num = numbers.pop(r) + answer.append(num) + #numbers = numbers[:r] + numbers[r + 1:] + return answer \ No newline at end of file From c1d56c37520c8a0df245e1a74862e748bb453ccd Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Wed, 13 Apr 2022 14:43:06 +0900 Subject: [PATCH 50/55] =?UTF-8?q?=EB=A9=80=EB=A6=AC=20=EB=9B=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\251\200\353\246\254 \353\233\260\352\270\260.py" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" "b/programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" index e69de29..da609fb 100644 --- "a/programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" +++ "b/programmers/\353\251\200\353\246\254 \353\233\260\352\270\260.py" @@ -0,0 +1,5 @@ +def solution(n): + answer = [1, 1] + for i in range(2, n+1): + answer.append((answer[-1] + answer[-2]) % 1234567) + return answer[n] \ No newline at end of file From 4d34254937317aab42bb6489274d415e2b9bc20f Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Thu, 14 Apr 2022 13:25:48 +0900 Subject: [PATCH 51/55] =?UTF-8?q?=EC=95=BC=EA=B7=BC=20=EC=A7=80=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\352\262\260\355\225\230\352\270\260.py" | 0 ...4\352\267\274 \354\247\200\354\210\230.py" | 33 +++++++++++++++++++ ...40\354\210\234\354\234\204\355\201\220.py" | 0 ...0 \354\202\274\352\260\201\355\230\225.py" | 0 4 files changed, 33 insertions(+) create mode 100644 "programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" create mode 100644 "programmers/\354\225\274\352\267\274 \354\247\200\354\210\230.py" create mode 100644 "programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" create mode 100644 "programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" diff --git "a/programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" "b/programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" new file mode 100644 index 0000000..e69de29 diff --git "a/programmers/\354\225\274\352\267\274 \354\247\200\354\210\230.py" "b/programmers/\354\225\274\352\267\274 \354\247\200\354\210\230.py" new file mode 100644 index 0000000..516c7d6 --- /dev/null +++ "b/programmers/\354\225\274\352\267\274 \354\247\200\354\210\230.py" @@ -0,0 +1,33 @@ +def solution(n, works): + answer = 0 + works.sort(reverse=True) + l, r = 0, works[0] + while l <= r: + mid = (l + r) // 2 + if check(mid, n, works): + r = mid - 1 + else: + l = mid + 1 + for i in range(len(works)): + diff = works[i] - l + if diff <= 0: + continue + works[i] = l + n -= diff + for i in range(min(n, len(works))): + if works[i] == 0: + break + works[i] -= 1 + for work in works: + answer += work ** 2 + return answer +def check(d, n, works): + total = 0 + for work in works: + diff = work - d + if diff <= 0: + continue + total += diff + if total > n: + return False + return True \ No newline at end of file diff --git "a/programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" "b/programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" new file mode 100644 index 0000000..e69de29 diff --git "a/programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" "b/programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" new file mode 100644 index 0000000..e69de29 From f906e4a3e53f5c78d73e1d71b7ac8758e112d78e Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Mon, 18 Apr 2022 23:06:02 +0900 Subject: [PATCH 52/55] =?UTF-8?q?=EC=84=AC=20=EC=97=B0=EA=B2=B0=ED=95=98?= =?UTF-8?q?=EA=B8=B0,=20=EC=9D=B4=EC=A4=91=EC=9A=B0=EC=84=A0=EC=88=9C?= =?UTF-8?q?=EC=9C=84=ED=81=90,=20=EC=A0=95=EC=88=98=20=EC=82=BC=EA=B0=81?= =?UTF-8?q?=ED=98=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\352\262\260\355\225\230\352\270\260.py" | 28 +++++++++++++++++++ ...40\354\210\234\354\234\204\355\201\220.py" | 28 +++++++++++++++++++ ...0 \354\202\274\352\260\201\355\230\225.py" | 5 ++++ 3 files changed, 61 insertions(+) diff --git "a/programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" "b/programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" index e69de29..2700bfc 100644 --- "a/programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" +++ "b/programmers/\354\204\254 \354\227\260\352\262\260\355\225\230\352\270\260.py" @@ -0,0 +1,28 @@ +def solution(n, costs): + parent = [i for i in range(n)] + rank = [0] * n + + def find(u): + p = parent[u] + if p == u: + return u + return find(p) + + def union(u, v, root1, root2): + rank1, rank2 = rank[root1], rank[root2] + if rank1 > rank2: + parent[root2] = root1 + else: + parent[root1] = root2 + if rank1 == rank2: + rank[root2] += 1 + return + + answer = 0 + costs.sort(key=lambda x: x[2]) + for a, b, c in costs: + p1, p2 = find(a), find(b) + if p1 != p2: + union(a, b, p1, p2) + answer += c + return answer \ No newline at end of file diff --git "a/programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" "b/programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" index e69de29..9aa9d59 100644 --- "a/programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" +++ "b/programmers/\354\235\264\354\244\221\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220.py" @@ -0,0 +1,28 @@ +from heapq import heappush, heappop + + +def solution(operations): # min heap & max heap + maxh = [] + minh = [] + for ops in operations: + op, n = ops.split() + n = int(n) + if op == "D": + if n == 1: + if not maxh: + continue + heappop(maxh) + if not maxh or -maxh[0] < minh[0]: ## 동기화 + minh, maxh = [], [] + else: + if not minh: + continue + heappop(minh) + if not minh or minh[0] > -maxh[0]: + minh, maxh = [], [] + else: + heappush(maxh, -n) + heappush(minh, n) + if not maxh: + return [0, 0] + return [heappop(maxh), heappop(minh)] diff --git "a/programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" "b/programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" index e69de29..478f026 100644 --- "a/programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" +++ "b/programmers/\354\240\225\354\210\230 \354\202\274\352\260\201\355\230\225.py" @@ -0,0 +1,5 @@ +def solution(triangle): + for i in range(len(triangle)-1, 0, -1): + for j in range(i): + triangle[i-1][j] += max(triangle[i][j], triangle[i][j+1]) + return triangle[0][0] \ No newline at end of file From d2b885afd33da885ee08614b4c54303365419bc6 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Tue, 19 Apr 2022 15:42:03 +0900 Subject: [PATCH 53/55] 21610 --- baekjoon/21610.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/baekjoon/21610.py b/baekjoon/21610.py index e69de29..e7c6095 100644 --- a/baekjoon/21610.py +++ b/baekjoon/21610.py @@ -0,0 +1,44 @@ +N, M = map(int, input().split()) +maps = [list(map(int, input().split())) for _ in range(N)] +mv = [(0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)] +diagonal = [(-1, -1), (-1, 1), (1, -1), (1, 1)] +clouds = [[N-1, 0], [N-1, 1], [N-2, 0], [N-2, 1]] + + +def biba(d, s, maps, clouds): + x, y = mv[d] + xx, yy = x * s, y * s + visited = [[False] * N for _ in range(N)] + for i in range(len(clouds)): + r, c = clouds[i] + clouds[i][0] = (r + xx) % N + clouds[i][1] = (c + yy) % N + for r, c in clouds: #rainning + maps[r][c] += 1 + visited[r][c] = True + cnts = [] + for r, c in clouds: #water copy + cnts.append(0) + for x, y in diagonal: + xx, yy = r + x, c + y + if 0 <= xx < N and 0 <= yy < N and maps[xx][yy]: + cnts[-1] += 1 + for idx, cloud in enumerate(clouds): + r, c = cloud + maps[r][c] += cnts[idx] + clouds = [] + for i in range(N): + for j in range(N): + if not visited[i][j] and maps[i][j] > 1: + maps[i][j] -= 2 + clouds.append([i, j]) + return clouds + + +for i in range(M): + d, s = map(int, input().split()) + clouds = biba(d-1, s, maps, clouds) +res = 0 +for i in range(N): + res += sum(maps[i]) +print(res) \ No newline at end of file From 0fe3c9ac71f585d6c253df74c7e8a4f3406d333f Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Fri, 29 Apr 2022 17:06:27 +0900 Subject: [PATCH 54/55] 21611 --- baekjoon/21611.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 baekjoon/21611.py diff --git a/baekjoon/21611.py b/baekjoon/21611.py new file mode 100644 index 0000000..0845710 --- /dev/null +++ b/baekjoon/21611.py @@ -0,0 +1,115 @@ +N, M = map(int, input().split()) +beads = [list(map(int, input().split())) for _ in range(N)] +magic = [] +pos = {} +mv = {1: (-1, 0), 2: (1, 0), 3: (0, -1), 4: (0, 1)} +j = 0 +p = 1 +x, y = N//2, N//2 +scores = {1: 0, 2: 0, 3: 0} +for i in range(N//2*4 + 1): + if i % 2 == 0: + j += 1 + if i % 4 == 0: + d = 3 + elif i % 4 == 1: + d = 2 + elif i % 4 == 2: + d = 4 + else: + d = 1 + for k in range(j): + xx, yy = mv[d] + x, y = x + xx, y + yy + pos[p] = (x, y) + p += 1 +del pos[N*N] + + +def pullBead(): + cnt = 0 + for i in range(1, N * N): + x, y = pos[i] + p = beads[x][y] + if p > 0: + cnt += 1 + x, y = pos[cnt] + beads[x][y] = p + for i in range(cnt + 1, N * N): + x, y = pos[i] + beads[x][y] = 0 + return + + +def ice(d, s): + x, y = N//2, N//2 + xx, yy = mv[d] + for i in range(s): + x += xx + y += yy + beads[x][y] = 0 + pullBead() + return + + +def explode(): + n = -1 + cnt = 0 + res = False + for i in range(1, N*N): + x, y = pos[i] + cur = beads[x][y] + if cur == 0: + break + if n == cur: + cnt += 1 + else: + if cnt >= 4: + for j in range(1, cnt+1): + x, y = pos[i-j] + beads[x][y] = 0 + scores[n] += cnt + res = True + n = cur + cnt = 1 + if cnt >= 4: + for j in range(cnt): + x, y = pos[i-j] + beads[x][y] = 0 + scores[n] += cnt + res = True + return res + + +def makeBeads(): + nbeads = [[0] * N for _ in range(N)] + n = beads[N//2][N//2-1] + nbeads[N//2+1][N//2-1] = n + cur_idx = 1 + for i in range(1, N*N): + x, y = pos[i] + cur = beads[x][y] + if cur == 0: + break + if cur == n: + x, y = pos[cur_idx] + nbeads[x][y] += 1 + else: + cur_idx += 2 + if cur_idx >= N*N: + break + x, y = pos[cur_idx] + nbeads[x][y] += 1 + x, y = pos[cur_idx + 1] + nbeads[x][y] = cur + n = cur + return nbeads + + +for i in range(M): + d, s = map(int, input().split()) + ice(d, s) + while explode(): + pullBead() + beads = makeBeads() +print(scores[1] + scores[2]*2 + scores[3]*3) \ No newline at end of file From 9d2e461605f0a694121d357bd107551ab70a8c45 Mon Sep 17 00:00:00 2001 From: jinjunpark Date: Sat, 30 Apr 2022 03:24:07 +0900 Subject: [PATCH 55/55] 23290 --- baekjoon/23290.py | 102 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 baekjoon/23290.py diff --git a/baekjoon/23290.py b/baekjoon/23290.py new file mode 100644 index 0000000..1a353a3 --- /dev/null +++ b/baekjoon/23290.py @@ -0,0 +1,102 @@ +M, S = map(int, input().split()) +fishes = {} +for i in range(4): + for j in range(4): + fishes[(i, j)] = [] +mv = {0: (0, -1), 1: (-1, -1), 2: (-1, 0), 3: (-1, 1), 4: (0, 1), 5: (1, 1), 6: (1, 0), 7: (1, -1)} +grid = [[0] * 4 for _ in range(4)] +scents = [[0] * 4 for _ in range(4)] +for i in range(M): + x, y, d = map(int, input().split()) + fishes[(x-1, y-1)].append(d-1) + grid[x-1][y-1] += 1 +origin = {} +s1, s2 = list(map(int, input().split())) +shark = [s1-1, s2-1] +smv = [(-1, 0), (0, -1), (1, 0), (0, 1)] # 상좌하우 + + +def move(fishes): + nfishes = {} + for i in range(4): + for j in range(4): + nfishes[(i, j)] = [] + for x, y in fishes: + for d in fishes[(x, y)]: + for i in range(8): + xx, yy = mv[(d-i) % 8] + nx, ny = x+xx, y+yy + if 0 <= nx < 4 and 0 <= ny < 4 and shark != [nx, ny] and not scents[nx][ny]: + nfishes[(nx, ny)].append((d-i) % 8) + grid[x][y] -= 1 + grid[nx][ny] += 1 + break + else: + nfishes[(x, y)].append(d) + return nfishes + + +def search(): + candi = [] + cnt = -1 + for i in range(4): + xx1, yy1 = smv[i] + nx1, ny1 = shark[0] + xx1, shark[1] + yy1 + if not(0 <= nx1 < 4 and 0 <= ny1 < 4): + continue + cnt1 = grid[nx1][ny1] + for j in range(4): + xx2, yy2 = smv[j] + nx2, ny2 = nx1 + xx2, ny1 + yy2 + if not (0 <= nx2 < 4 and 0 <= ny2 < 4): + continue + cnt2 = cnt1 + grid[nx2][ny2] + for k in range(4): + xx3, yy3 = smv[k] + nx3, ny3 = nx2 + xx3, ny2 + yy3 + if not (0 <= nx3 < 4 and 0 <= ny3 < 4): + continue + cnt3 = cnt2 + grid[nx3][ny3] + if nx3 == nx1 and ny3 == ny1: + cnt3 = cnt2 + if cnt3 > cnt: + cnt = cnt3 + candi = [i, j, k] + for d in candi: + xx, yy = smv[d] + shark[0] += xx + shark[1] += yy + if grid[shark[0]][shark[1]] > 0: + scents[shark[0]][shark[1]] = 3 + grid[shark[0]][shark[1]] = 0 + fishes[tuple(shark)] = [] + return + + +def rm_scent(): + for i in range(4): + for j in range(4): + if scents[i][j] > 0: + scents[i][j] -= 1 + + +def fishcopy(): + for fish, v in origin.items(): + x, y = fish + grid[x][y] += len(v) + fishes[(x, y)].extend(v) + return + + +for i in range(S): + for j in range(4): + for k in range(4): + origin[(j, k)] = fishes[(j, k)][:] + fishes = move(fishes) + search() + rm_scent() + fishcopy() +res = 0 +for i in range(4): + res += sum(grid[i]) +print(res) \ No newline at end of file