查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)
查找两个有序数组中的第K个元素
int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend)
{
int aLen = aend - astart + ;
int bLen = bend - bstart + ; if (aLen == )
{
return b[bstart + k];
} if (bLen == )
{
return a[astart + k];
} if (k == )
{
return a[astart] > b[bstart] ? b[bstart] : a[astart] ;
} int amid = aLen * k / (aLen + bLen); //按比例,算出分界点
int bmid = k - amid - ; amid += astart;
bmid += bstart; if (a[amid] > b[bmid])
{
k -= (bmid - bstart + );
aend = amid;
bstart = bmid + ;
}
else
{
k -= (amid - astart + );
bend = bmid;
astart = amid + ;
}
return FindKth(a, b, k, astart, aend, bstart, bend);
} int main(int argc, char* argv[])
{
int a[] = {, , , , , , };
int b[] = {, , , }; printf("\nfind 0th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 1th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 2th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 3th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 4th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 5th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 6th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 7th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 8th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 9th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 10th it=%d\n", FindKth(a, b, , , , , )); getchar();
return ;
}
查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)的更多相关文章
- 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, respec ...
- [转载]寻找两个有序数组中的第K个数或者中位数
http://blog.csdn.net/realxie/article/details/8078043 假设有长度分为为M和N的两个升序数组A和B,在A和B两个数组中查找第K大的数,即将A和B按升序 ...
- 选取两个有序数组中最大的K个值,降序存入另一个数组中
原题: 假设有两个有序的整型数组int *a1, int *a2,长度分别为m和n.试用C语言写出一个函数选取两个数组中最大的K个值(K可能大于m+n)写到int *a3中,保持a3降序,并返回a3实 ...
- Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解
题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 两个有序数组中的中位数以及求第k个最小数的值
解法参考 <[分步详解]两个有序数组中的中位数和Top K问题> https://blog.csdn.net/hk2291976/article/details/51107778 里面求中 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 如何寻找无序数组中的第K大元素?
如何寻找无序数组中的第K大元素? 有这样一个算法题:有一个无序数组,要求找出数组中的第K大元素.比如给定的无序数组如下所示: 如果k=6,也就是要寻找第6大的元素,很显然,数组中第一大元素是24,第二 ...
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- AttributeError: module 'Crypto.PublicKey.RSA' has no attribute 'import_key'
I solved this problem by pip install pycryptodome
- k8s存储卷概述
pod本身具有生命周期,故其内部运行的容器及其相关数据自身均无法持久存在.docker支持配置容器使用存储卷将数据持久存储于容器自身文件系统之外的存储空间中,它们可以是节点文件系统或网络文件系统之上的 ...
- dlerror和dlclose用法
dlclose() 1. 包含头文件 #include<dlfcn.h> 2. 函数定义 int dlclose(void *handle) dlclose用于关闭指定句柄的动态链接库, ...
- 23. ClustrixDB AUTO_UNIQUE
AUTO_INCREMENT 许多表具有使用AUTO_INCREMENT自动填充的代理键.ClustrixDB也支持这个MySQL特性,并在将记录插入表时创建惟一的ID.这些生成的id单调递增. 惟一 ...
- 信息提示框:MessageBox
一 函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; 1.参数列表 hW ...
- codevs 5960 信使x
题目描述 Description •战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单位).指挥部设在第一个哨所. ...
- 【HDOJ5447】Good Numbers(数论)
题意: 思路:From https://blog.csdn.net/qq_36553623/article/details/76683438 大概就是把1e6里面的质因子能除的都除光之后借助两者gcd ...
- html密码框value为空,但是总有默认密码(原)
input输入框加属性:autocomplete="new-password" ,浏览器就不会给他填充默认密码. <input class="form-contro ...
- JS框架_(coolShow.js)图片旋转动画特效
百度云盘 传送门 密码:ble6 coolShow.js插件图片旋转动画效果 <!DOCTYPE HTML> <head> <meta http-equiv=" ...
- [CSP-S模拟测试]:爬(贪心)
题目传送门(内部题134) 输入格式 第一行两个数$N,L$. 接下来$N$行每行两个数$A_i,B_i$. 接下来$N$行每行一个整数$C_i$. 输出格式 一行一个整数表示答案,无解输出$-1$. ...