File tree Expand file tree Collapse file tree 2 files changed +11
-7
lines changed Expand file tree Collapse file tree 2 files changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -25,15 +25,19 @@ public T dequeue(){
25
25
}
26
26
27
27
public T peek (){
28
- shiftStacks ();
28
+ if (size () == 0 )
29
+ return null ;
30
+ if (stack2 .isEmpty ())
31
+ shiftStacks ();
29
32
return stack2 .peek ();
30
33
}
31
34
32
- /* Great helper function */
33
35
private void shiftStacks (){
34
- while (!stack1 .isEmpty ()){
35
- T temp = stack1 .pop ();
36
- stack2 .push (temp );
36
+ if (stack2 .isEmpty ()){ // shifting items while stack2 has elements in it would mess up our queue's ordering
37
+ while (!stack1 .isEmpty ()){
38
+ T temp = stack1 .pop ();
39
+ stack2 .push (temp );
40
+ }
37
41
}
38
42
}
39
43
}
Original file line number Diff line number Diff line change 11
11
* maxheap.size()-1 == minheap.size() always holds.
12
12
*/
13
13
public class EighteenPoint09 { //code from website
14
- private static PriorityQueue <Integer > maxHeap = new PriorityQueue <Integer >(50 , Collections .reverseOrder ()); // maxHeap contains all SMALL elements
15
- private static PriorityQueue <Integer > minHeap = new PriorityQueue <Integer >(50 ); // minHeap contains all LARGE elements
14
+ private static PriorityQueue <Integer > maxHeap = new PriorityQueue <Integer >(Collections .reverseOrder ()); // maxHeap contains all SMALL elements
15
+ private static PriorityQueue <Integer > minHeap = new PriorityQueue <Integer >(); // minHeap contains all LARGE elements
16
16
17
17
public static void addNum (int n ) {
18
18
if (maxHeap .isEmpty ())
You can’t perform that action at this time.
0 commit comments