| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Python Implementation
- t1
- Substring with Concatenation of All Words
- data science
- 715. Range Module
- shiba
- iterator
- concurrency
- Generator
- attribute
- Class
- LeetCode
- 파이썬
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- Convert Sorted List to Binary Search Tree
- Decorator
- 운영체제
- DWG
- 시바견
- Python
- Protocol
- kaggle
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- Python Code
- Regular Expression
- 프로그래머스
- 컴퓨터의 구조
Archives
- Today
- Total
Scribbling
LeetCode: 990. Satisfiability of Equality Equations 본문
Computer Science/Coding Test
LeetCode: 990. Satisfiability of Equality Equations
focalpoint 2022. 2. 26. 20:05class Solution:
def equationsPossible(self, equations: List[str]) -> bool:
# Define parents list for find/union parent operations
parents = [i for i in range(26)]
# find parent operation
def find_parent(node):
if node == parents[node]:
return node
parents[node] = find_parent(parents[node])
return parents[node]
# union parent operation
def union_parent(node1, node2):
node1 = find_parent(node1)
node2 = find_parent(node2)
if node1 <= node2:
parents[node2] = node1
else:
parents[node1] = node2
# For all equalities: apply 'union parent opt'
# We'll later deal with unequalities
unequals = []
for equation in equations:
if equation[1] == '!':
unequals.append(equation)
else:
node1, node2 = ord(equation[0]) - ord('a'), ord(equation[3]) - ord('a')
union_parent(node1, node2)
# Make sure every node has its eldest parent
for node in range(26):
find_parent(node)
# Now deal with unequalities:
# If they have the same parent, it's a contradiction
for equation in unequals:
node1, node2 = ord(equation[0]) - ord('a'), ord(equation[3]) - ord('a')
if parents[node1] == parents[node2]:
return False
return True'Computer Science > Coding Test' 카테고리의 다른 글
| LeetCode: 715. Range Module (0) | 2022.03.03 |
|---|---|
| LeetCode: 1048. Longest String Chain (0) | 2022.02.27 |
| LeetCode: 652. Find Duplicate Subtrees (0) | 2022.02.23 |
| LeetCode: 465. Optimal Account Balancing (0) | 2022.02.17 |
| LeetCode: 410. Split Array Largest Sum (0) | 2022.02.17 |