find the kth largest element in a list and find the first k largest elements in a list
来源:互联网 发布:台湾中视源码 编辑:程序博客网 时间:2024/04/30 19:56
Problem 1: Given an unordered list, find the kth largest element in this list.
Solution 1
sort this list and you can immediately get the kth largest element.
The time-complexity is O(NlogN).
Solution 2
pythonic pseudocode:
find_kth(list, k):
create a container H with a fixed size k.
Fullfill H with the first k elements in the specified list.
for e in list[k: N -1]:
min = smallest(H)
if e is larger than min:
delete min from H
add e into H
return H[k - 1]
Time-complexity: O(kN).
A better choice of implemention of the container in above code is Heap, which is easy to find the minimum element, delete it and add a new element.
Solution 3: quick select alogrithm
Quick select alogrithm is very similar to quick sort.
Pythonic pseudocode:
quick_select(list, k):
pivot = list[0].
split list into two parts(smaller and larger), one contains elements equal to or smaller than pivot, the other one contains elements larger than pivot.
if len(larger) == k:
return larger[k-1]
elif len(larger) > k:
return quick_select(larger, k)
else:
return quick_select(smaller, k - len(larger)
Time Complexity: O(N)
Implementions:
erlang code:
java code:
Problem 2: Given an unordered list, find the first k largest elements in this list.
This problem is similar to the first problem. Actually, if we examine the three solutions of the first problem, we can see that we alread get the first k largest elements for finding the kth largest element . So we have solved this problem.
- find the kth largest element in a list and find the first k largest elements in a list
- Find the second largest element in a Binary Search Tree
- Find the kth largest element in an unsorted array
- 1007. Find the middle element in a Linked List
- Find the Minimum Element in A sorted and Rotated Array
- Q2.2 Find the kth to last element of a singly linked list
- To find the kth to Last Element of a Singly Linked List
- Find the largest square fits a rectangle
- Un-sorted list, find the "k" largest value, the original list can be changed
- the kth largest element in an unsorted array.
- The kth largest element in max-heap 最大堆的第k大元素
- Could not find a price list in Ordered UOM xxx and Primary UOM of the item
- find kth largest number in an array
- 1、Find the last element of a list.
- 代码面试题:Find the first element in a given string A that also appears in another given string B
- 515. Find Largest Element in Each Row
- leetcode 215: the Kth Largest in array
- 编程之 最小的K个数 216 Kth Largest Element in an Array 378. Kth Smallest Element in a Sorted Matrix
- PHP有感
- Hibernate & Proxool or C3p0
- 央行超级网银上线对银行和第三方支付平台的影响
- 递归遍历某个目录下的所有java文件
- 收藏---很實用到類 (防止sql注入,字符串操作等)
- find the kth largest element in a list and find the first k largest elements in a list
- 敏捷的核心:消除浪费,走向精益
- IT系统选型也要务实
- redis做带分页的列表缓存
- JSP+Tomcat 6.0 JDBC连接MySQL数据库设置
- CodeIgniter Model间调用和耦合性冲突问题
- local_listener与remote_listener详述
- Android拨打电话和发短信的简单程序
- 用“爱生活”来搜索周边的美食、娱乐热点,很快捷,出行必备