查找两个有序数组中的第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 ...
随机推荐
- 跨站请求伪造(csrf),django的settings源码剖析,django的auth模块
目录 一.跨站请求伪造(csrf) 1. 什么是csrf 2. 钓鱼网站原理 3. 如何解决csrf (1)思路: (2)实现方法 (3)实现的具体代码 3. csrf相关的装饰器 (1)csrf_p ...
- Python可迭代序列反转总结
字符串反转 示例:s = "hello" 方法一:使用切片 def reversed_str(s): return s[::-1] 方法二:使用reversed # 字符串 -&g ...
- qt5-QPushButton按钮
Win::Win(QWidget *parent) //构造函数体 : QWidget(parent) //执行父类初始化操作 { //创建按钮方式一 ,);//重置窗口大小 this->set ...
- Eclipse快捷键和IDEA对比
- python基础--几个特性
1.helloword程序的解释 #!/usr/bin/python3 print("Hello, World!") 关于脚本第一行的 #!/usr/bin/python 的解释, ...
- 6、Lambda表达式(推荐使用)
Lambda表达式(匿名的函数对象),是C++11增加的新特性,Qt配合信号一起使用,非常方便. pro项目文件中引入了这种特性: CONFIG += c++11 通过connect来了解Lambda ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- AcWing:108. 奇数码问题(归并排序 + 逆序数)
你一定玩过八数码游戏,它实际上是在一个3×3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3×3的网格中. 例如: 5 2 8 1 3 _ 4 6 7 在游戏过程中,可以把空格与其上 ...
- TensorFlow自动求梯度
例1 import tensorflow as tf a=tf.Variable(tf.constant(1.0),name='a') b=tf.Variable(tf.constant(1.0),n ...
- spring boot V部落 V人事项目
公司倒闭 1 年多了,而我在公司倒闭时候做的开源项目,最近却上了 GitHub Trending,看着这个数据,真是不胜唏嘘. 缘起 2017 年 11 月份的时候,松哥所在的公司因为经营不善要关门了 ...