TopK 问题
程序员文章站
2022-03-24 17:26:50
...
TopK 问题
package p56;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* TopK 最小的k个数
* @author Guozhu Zhu
* @date 2019/4/13
* @version 1.0
*
*/
public class Solution01 {
/* ========== Test ========== */
public static void main(String[] args) {
int[] arr = new int[] {1, 3, 2, 4, 6, 5, 9};
ArrayList<Integer> res = method03(arr, 4);
System.out.println(res);
}
//1. quickSort T(o) = nlogn
public static ArrayList<Integer> method01(int[] arr, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (arr.length == 0 || k > arr.length) {
return res;
}
quickSort(arr, 0, arr.length-1);
for (int i = 0; i < k; i++) {
res.add(arr[i]);
}
return res;
}
public static void quickSort(int[] arr, int left, int right) {
if (left > right) {
return;
}
int i = left;
int j = right;
int k = arr[left];
while (i < j) {
while (i < j && arr[j] >= k) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= k) {
i++;
}
arr[j] = arr[i];
}
arr[i] = k;
quickSort(arr, left, i-1);
quickSort(arr, i+1, right);
}
//2. 部分排序, 冒泡, T(o) = n*k
public static ArrayList<Integer> method02(int[] arr, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (arr.length == 0 || k > arr.length) {
return res;
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j+1] > arr[j]) {
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = arr.length-1; i > arr.length-1-k; i--) {
res.add(arr[i]);
}
return res;
}
//3. 大顶堆, T(o) = nlogk
public static ArrayList<Integer> method03(int[] arr, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (arr.length == 0 || k > arr.length) {
return res;
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2.compareTo(o1);
}
});
for (int i = 0; i < arr.length; i++) {
if (maxHeap.size() < k) {
maxHeap.offer(arr[i]);
} else if (maxHeap.peek() > arr[i]) {
maxHeap.poll(); //最大的poll掉
maxHeap.offer(arr[i]);
}
}
for (Integer i : maxHeap) {
res.add(i);
}
/*for (int i = 0; i < k; i++) {
res.add(maxHeap.poll());
}*/
Collections.reverse(res);
return res;
}
}
package p56;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* TopK 最大的k个数
* @author Guozhu Zhu
* @date 2019/4/13
* @version 1.0
*
*/
public class Solution02 {
/* =========== Test ========== */
public static void main(String[] args) {
int[] arr = new int[] {1, 4, 3, 2, 9, 8, 0, 5};
ArrayList<Integer> res = method03(arr, 4);
System.out.println(res);
}
//1. 快速排序全排序
public static ArrayList<Integer> method01(int[] arr, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (arr.length == 0 || k > arr.length) {
return res;
}
quickSort(arr, 0, arr.length-1);
for (int i = arr.length-1; i > arr.length-1-k; i--) {
res.add(arr[i]);
}
return res;
}
public static void quickSort(int[] arr, int left, int right) {
if (left > right) {
return ;
}
int i = left;
int j = right;
int k = arr[i];
while (i < j) {
while (i < j && arr[j] >= k) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= k) {
i++;
}
arr[j] = arr[i];
}
arr[i] = k;
quickSort(arr, left, i-1);
quickSort(arr, i+1, right);
}
//2. 部分排序,
public static ArrayList<Integer> method02(int[] arr, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (arr.length == 0 || k > arr.length) {
return res;
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j+1] < arr[j]) {
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = arr.length-1; i > arr.length-1-k; i--) {
res.add(arr[i]);
}
return res;
}
//3. 堆排序
public static ArrayList<Integer> method03(int[] arr, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (arr.length == 0 || k > arr.length) {
return res;
}
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o1.compareTo(o2);
}
});
for (int i = 0; i < arr.length; i++) {
if (minHeap.size() < k) {
minHeap.offer(arr[i]);
} else if (arr[i] > minHeap.peek()) {
minHeap.poll();
minHeap.offer(arr[i]);
}
}
for (int i = 0; i < k; i++) {
res.add(0, minHeap.poll());
}
return res;
}
}
上一篇: 数据结构——线性结构
下一篇: 数据结构-->结构体