-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
91 lines (77 loc) · 3.07 KB
/
Solution.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
class Solution {
class MaxHeap {
class Node {
Object item;
int priority;
Node(Object item, int priority) {
this.item = item;
this.priority = priority;
}
}
ArrayList<Node> heap;
MaxHeap() {
this.heap = new ArrayList<Node>();;
}
void push(Object item, int priority) {
heap.add(new Node(item, priority));
int pos = heap.size() - 1;
int pPos = (pos % 2 == 0 ? pos - 2 : pos - 1) / 2;
while (pPos >= 0 && priority > heap.get(pPos).priority) {
Node temp = heap.get(pPos);
heap.set(pPos, heap.get(pos));
heap.set(pos, temp);
pos = pPos;
pPos = (pos % 2 == 0 ? pos - 2 : pos - 1) / 2;
}
}
int size() {
return heap.size();
}
Object pop() {
int size = heap.size();
if (size == 0)
throw new ArrayIndexOutOfBoundsException("pop from empty list");
else if (size == 1)
return heap.remove(0).item;
Object item = heap.get(0).item;
int pos = 0;
int maxChildPos = pos * 2 + 2;
heap.set(0, heap.remove(size - 1));
if (maxChildPos < size - 1)
maxChildPos = heap.get(maxChildPos).priority > heap.get(maxChildPos - 1).priority ? maxChildPos : maxChildPos - 1;
else
maxChildPos = maxChildPos - 1 < size - 1 ? maxChildPos - 1 : pos;
while (maxChildPos < size - 1 && heap.get(pos).priority < heap.get(maxChildPos).priority) {
Node temp = heap.get(maxChildPos);
heap.set(maxChildPos, heap.get(pos));
heap.set(pos, temp);
pos = maxChildPos;
maxChildPos = pos * 2 + 2;
if (maxChildPos < size - 1)
maxChildPos = heap.get(maxChildPos).priority > heap.get(maxChildPos - 1).priority ? maxChildPos : maxChildPos - 1;
else
maxChildPos = maxChildPos - 1 < size - 1 ? maxChildPos - 1 : pos;
}
return item;
}
}
public String frequencySort(String s) {
Map<Character, Integer> charMap = new HashMap<Character, Integer>();
for (char c : s.toCharArray()) {
if (charMap.get(c) == null)
charMap.put(c, 1);
else
charMap.put(c, charMap.get(c) + 1);
}
MaxHeap maxHeap = new MaxHeap();
for (char c : charMap.keySet())
maxHeap.push(c, charMap.get(c));
StringBuilder sb = new StringBuilder();
while (maxHeap.size() > 0) {
char c = (char) maxHeap.pop();
for (int i = 0; i < charMap.get(c); i++)
sb.append(String.valueOf(c));
}
return sb.toString();
}
}