var AllOne = function () {
this.map = new Map(); // key -> node
this.head = null; // Head of the doubly linked list (min count)
this.tail = null; // Tail of the doubly linked list (max count)
};
/**
* Node structure for the linked list
* @param {number} count - the count of the keys in the set
* @param {Set<string>} keys - the set of keys associated with this count
*/
function Node(count, keys = new Set()) {
this.count = count;
this.keys = keys;
this.prev = null;
this.next = null;
}
/**
* Helper function to remove a node from the linked list
* @param {Node} node
*/
AllOne.prototype.removeNode = function (node) {
if (!node) return;
if (node.prev) {
node.prev.next = node.next;
} else {
this.head = node.next; // Node is the head
}
if (node.next) {
node.next.prev = node.prev;
} else {
this.tail = node.prev; // Node is the tail
}
node.prev = null;
node.next = null;
};
/**
* Helper function to insert a node after another node
* @param {Node} nodeToInsert
* @param {Node} nodeAfter
*/
AllOne.prototype.insertAfter = function (nodeToInsert, nodeAfter) {
if (!nodeToInsert) return;
nodeToInsert.prev = nodeAfter;
nodeToInsert.next = nodeAfter.next;
if (nodeAfter.next) {
nodeAfter.next.prev = nodeToInsert;
} else {
this.tail = nodeToInsert; // nodeAfter was the tail
}
nodeAfter.next = nodeToInsert;
};
/**
* @param {string} key
* @return {void}
*/
AllOne.prototype.inc = function (key) {
if (!this.map.has(key)) {
// Key doesn't exist
if (!this.head || this.head.count > 1) {
// Create a new node with count 1 and insert at the head
const newNode = new Node(1, new Set([key]));
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
this.map.set(key, newNode);
} else {
// Head exists and has count 1, just add the key
this.head.keys.add(key);
this.map.set(key, this.head);
}
} else {
// Key exists, increment the count
const node = this.map.get(key);
node.keys.delete(key); // Remove key from the current node
if (node.next === null || node.next.count > node.count + 1) {
// Create a new node with incremented count and insert it
const newNode = new Node(node.count + 1, new Set([key]));
this.insertAfter(newNode, node);
this.map.set(key, newNode);
} else {
// Next node exists and has the correct count, just add the key
node.next.keys.add(key);
this.map.set(key, node.next);
}
if (node.keys.size === 0) {
// Remove the node if it's empty
this.removeNode(node);
}
}
};
/**
* @param {string} key
* @return {void}
*/
AllOne.prototype.dec = function (key) {
if (!this.map.has(key)) return; // Key doesn't exist, do nothing
const node = this.map.get(key);
node.keys.delete(key); // Remove key from the current node
if (node.count === 1) {
// Removing the key entirely
this.map.delete(key);
if (node.keys.size === 0) {
this.removeNode(node);
}
} else {
if (node.prev === null || node.prev.count < node.count - 1) {
// Create a new node with decremented count and insert it
const newNode = new Node(node.count - 1, new Set([key]));
if (node.prev) {
this.insertAfter(newNode, node.prev)
} else {
newNode.next = node;
node.prev = newNode;
this.head = newNode
}
this.map.set(key, newNode);
} else {
// Previous node exists and has the correct count, just add the key
node.prev.keys.add(key);
this.map.set(key, node.prev);
}
if (node.keys.size === 0) {
// Remove the node if it's empty
this.removeNode(node);
}
}
};
/**
* @return {string}
*/
AllOne.prototype.getMaxKey = function () {
if (!this.tail) return "";
for (const key of this.tail.keys) {
return key; // Return the first key in the tail's key set
}
};
/**
* @return {string}
*/
AllOne.prototype.getMinKey = function () {
if (!this.head) return "";
for (const key of this.head.keys) {
return key; // Return the first key in the head's key set
}
};