Skip to content

Commit 65658b1

Browse files
committed
Add solution #432
1 parent 00f5917 commit 65658b1

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
427|[Construct Quad Tree](./0427-construct-quad-tree.js)|Medium|
343343
429|[N-ary Tree Level Order Traversal](./0429-n-ary-tree-level-order-traversal.js)|Medium|
344344
430|[Flatten a Multilevel Doubly Linked List](./0430-flatten-a-multilevel-doubly-linked-list.js)|Medium|
345+
432|[All O`one Data Structure](./0432-all-oone-data-structure.js)|Hard|
345346
434|[Number of Segments in a String](./0434-number-of-segments-in-a-string.js)|Easy|
346347
435|[Non-overlapping Intervals](./0435-non-overlapping-intervals.js)|Medium|
347348
437|[Path Sum III](./0437-path-sum-iii.js)|Medium|
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* 432. All O`one Data Structure
3+
* https://leetcode.com/problems/all-oone-data-structure/
4+
* Difficulty: Hard
5+
*
6+
* Design a data structure to store the strings' count with the ability to return the strings
7+
* with minimum and maximum counts.
8+
*
9+
* Implement the AllOne class:
10+
* - AllOne() Initializes the object of the data structure.
11+
* - inc(String key) Increments the count of the string key by 1. If key does not exist in the
12+
* data structure, insert it with count 1.
13+
* - dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after
14+
* the decrement, remove it from the data structure. It is guaranteed that key exists in the
15+
* data structure before the decrement.
16+
* - getMaxKey() Returns one of the keys with the maximal count. If no element exists, return
17+
* an empty string "".
18+
* - getMinKey() Returns one of the keys with the minimum count. If no element exists, return
19+
* an empty string "".
20+
*
21+
* Note that each function must run in O(1) average time complexity.
22+
*/
23+
24+
var AllOne = function() {
25+
this.map = new Map();
26+
this.head = { count: 0, keys: new Set(), prev: null, next: null };
27+
this.tail = { count: Infinity, keys: new Set(), prev: this.head, next: null };
28+
this.head.next = this.tail;
29+
};
30+
31+
/**
32+
* @param {string} key
33+
* @return {void}
34+
*/
35+
AllOne.prototype.inc = function(key) {
36+
const node = this.map.get(key) || this.head;
37+
const count = node.count + 1;
38+
let next = node.next;
39+
40+
if (next.count !== count) {
41+
next = { count, keys: new Set(), prev: node, next: node.next };
42+
node.next.prev = next;
43+
node.next = next;
44+
}
45+
46+
next.keys.add(key);
47+
node.keys.delete(key);
48+
this.map.set(key, next);
49+
50+
if (node !== this.head && node.keys.size === 0) {
51+
node.prev.next = node.next;
52+
node.next.prev = node.prev;
53+
}
54+
};
55+
56+
/**
57+
* @param {string} key
58+
* @return {void}
59+
*/
60+
AllOne.prototype.dec = function(key) {
61+
const node = this.map.get(key);
62+
const count = node.count - 1;
63+
64+
node.keys.delete(key);
65+
66+
if (count === 0) {
67+
this.map.delete(key);
68+
} else {
69+
let prev = node.prev;
70+
if (prev.count !== count) {
71+
prev = { count, keys: new Set(), prev: node.prev, next: node };
72+
node.prev.next = prev;
73+
node.prev = prev;
74+
}
75+
prev.keys.add(key);
76+
this.map.set(key, prev);
77+
}
78+
79+
if (node.keys.size === 0) {
80+
node.prev.next = node.next;
81+
node.next.prev = node.prev;
82+
}
83+
};
84+
85+
/**
86+
* @return {string}
87+
*/
88+
AllOne.prototype.getMaxKey = function() {
89+
const node = this.tail.prev;
90+
return node === this.head ? '' : node.keys.values().next().value;
91+
};
92+
93+
/**
94+
* @return {string}
95+
*/
96+
AllOne.prototype.getMinKey = function() {
97+
const node = this.head.next;
98+
return node === this.tail ? '' : node.keys.values().next().value;
99+
};

0 commit comments

Comments
 (0)