-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsistentHash.java
More file actions
49 lines (49 loc) · 1.5 KB
/
Copy pathConsistentHash.java
File metadata and controls
49 lines (49 loc) · 1.5 KB
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
package leetcode;
import java.util.*;
public class ConsistentHash {
public List<List<Integer>> consistentHashing(int n) {
// Write your code here
PriorityQueue<ArrayList<Integer>> pq = new PriorityQueue<ArrayList<Integer>>(n, new Comparator<ArrayList<Integer>>() {
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
int diff = (o1.get(1) - o1.get(0)) - (o2.get(1) - o2.get(0));
if(diff == 0){
return o1.get(2) - o2.get(2);
}
else if (diff < 0) {
return 1;
}
else{
return -1;
}
}
});
pq.add(new ArrayList<Integer>(Arrays.asList(0,359,1)));
int maxId = 2;
while(maxId <= n){
ArrayList<Integer> range = pq.poll();
int x = range.get(0);
int y = range.get(1);
int id = range.get(2);
ArrayList<Integer> first = new ArrayList<Integer>(Arrays.asList(x,(x+y)/2,id));
ArrayList<Integer> second = new ArrayList<Integer>(Arrays.asList((x+y)/2+1,y,maxId++));
pq.offer(first);
pq.offer(second);
}
List<List<Integer>> ret = new ArrayList<List<Integer>>();
while(!pq.isEmpty()){
ret.add(pq.poll());
}
Collections.sort(ret, new Comparator<List<Integer>>() {
public int compare(List<Integer> o1, List<Integer> o2) {
return o1.get(0) - o2.get(0);
}
});
return ret;
}
public static void main(String[] args) {
ConsistentHash ch = new ConsistentHash();
for(List<Integer> a:ch.consistentHashing(6)){
System.out.println(a);
}
}
}