Approach
We can also solve this problem using a red-black tree data structure to manage the price sequence. A red-black tree is a balanced binary search tree that ensures the tree remains balanced after each insert operation.
- Create a class
Node to represent a node in the red-black tree. Each node will have the following attributes:
key: The value of the node (price).
left: The left child of the node.
right: The right child of the node.
color: The color of the node.
parent: The parent of the node.
min_diff: The minimum absolute difference between adjacent prices in the subtree rooted at this node.
- Create a class
RedBlackTree to represent the red-black tree. This class will have the following methods:
__init__(): Initialize an empty red-black tree.
insert(key): Insert a new node with the given key into the red-black tree.
get_closest_adj_price(): Get the minimum absolute difference between adjacent prices in the red-black tree.
get_closest_price(): Get the minimum absolute difference between any two prices in the red-black tree.
left_rotate(node): Perform a left rotation on the given node.
right_rotate(node): Perform a right rotation on the given node.
fix_violations(node): Fix any violations in the red-black tree after an insert operation.
update_min_diff(node): Update the min_diff attribute of the given node based on its children.
- Initialize an empty red-black tree.
- Read the values of
n and m from input.
- Read the initial sequence
a from input and insert each element into the red-black tree.
- Iterate
m times to perform each operation:
- Read the current operation from input.
- If the operation is "BUY x", insert the price
x into the red-black tree.
- If the operation is "CLOSEST ADJ PRICE", call the
get_closest_adj_price() method and print the result.
- If the operation is "CLOSEST PRICE", call the
get_closest_price() method and print the result.
- Continue step 6 until all operations are performed.
Implementation
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.color = None
self.parent = None
self.min_diff = float('inf')
class RedBlackTree:
def __init__(self):
self.root = None
def insert(self, key):
node = Node(key)
node.color = 'RED'
parent = None
current = self.root
while current is not None:
parent = current
if node.key < current.key:
current = current.left
else:
current = current.right
node.parent = parent
if parent is None:
self.root = node
elif node.key < parent.key:
parent.left = node
else:
parent.right = node
self.fix_violations(node)
def get_closest_adj_price(self):
return self.root.min_diff if self.root is not None else 0
def get_closest_price(self):
return self.find_closest_price(self.root)
def find_closest_price(self, node):
if node is None or (node.left is None and node.right is None):
return float('inf')
current_diff = float('inf')
if node.left is not None:
left_diff = abs(node.key - node.left.key)
current_diff = min(current_diff, left_diff)
current_diff = min(current_diff, self.find_closest_price(node.left))
if node.right is not None:
right_diff = abs(node.key - node.right.key)
current_diff = min(current_diff, right_diff)
current_diff = min(current_diff, self.find_closest_price(node.right))
return current_diff
def left_rotate(self, node):
right_child = node.right
node.right = right_child.left
if right_child.left is not None:
right_child.left.parent = node
right_child.parent = node.parent
if node.parent is None:
self.root = right_child
elif node == node.parent.left:
node.parent.left = right_child
else:
node.parent.right = right_child
right_child.left = node
node.parent = right_child
def right_rotate(self, node):
left_child = node.left
node.left = left_child.right
if left_child.right is not None:
left_child.right.parent = node
left_child.parent = node.parent
if node.parent is None:
self.root = left_child
elif node == node.parent.left:
node.parent.left = left_child
else:
node.parent.right = left_child
left_child.right = node
node.parent = left_child
def fix_violations(self, node):
while node.parent is not None and node.parent.color == 'RED':
if node.parent == node.parent.parent.left:
uncle = node.parent.parent.right
if uncle is not None and uncle.color == 'RED':
node.parent.color = 'BLACK'
uncle.color = 'BLACK'
node.parent.parent.color = 'RED'
node = node.parent.parent
else:
if node == node.parent.right:
node = node.parent
self.left_rotate(node)
node.parent.color = 'BLACK'
node.parent.parent.color = 'RED'
self.right_rotate(node.parent.parent)
else:
uncle = node.parent.parent.left
if uncle is not None and uncle.color == 'RED':
node.parent.color = 'BLACK'
uncle.color = 'BLACK'
node.parent.parent.color = 'RED'
node = node.parent.parent
else:
if node == node.parent.left:
node = node.parent
self.right_rotate(node)
node.parent.color = 'BLACK'
node.parent.parent.color = 'RED'
self.left_rotate(node.parent.parent)
self.root.color = 'BLACK'
def update_min_diff(self, node):
left_diff = abs(node.key - node.left.key) if node.left is not None else float('inf')
right_diff = abs(node.key - node.right.key) if node.right is not None else float('inf')
node.min_diff = min(left_diff, right_diff)
if node.parent is not None:
self.update_min_diff(node.parent)
n, m = map(int, input().split())
prices = list(map(int, input().split()))
rb_tree = RedBlackTree()
for price in prices:
rb_tree.insert(price)
for _ in range(m):
operation = input()
if operation.startswith("BUY"):
_, x = operation.split()
rb_tree.insert(int(x))
elif operation == "CLOSEST ADJ PRICE":
print(rb_tree.get_closest_adj_price())
elif operation == "CLOSEST PRICE":
print(rb_tree.get_closest_price())
Complexity Analysis
The time complexity for this approach is O(mlog(n)), where m is the number of operations and n is the length of the price sequence. This is because the insert, get_closest_adj_price, and get_closest_price operations take O(log(n)) time in the worst case. The overall complexity is dominated by the insert operation as it will be performed m times.