第k大问题各类变种小结
声明
文章均为本人技术笔记,转载请注明出处:
[1] https://segmentfault.com/u/yzwall
[2] blog.csdn.net/j_dark/
1. 求第k大数
- lintcode 5 Kth Largest Element
- lintcode 606 Kth Largest Element II
- lintcode 461 Kth Smallest Numbers in Unsorted Array(第k小问题,切分时与求第k大相反)
1.1 解法1:Partition O(n) 复杂度解法
1.1.1 分治法思想
参考快速排序中的切分思想,切分花费 O(n) 时间,分治将问题规模缩小为 T(n2)
T(n)=O(n)+T(n/2)=O(n)
求第k大数问题,找到轴点 pivot ,在区间[start, end]中,
1. 切分(partition)操作:求第k大数问题,将所有比 pivot 小的放在 pivot左