-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
295.java
157 lines (136 loc) · 4.96 KB
/
295.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
__________________________________________________________________________________________________
sample 98 ms submission
class MedianFinder {
TreeNode head;
/** initialize your data structure here. */
public MedianFinder() {
}
public void addNum(int num) {
if (this.head == null) {
this.head = new TreeNode(num, 1, 0, 0);
return;
}
TreeNode current = this.head;
while (current != null) {
if (num == current.value) {
current.count++;
return;
} else if (num < current.value) {
current.leftNodes++;
if (current.left == null) {
current.left = new TreeNode(num, 1, 0, 0);
return;
}
current = current.left;
} else {
current.rightNodes++;
if (current.right == null) {
current.right = new TreeNode(num, 1, 0, 0);
return;
}
current = current.right;
}
}
}
public double findMedian() {
if (this.head == null) {
return Double.MIN_VALUE; // Invalid
}
TreeNode current = this.head;
int lowers = 0;
int highers = 0;
while (current != null) {
if (current.count > Math.abs((current.leftNodes + lowers) - (current.rightNodes + highers))) {
return (double) current.value;
} else if (current.count == Math.abs((current.leftNodes + lowers) - (current.rightNodes + highers))) {
if ((current.leftNodes + lowers) > (current.rightNodes + highers)) {
return (double) (current.value + this.rightMost(current.left).value) / 2;
} else {
return (double) (current.value + this.leftMost(current.right).value) / 2;
}
} else if ((current.leftNodes + lowers) > (current.rightNodes + highers)) {
highers += current.count + current.rightNodes;
current = current.left;
} else {
lowers += current.count + current.leftNodes;
current = current.right;
}
}
return Double.MIN_VALUE; // Invalid
}
private TreeNode leftMost(TreeNode node) {
while (node.left != null) {
node = node.left;
}
return node;
}
private TreeNode rightMost(TreeNode node) {
while (node.right != null) {
node = node.right;
}
return node;
}
private class TreeNode {
public int value;
public int count;
public int leftNodes;
public int rightNodes;
public TreeNode left;
public TreeNode right;
public TreeNode(int value, int count, int leftNodes, int rightNodes) {
this.value = value;
this.count = count;
this.leftNodes = leftNodes;
this.rightNodes = rightNodes;
}
}
}
//1256333493
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
__________________________________________________________________________________________________
sample 50468 kb submission
class MedianFinder {
/** initialize your data structure here. */
PriorityQueue<Integer> maxHeap;
PriorityQueue<Integer> minHeap;
public MedianFinder() {
maxHeap = new PriorityQueue<Integer>((a,b)->(b-a));
minHeap = new PriorityQueue<Integer>();
}
public void addNum(int num) {
if (maxHeap.size() == 0 || maxHeap.peek()>num) {
maxHeap.offer(num);
} else {
minHeap.offer(num);
}
if (Math.abs(maxHeap.size()-minHeap.size())>1) {
rebalance(maxHeap, minHeap);
}
}
private void rebalance(PriorityQueue<Integer> maxHeap, PriorityQueue<Integer> minHeap) {
PriorityQueue<Integer> large = maxHeap.size()>minHeap.size()? maxHeap: minHeap;
PriorityQueue<Integer> small = minHeap.size()<maxHeap.size()? minHeap: maxHeap;
small.offer(large.poll());
}
public double findMedian() {
PriorityQueue<Integer> large = maxHeap.size()>minHeap.size()? maxHeap: minHeap;
PriorityQueue<Integer> small = minHeap.size()<maxHeap.size()? minHeap: maxHeap;
if (large.size() == small.size()) {
return (double) (large.peek() + small.peek())/2;
} else {
return large.peek();
}
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
__________________________________________________________________________________________________