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 题目详情 ...
随机推荐
- 【转载】jxl的使用总结(java操作excel)
jxl.jar是通过java操作excel表格的工具类库: 链接:https://pan.baidu.com/s/1AAT_eA_Q47zFeQohap6eQg 提取码:777b 1:通过模拟实现创建 ...
- Django - 自定义simple_tag
使用现有函数: 通过对传入的参数,后面跟一个管道符号+python函数,来完成对传入参数的修改. 返回值 自定义simple_tag: 具体操作步骤如下: 1.在某个app下,创建目录template ...
- <MyBatis>入门六 动态sql
package org.maple.mapper; import org.apache.ibatis.annotations.Param; import org.maple.pojo.Employee ...
- Vim常用快捷键--正常的学习曲线
vim可能对于初学者不太友好,学习曲线有点陡,特此整理了较为平滑的学习曲线的学习快捷键的方式,包含最常用的快捷键,让初学者领悟vim的优点,想要进阶学习请查找其它更好的教程 正常模式:可以使用快捷键命 ...
- Network----轮询
轮询: 定时每隔多长时间刷新一次,但是,7X24的对服务器的压力会过大,因为在夜间或者是流量低峰期时,他还要持续工作. 客户端发一次请求,服务器就要相应一次. 长轮询: 和轮询的模式不同,长轮询是一次 ...
- F - Shooter
UVA___10535 The shooter is in a great problem. He is trapped in a “2D” maze with a laser gun and can ...
- 【Codeforces 1030D】Vasya and Triangle
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 参考这篇题解:https://blog.csdn.net/mitsuha_/article/details/82825862 为什么可以保证m ...
- 【Codeforces 493C】Vasya and Basketball
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举三分线(离散后)的位置 然后根据预处理的前缀和,快速算出两个队伍的分数. [代码] #include <bits/stdc++.h& ...
- JavaSE 学习笔记之Jdk5.0新特性(十九)
Jdk5.0新特性: Collection在jdk1.5以后,有了一个父接口Iterable,这个接口的出现的将iterator方法进行抽取,提高了扩展性. --------------------- ...
- centos7 安装mongodb3.4 及用户管理
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/1.semanage command not found yum ...