LintCode: Median of two Sorted Arrays
求第k个值
1.归并排序
归并到第k个值为止
时间复杂度:O(k)
class Solution {
public:
// merge-sort to find K-th value
double helper(vector<int> A, vector<int> B, int lenA, int lenB, int k) {
int i = , j = ;
while ((i < lenA) && (j < lenB)) {
k--;
if (A[i] < B[j]) {
if ( == k) {
return A[i];
}
++i;
} else if ( == k) {
return B[j];
} else {
++j;
}
}
return (i >= lenA)?B[j + k - ]:A[i + k - ];
}
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
// write your code here
int m = A.size();
int n = B.size();
return ((m + n) & )?
(helper(A, B, m, n, (m + n + )>>)):
(((helper(A, B, m, n, ((m + n)>>) + ))+(helper(A, B, m, n, (m + n)>>))) * .);
}
};
2. 分治法
利用归并的思想,从a和b中一共取k个数
假设len(a)<len(b)
从a中取pa = min(k/2, len(a))个元素;
从b中取pb = k-pa个元素;
如果a[pa - 1]<b[pb - 1],则归并排序时先归并a[pa - 1],说明a数组的数取“少”了,不够用
a到终点后,只用b的数凑足了k个,这说明总的第k大的值不会出现在a[0...pa-1]里边,所以我们扔掉前pa个数
对于b数组,说明总的第k大的数不会出现在b[pb...lenB]里边,pb后边的数就没用了
如果a[pa - 1]>=b[pb - 1],是对称情况
归并排序时,会先归并b[pb - 1],说明b数组的数取“少”了,不够用
b到终点后,只用a的数凑足了k个,这说明总的第k大的值不会出现在b[0...pb-1]里边,所以我们扔掉前pb个数
对于a数组,说明第k大的数不会出现在a[pa...lenA]里边,pa的后边就没用了
总结:扔掉较小数组的前一部分,扔掉较大数组的后一部分
复杂度分析:O(logK), K每次几乎减少一半
class Solution {
public:
// merge-sort to find K-th value
double helper(int *A, int *B, int lenA, int lenB, int k) {
if (lenA > lenB) {
return helper(B, A, lenB, lenA, k);
}
// lenA <= lenB
if (lenA == ) {
return B[k - ];
}
if ( == k) {
return min(A[], B[]);
}
int pa = min(lenA, k >> ), pb = k - pa;
return (A[pa - ] < B[pb - ])?
helper(A + pa, B, lenA - pa, lenB, k - pa):
helper(A, B + pb, lenA, lenB - pb, k - pb);
}
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
// write your code here
int m = A.size();
int n = B.size();
return ((m + n) & )?
(helper(A.data(), B.data(), m, n, (m + n + )>>)):
(((helper(A.data(), B.data(), m, n, ((m + n)>>) + ))+(helper(A.data(), B.data(), m, n, (m + n)>>))) * .);
}
};
LintCode: Median of two Sorted Arrays的更多相关文章
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
随机推荐
- Android 程序目录介绍
还是回到 Eclipse 中,首先展开 HelloWorld 项目,你会看到如图 1.17 所示的目录结构. 图 1.17 一开始看到这么多陌生的东西,你一定会感到有点头晕吧.别担心,我现在就对上 ...
- excel 批注
Excel VBA之Name对象.Comment对象及字体设置等,点滴学习积累 存在的方式 百家号11-1518:46 ======================================== ...
- 【docker】docker的简单状态监控
命令: docker stats 可以使用占位符,显示想要看的信息: docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{ ...
- Unity3D 经常使用库
JSON.NET:http://james.newtonking.com/json LitJSON: http://lbv.github.io/litjson/ ProtoBuf - net:htt ...
- linux配置nginx
相关命令: nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试ngin ...
- Java中CAS详解
在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题. (2 ...
- [DEFCON全球黑客大会] CTF(Capture The Flag)
copy : https://baike.baidu.com/item/ctf/9548546?fr=aladdin CTF(Capture The Flag)中文一般译作夺旗赛,在网络安全领域中指的 ...
- 关于GreenPlum的一些整理
Greenplum数据库架构 Greenplum数据库基本由PostgreSQL核心增强数据库实例组合并衔接成的数据库管理系统,即Greenplum数据在PostgreSQL基础上扩展开发,每个Gre ...
- LaTeX技巧24:LaTeX常用命令集锦
\hyphenation{word list} %断字命令:\showthe\topmargin %显示某个参数的数值或者内容: 在tex编译过程中出现行溢出(overflow hbox)是由于断字程 ...
- 指定文件兼容性模式 < meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />的意义
X-UA-Compatible是神马? X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中 ...