Skip to content

Commit dfec9d5

Browse files
author
hasibulislam999
committed
All-Oone-Data-Structure problem solved
1 parent 86878ea commit dfec9d5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Title: All-Oone-Data-Structure
3+
* Description: Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
4+
* Author: Hasibul Islam
5+
* Date: 06/04/2023
6+
*/
7+
8+
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
9+
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
10+
11+
function AllOne() {
12+
let m = new Map();
13+
let preOp = "start"; // record pre operation
14+
return { inc, dec, getMaxKey, getMinKey };
15+
function inc(k) {
16+
m.set(k, m.get(k) + 1 || 1);
17+
preOp = "inc";
18+
}
19+
function dec(k) {
20+
let occ = m.get(k);
21+
occ == 1 ? m.delete(k) : m.set(k, occ - 1);
22+
preOp = "dec";
23+
}
24+
function getMaxKey() {
25+
if (preOp != "max") m = stmvalue_de(m);
26+
preOp = "max";
27+
return m.keys().next().value || "";
28+
}
29+
function getMinKey() {
30+
if (preOp != "min") m = stmvalue_in(m);
31+
preOp = "min";
32+
return m.keys().next().value || "";
33+
}
34+
}
35+
36+
/**
37+
* Your AllOne object will be instantiated and called as such:
38+
* var obj = new AllOne()
39+
* obj.inc(key)
40+
* obj.dec(key)
41+
* var param_3 = obj.getMaxKey()
42+
* var param_4 = obj.getMinKey()
43+
*/

0 commit comments

Comments
 (0)