当前位置:网站首页>Byte interview programming question: the minimum number of K
Byte interview programming question: the minimum number of K
2022-04-23 15:09:00 【White horse is not a horse·】
1. Fast algorithm
Front of output k The lowest decimal , All ordered , The time complexity is O(nlogn), Space for O(logn)
package demo1;
import java.util.Arrays;
public class demo2 {
public static void main(String[] args) {
// Fast algorithm
int[] data=new int[]{
1,4,5,3,2,6,7,9,8};
int k=5;
System.out.println(Arrays.toString(data));
fastsort(data,0,data.length-1,k);
System.out.println(Arrays.toString(data));
}
public static void fastsort(int[] data,int first,int last,int k){
// A special case
if(first>last) return ;
// General situation
//1) Find a random number , Exchange on this basis
int left=first;
int right=last;
int temp=data[first]; // The selected benchmark
while(left<right){
//1) Find the object to exchange ( If the left side is the datum , So start on the right and look for )
while(left<right && data[right]>temp) right--;
while(left<right && data[left]<=temp) left++;
//2) Exchange when you find it
if(left<right){
int temp1=data[left];
data[left]=data[right];
data[right]=temp1;
}
}
// Change the position of the benchmark
data[first]=data[left];
data[left]=temp;
// Continue recursion ( With left For boundaries )
if(left+1<k){
fastsort(data,left+2,last, k-left-1);
}else if(left==k){
return;
}else{
fastsort(data,first,left,k);
}
}
}
2. Heap sort
1) Small top heap sorting algorithm ( Through an array , Big pile top , A complete binary tree with child nodes greater than or equal to child nodes )
2) Call to realize the collection of large top heap PriorityQueue, The default is small top heap , Rewritable Comparator Class in compare, Realize the big top reactor .
3) The time complexity is O(nlogn), Spatial complexity O(1)
package demo1;
import java.util.*;
public class demo4 {
public static void main(String[] args) {
List<Integer> list=new ArrayList<>(Arrays.asList(2,3,1,5,7,6,9,8,0));
int k=8;
PriorityQueue<Integer> queue=new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(Integer a,Integer b){
return a-b;
}
});
for(Integer i:list) queue.add(i);
System.out.println(queue);
for(int i=0;i<k;i++) System.out.print(queue.poll()+" ");
}
}
3. Improved fast scheduling algorithm
- Fast scheduling algorithm ( Front of output k The lowest decimal , disorder ), The time complexity is O(n), Space is a constant space
- Learning from the idea of fast platoon , There are multiple choices in recursive operations
package demo1;
import java.util.Arrays;
public class demo3 {
public static void main(String[] args) {
// rewrite : Fast algorithm
int[] data=new int[]{
2,6,7,9,8,1,4,5,3};
//int[] data=new int[]{3,2,4,1,5,8,7};
int k=4;
fastsort(data,0,data.length-1,k);
System.out.println("data="+ Arrays.toString(data));
int[] data1=Arrays.copyOfRange(data,0,k);
System.out.println("result="+ Arrays.toString(data1));
}
public static void fastsort(int[]data,int begin,int end,int k){
// A special case
if(begin>end) return;
// General situation
int left=begin;
int right=end;
int temp=begin;
while(left<right){
// Find something to exchange ( The reference value is... On the left , Look, start on the right )
while(left<right && data[right]>data[temp]) right--;
while(left<right && data[left]<=data[temp]) left++;
// Find it and exchange it
if(left<right){
int tem1=data[right];
data[right]=data[left];
data[left]=tem1;
}
}
// Then the reference value is placed in the position where it should be placed
int tem2=data[left];
data[left]=data[begin];
data[begin]=tem2;
System.out.println("data="+ Arrays.toString(data));
// Perform the following recursive operations ( Core code , Finding the middle position is the reference value
if(left+1<k){
fastsort(data,left+1,end,k);
}else if(left==k || left+1==k ){
return;
}else{
fastsort(data,begin,left,k);
}
}
}
It's easy to get wrong :
1) In the method , First of all, it should be right begin>end Discuss the situation
2) When the appropriate exchange value is found , If the reference value is the first , Then start from the right , Then look to the left
3) Notice the case of recursive selection .
版权声明
本文为[White horse is not a horse·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407525012.html
边栏推荐
- Flink datastream type system typeinformation
- Tencent has written a few words, Ali has written them all for a month
- JS - implémenter la fonction de copie par clic
- async void 导致程序崩溃
- 脏读、不可重复读和幻读介绍
- Go basic reflection
- MySQL sync could not find first log file name in binary log index file error
- LeetCode162-寻找峰值-二分-数组
- The win10 taskbar notification area icon is missing
- LeetCode149-直线上最多的点数-数学-哈希表
猜你喜欢
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
Tun model of flannel principle
LeetCode162-寻找峰值-二分-数组
MySQL error packet out of order
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
Leetcode162 - find peak - dichotomy - array
How to design a good API interface?
Redis master-slave synchronization
Daily question - leetcode396 - rotation function - recursion
Nuxt project: Global get process Env information
随机推荐
分布式事务Seata介绍
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
SQL中HAVING和WHERE的区别
How to upload large files quickly?
Async keyword
大文件如何快速上传?
Nacos program connects to mysql8 0+ NullPointerException
JUC学习记录(2022.4.22)
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
My raspberry PI zero 2W toss notes to record some problems and solutions
Brute force of DVWA low -- > High
牛客网数据库SQL实战详细剖析(26-30)
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
如何设计一个良好的API接口?
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
C语言超全学习路线(收藏让你少走弯路)
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
LeetCode 练习——396. 旋转函数
Redis主从同步
Share 20 tips for ES6 that should not be missed