1
+ /**
2
+ * Hackerrank
3
+ * This question is designed to help you get a better understanding of basic
4
+ * heap operations. You will be given queries of types:
5
+ * 1. '1 v' - Add an element v to the heap.
6
+ * 2. '2 v' - Delete the element from the heap.
7
+ * 3. '3' - Print the minimum of all the elements in the heap.
8
+ * NOTE: It is guaranteed that the element to be deleted will be there in the
9
+ * heap. Also, at any instant, only distinct elements will be in the heap.
10
+ *
11
+ * Constraints:
12
+ * 1. 1 <= Q <= 10^5 - Q (number of queries)
13
+ * 2. -10^9 <= v <= 10^9
14
+ * 3. All the characters in the sequence: {, }, [, ], (, )
15
+ *
16
+ * This solution got 25 points
17
+ * Problem link: http://hr.gs/eceaeb
18
+ */
19
+
20
+ /**
21
+ *
22
+ * @param {string } input
23
+ */
24
+ function processData ( input ) {
25
+ const queries = input . split ( '\n' )
26
+ . slice ( 1 )
27
+ . map ( query => query . split ( ' ' ) ) ;
28
+
29
+ if ( + queries [ 0 ] [ 0 ] !== 1 ) {
30
+ return ;
31
+ }
32
+
33
+ const heap = new Heap ( + queries [ 0 ] [ 1 ] ) ;
34
+ queries . slice ( 1 ) . forEach ( query => {
35
+ switch ( + query [ 0 ] ) {
36
+ case 1 :
37
+ heap . insert ( + query [ 1 ] ) ;
38
+ break ;
39
+ case 2 :
40
+ heap . delete ( + query [ 1 ] ) ;
41
+ break ;
42
+ case 3 :
43
+ heap . peek ( ) ;
44
+ break ;
45
+ }
46
+ } ) ;
47
+ }
48
+
49
+ class Heap {
50
+ constructor ( root ) {
51
+ this . priorityQueue = [ null ] ;
52
+ if ( root != null ) {
53
+ this . priorityQueue . push ( root ) ;
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Order the array in order to turn it into a min heap
59
+ */
60
+ heapify ( ) {
61
+ for ( let i = Math . floor ( ( this . priorityQueue . length - 1 ) / 2 ) ; i > 0 ; i -- ) {
62
+ this . minHeapify ( i ) ;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Check if a node meet the Min Heap criteria and perfom several swaps in case it doesn't
68
+ * @param {number } i - Index of the node to be checked
69
+ */
70
+ minHeapify ( i ) {
71
+ const leftChild = i * 2 ;
72
+ const rightChild = i * 2 + 1 ;
73
+
74
+ if ( leftChild < this . priorityQueue . length ) {
75
+ let lowestI = i ;
76
+ if ( this . priorityQueue [ leftChild ] < this . priorityQueue [ lowestI ] ) {
77
+ lowestI = leftChild ;
78
+ }
79
+
80
+ if ( this . priorityQueue [ rightChild ] < this . priorityQueue [ lowestI ] ) {
81
+ lowestI = rightChild ;
82
+ }
83
+
84
+ if ( lowestI !== i ) {
85
+ swap ( this . priorityQueue , i , lowestI ) ;
86
+ this . minHeapify ( lowestI ) ;
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Insert a new value on the heap and validate that queue meet the Min Heap
93
+ * criteria
94
+ * @param {number } val - Value to be inserted in the heap
95
+ */
96
+ insert ( val ) {
97
+ this . priorityQueue . push ( val ) ;
98
+ this . heapify ( ) ;
99
+ }
100
+
101
+ /**
102
+ * Delete the given value from the heap and validate that queue meet the Min Heap
103
+ * criteria
104
+ * @param {number } val - Value to be removed from the heap
105
+ */
106
+ delete ( val ) {
107
+ const i = this . priorityQueue . indexOf ( val ) ;
108
+ if ( i > - 1 ) {
109
+ const size = this . priorityQueue . length ;
110
+ const lastLeaf = this . priorityQueue . pop ( ) ;
111
+ if ( i < this . priorityQueue . length ) {
112
+ this . priorityQueue [ i ] = lastLeaf ;
113
+ this . minHeapify ( i ) ;
114
+ }
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Print the root of the Min Heap
120
+ */
121
+ peek ( ) {
122
+ if ( this . priorityQueue . length > 1 ) {
123
+ console . log ( this . priorityQueue [ 1 ] ) ;
124
+ }
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Swap two elements in an given array
130
+ * @param {number[] } arr - Array where changes will be perfomed
131
+ * @param {number } org - Original index to be swapped
132
+ * @param {number } next - New index to be swapped
133
+ */
134
+ function swap ( arr , org , next ) {
135
+ const prev = arr [ org ] ;
136
+ arr [ org ] = arr [ next ] ;
137
+ arr [ next ] = prev ;
138
+ }
0 commit comments