Skip to content

Commit af7161c

Browse files
committed
Runtime: 672 ms (Top 16.08%) | Memory: 83 MB (Top 51.75%)
1 parent 69f0ec1 commit af7161c

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
// Runtime: 672 ms (Top 16.08%) | Memory: 83 MB (Top 51.75%)
12
var FreqStack = function() {
23
//hashMap to keep track of the values being repeated
34
this.frequencyMap = {};
4-
5+
56
//List map to keep track of the sequence of the value being entered
67
this.listMap = {};
7-
8+
89
//Max Frequency variable to keep track of the max frequency
910
this.maxValueFrequency = 0;
1011
};
1112

12-
/**
13+
/**
1314
* @param {number} val
1415
* @return {void}
1516
*/
1617
FreqStack.prototype.push = function(val) {
1718
//if the hashMap doesn't have value being pushed then make a entry to it with 1 else increament by 1
1819
this.frequencyMap[val] = this.frequencyMap[val] ? this.frequencyMap[val]+1 : 1;
19-
20+
2021
//get the frequency of the value being pushed
2122
const currentValueFrequency = this.frequencyMap[val];
22-
23+
2324
//check if the current frequency is max or itself
2425
this.maxValueFrequency = Math.max(this.maxValueFrequency, currentValueFrequency);
25-
26+
2627
//if current value frequency is not in the listmap then make a new entry else push it
2728
if(!this.listMap[currentValueFrequency]) this.listMap[currentValueFrequency] =[val];
2829
else this.listMap[currentValueFrequency].push(val);
29-
30+
3031
};
3132

3233
/**
@@ -35,26 +36,26 @@ FreqStack.prototype.push = function(val) {
3536
FreqStack.prototype.pop = function() {
3637
//make a temporary list of the max value frequency
3738
const tempList = this.listMap[this.maxValueFrequency];
38-
39-
//get the last element from the temporary list
39+
40+
//get the last element from the temporary list
4041
const top = tempList[tempList.length - 1];
41-
42+
4243
//remove the item from the list
4344
tempList.pop();
44-
45+
4546
//update the list
4647
this.listMap[this.maxValueFrequency] = tempList;
47-
48+
4849
//if the popped item exist in the frequecy map then decrease it by 1
4950
if(this.frequencyMap[top]) this.frequencyMap[top]--;
50-
51+
5152
//if the max value frequency in the listmap is blank then reduce the maxValueFrequency;
5253
if(this.listMap[this.maxValueFrequency].length === 0) this.maxValueFrequency--;
53-
54+
5455
//return the max value frequency with proper order if it is same
5556
return top;
56-
57+
5758
};
5859

5960
//Time O(Log n) // for both Push and Pop
60-
//Space O(n) for storing all the values in the hashMap and listMap
61+
//Space O(n) for storing all the values in the hashMap and listMap

0 commit comments

Comments
 (0)