Skip to content

Commit

Permalink
2024-09-27
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Sep 27, 2024
1 parent 9db5604 commit 8e8ba20
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@
| 74์ฐจ์‹œ | 2024.08.30 | BFS | <a href="https://www.acmicpc.net/problem/11967">๋ถˆ ์ผœ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/242
| 75์ฐจ์‹œ | 2024.09.02 | DP | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/258705">์‚ฐ ๋ชจ์–‘ ํƒ€์ผ๋ง</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/243
| 76์ฐจ์‹œ | 2024.09.06 | DFS + ํŠธ๋ฆฌ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/150367">ํ‘œํ˜„ ๊ฐ€๋Šฅํ•œ ์ด์ง„ํŠธ๋ฆฌ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/246
| 77์ฐจ์‹œ | 2024.09.27 | ๊ตฌํ˜„ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/150366">ํ‘œ ๋ณ‘ํ•ฉ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/247
---
101 changes: 101 additions & 0 deletions tgyuuAn/๊ตฌํ˜„/ํ‘œ ๋ณ‘ํ•ฉ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from collections import defaultdict

def solution(commands):
table = [[None for _ in range(52)] for _ in range(52)]
linked_dict = defaultdict(set)
value_dict = defaultdict(set)
answer = []

for command in commands:
splt = command.split()
length = len(splt)
query = splt[0]

if query == "UPDATE":
if(length == 3):
value1, value2 = splt[1], splt[2]

temp = set()
for (r, c) in value_dict[value1]:
table[r][c] = value2
temp.add((r,c))

value_dict[value1] = set()
value_dict[value2].update(temp)

elif(length == 4):
r, c, value1 = int(splt[1]), int(splt[2]), splt[3]

origin = table[r][c]
if len(linked_dict[(r,c)]) == 0:
table[r][c] = value1
value_dict[value1].add((r,c))
if origin is not None: value_dict[origin].discard((r,c))

else:
for (l_r, l_c) in linked_dict[(r,c)]:
if origin is not None: value_dict[origin].discard((l_r, l_c))
table[l_r][l_c] = value1

value_dict[value1].update(linked_dict[(r,c)])

elif query == "MERGE":
r1, c1, r2, c2 = int(splt[1]), int(splt[2]), int(splt[3]), int(splt[4])

if((r1, c1) == (r2, c2)): continue
value1 = table[r1][c1]
value2 = table[r2][c2]
merge_value = None
if value1 is not None and value2 is not None:
merge_value = value1

elif value1 is not None:
merge_value = value1

elif value2 is not None:
merge_value = value2

all_element = {(r1, c1), (r2, c2)}

value1_element = {(r1, c1)} | linked_dict[(r1, c1)]
value2_element = {(r2, c2)} | linked_dict[(r2, c2)]
all_element = value1_element | value2_element

if value1 is None and value2 is not None:
for (el_r, el_c) in value1_element:
table[el_r][el_c] = merge_value

value_dict[value2].add((el_r, el_c))

if not(value1 is None and value2 is None):
for (el_r, el_c) in value2_element:
table[el_r][el_c] = merge_value

if value1 is not None and value2 is not None:
value_dict[value2].discard((el_r, el_c))
value_dict[value1].add((el_r, el_c))

elif value1 is not None:
value_dict[value2].discard((el_r, el_c))
value_dict[value1].add((el_r, el_c))

for (l_r, l_c) in all_element:
linked_dict[(l_r, l_c)] = all_element

elif query =="UNMERGE":
r, c = int(splt[1]), int(splt[2])
value1 = table[r][c]
value_dict[value1] -= linked_dict[(r, c)]

for (now_r, now_c) in linked_dict[(r, c)]:
linked_dict[(now_r, now_c)] = set()
table[now_r][now_c] = None

table[r][c] = value1
value_dict[value1].add((r,c))

elif query =="PRINT":
r, c = int(splt[1]), int(splt[2])
answer.append(table[r][c] if table[r][c] is not None else "EMPTY")

return answer

0 comments on commit 8e8ba20

Please sign in to comment.