Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)
题目原文
Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respectively, design an algorithm to find the kth largest key. The order of growth of the worst case running time of your algorithm should be logn, where n = n1 + n2.
Version 1 : n1 = n2 and k = n/2
Version 2: k = n/2
Version 3: no restrictions
分析:
这个题目是要求从两个有序数组中寻找第K大元素,直观解法就是把连个数组归并排序,这样时间复杂度就是O(n),但是题目只要求知道第K大元素,其余比K大的元素的排列顺序并不关心。
默认数组a和数组b都是从小到大排序的。不论第K大元素在a或b中,其右侧元素一定大于K,可考虑分别从a和b中排除右侧(k-1)/2个元素,剩下的就是在未排除区间寻找第(k-已排除元素)大元素。通过递归逐步缩小比较范围。具体算法见如下代码。
package week3; import java.util.Arrays;
import edu.princeton.cs.algs4.StdRandom; public class KthInTwoSortedArrays {
/**
* 寻找第K大元素
* @param a 数组a
* @param alo a的查找区间下界
* @param ahi a的查找区间上界
* @param b 数组b
* @param blo b的查找区间下界
* @param bhi b的查找区间上界
* @param k 当前查找区间的第k大元素
* @return
*/
private static int find(int[] a, int alo, int ahi,int[] b, int blo, int bhi, int k){
if(alo > ahi) return b[bhi-k+1];
if(blo > bhi) return a[ahi-k+1];
if(k==1) return a[ahi] > b[bhi]? a[ahi]:b[bhi];
//直接用bhi-(k-1)/2或ahi-(k-1)/2进行查找可能会将第K个元素给漏掉
int bt = bhi-(k-1)/2 > blo ? bhi-(k-1)/2:blo;
int at = ahi-(k-1)/2 > alo ? ahi-(k-1)/2:alo;
if(a[at] >= b[bt])
return find(a,alo,at-1,b,blo,bhi,k-(ahi-at+1));
else
return find(a,alo,ahi,b,blo,bt-1,k-(bhi-bt+1));
} public static void main(String[] args){
int n = 10;
int n1 = StdRandom.uniform(n);
int n2 = n-n1;
int[] a = new int[n1];
int[] b = new int[n2];
for(int i=0;i<n1;i++){
a[i] = StdRandom.uniform(100);
}
for(int i=0;i<n2;i++){
b[i] = StdRandom.uniform(100);
}
Arrays.sort(a);
Arrays.sort(b);
System.out.println("a="+Arrays.toString(a));
System.out.println("b="+Arrays.toString(b));
int[] c = new int[n];
int i = 0;
int j = 0;
int l = 0;
while(l<n){
if(i>=n1) c[l++] = b[j++];
else if(j>=n2) c[l++] = a[i++];
else{
if(a[i] <= b[j])
c[l++] = a[i++];
else
c[l++] = b[j++];
} }
System.out.println("c="+Arrays.toString(c)); int k =StdRandom.uniform(1,n);
int largestK = find(a,0,n1-1,b,0,n2-1,k);
System.out.println("第"+k+"大元素是:"+largestK);
}
}
Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)的更多相关文章
- Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...
- Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)
题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...
- Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list
题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...
- Coursera Algorithms week3 归并排序 练习测验: Counting inversions
题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...
- Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array
题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted ...
- 无序数组中用 快速排序的分治思想 寻找第k大元素
#include <stdio.h> int *ga; int galen; void print_a(){ ; i < galen; i++){ printf("%d & ...
- 寻找第K大的数(快速排序的应用)
有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数.给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在.测试样例:[1,3,5,2,2],5, ...
- 快速排序 && 寻找第K大(小)的数
参考:https://minenet.me/2016/08/24/quickSort.html 快速排序 利用分治法可将快速排序的分为三步: 在数据集之中,选择一个元素作为"基准" ...
- 215. 数组中的第K个最大元素 + 快速排序 + 大根堆
215. 数组中的第K个最大元素 LeetCode-215 另一道类似的第k大元素问题:https://www.cnblogs.com/GarrettWale/p/14386862.html 题目详情 ...
随机推荐
- Codeforces985E. Pencils and Boxes (单调队列)
题意:n个数 把他们放进一些盒子里 每个盒子最少放k个数 且最小和最大的差不能大于d 题解:显然排个序 对于当前点 存一下前面有哪些节点可以当作结尾 那么就可以枚举这些点的下一个点作为起点能否和当前点 ...
- adb 命令收藏学习地址
adb 命令相关的网页https://www.cnblogs.com/medsonk/p/8334847.htmlhttps://www.cnblogs.com/medsonk/p/6959658.h ...
- input file 美化的方法
css input[type=file] 样式美化,input上传按钮美化 2014年8月29日 113210次浏览 由于明天公司组织出去游玩,今天把这两天的博客都写了吧,今天的内容是input[ty ...
- 低版本ie兼容问题的解决方案
CSS hack \9 所有的IE10及之前 * IE7以及IE7以下版本的 _ IE6以及IE6以下版本的 !important 提升样式优先级权重 1.ie6,7 ...
- Kattis - missinggnomesD Missing Gnomes (思路题)
题目: 题意: 给出已经去除了几个数的一个序列,任务是将去除的数字插回去补全这个序列,输出字典序排在第一的那个补全的序列. 例如: 样例输入: 5 3 1 4 2 样例输出: 1 3 4 2 5 思路 ...
- Flask蓝图基本使用
Flask蓝图基本使用 Flask通过使用蓝图将视图函数模块化,使应用显得更加规整 比如我们的应用的视图函数包括用户相关和文章相关,那么我们可以通过建立两个py文件分别存储两类视图函数 user.py ...
- nginx+keepalived+consul 实现高可用集群
继 负载均衡 之 nginx+consul+consul template,我这次将使用2台虚拟机,来做一个简单的双机负载均衡试验. 试验目标: 1. 当参加负载均衡的子节点服务,有任何其中一个或多个 ...
- [K/3Cloud]调用动态表单时,传递自定义参数
插件中在调用动态表单时,通过DynamicFormShowParameter的CustomParams,增加自定义的参数. private void ShowMaterialStock() { obj ...
- HDU——1787 GCD Again
题意: 在一次acm竞赛之后,你花了一些时间去思考和尝试解决那些未解决的问题吗? 不知道?哦,当你想成为“大牛”的时候,你就必须这样做. 现在你会发现,这个问题是如此熟悉: 两个正整数a和b的最大GC ...
- python 除法总返回浮点
python 除法总返回浮点,要返回整型需要用//: print(type(4/2),type(4//2)) #<class 'float'> <class 'int'>