「这是我参与2022首次更文挑战的第13天,活动详情查看:2022首次更文挑战」。
系统提供的堆无法做到的事情:
- 已经入堆的元素,如果参与排序的指标方法变化,系统提供的堆无法做到时间复杂度O(logN)调整!都是O(N)的调整!
- 系统提供的堆只能弹出堆顶,做不到自由删除任何一个堆中的元素,或者说,无法在时间复杂度O(logN)内完成!一定会高于O(logN),根本原因:无反向索引表
一、分析
手动改写堆:
- 建立反向索引表
- 建立比较器
- 核心在于各种结构相互配合
二、实现
public class HeapGreater<T> {
private ArrayList<T> heap;
private HashMap<T, Integer> indexMap;
private int heapSize;
private Comparator<? super T> comp;
public HeapGreater(Comparator<T> c) {
heap = new ArrayList<>();
indexMap = new HashMap<>();
heapSize = 0;
comp = c;
}
public boolean isEmpty() {
return heapSize == 0;
}
public int size() {
return heapSize;
}
public boolean contains(T obj) {
return indexMap.containsKey(obj);
}
public T peek() {
return heap.get(0);
}
public void push(T obj) {
heap.add(obj);
indexMap.put(obj, heapSize);
heapInsert(heapSize++);
}
public T pop() {
T ans = heap.get(0);
swap(0, heapSize - 1);
indexMap.remove(ans);
heap.remove(--heapSize);
heapify(0);
return ans;
}
public void remove(T obj) {
T replace = heap.get(heapSize - 1);
int index = indexMap.get(obj);
indexMap.remove(obj);
heap.remove(--heapSize);
if (obj != replace) {
heap.set(index, replace);
indexMap.put(replace, index);
resign(replace);
}
}
public void resign(T obj) {
heapInsert(indexMap.get(obj));
heapify(indexMap.get(obj));
}
// 请返回堆上的所有元素
public List<T> getAllElements() {
List<T> ans = new ArrayList<>();
for (T c : heap) {
ans.add(c);
}
return ans;
}
private void heapInsert(int index) {
while (comp.compare(heap.get(index), heap.get((index - 1) / 2)) < 0) {
swap(index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
private void heapify(int index) {
int left = index * 2 + 1;
while (left < heapSize) {
int best = left + 1 < heapSize && comp.compare(heap.get(left + 1), heap.get(left)) < 0 ? (left + 1) : left;
best = comp.compare(heap.get(best), heap.get(index)) < 0 ? best : index;
if (best == index) {
break;
}
swap(best, index);
index = best;
left = index * 2 + 1;
}
}
private void swap(int i, int j) {
T o1 = heap.get(i);
T o2 = heap.get(j);
heap.set(i, o2);
heap.set(j, o1);
indexMap.put(o2, i);
indexMap.put(o1, j);
}
}
三、总结
T一定要是非基础类型,有基础类型需求包一层
四、扩展
最大线段重合问题
给定很多线段,每个线段都有两个数[start, end], 表示线段开始位置和结束位置,左右都是闭区间 规定:
- 线段的开始和结束位置一定都是整数值
- 线段重合区域的长度必须>=1,返回线段最多重合区域中,包含了几条线段
方法一:
找出所有线段开始的最小min,结束的最大max,因为线段重合区域的长度必须大于等于1,且线段的开始和结束位置都是整数,所以假设以0.5的步长增加,比如1、1.5、2、2.5、3、3.5......
有这么多的0.5 = (max - min) ,每个0.5范围内,找重合区域,求max就是答案,时间复杂度O((max - min) * N)
方法二:
利用小根堆,把线段开始位置从小到大排序,依次考察每个线段,遍历到任何线段时,在小根堆里把所有小于等于开始位置的值全弹出,然后把此时线段的结束位置加入到小根堆里,看小根堆里有几个数就是这个线段的答案,所有线段的答案都求出来,max就是最大线段重合