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 ...
随机推荐
- 浏览器中回车(Enter)和刷新的区别是什么?[转载]
在浏览器中回车和F5刷新有什么区别那?今天就来说说:浏览器中回车(Enter)和刷新的区别是什么? 这点事. 概论: 1.回车在 Expires有效的时候,是不会去请求服务器的,打开调试看到的请求也只 ...
- algid parse error, not a sequence错误
主要使用由于没有对使用openssl生成的公私密钥对进行pkcs8编码,导致程序无法识别参考支付宝.项目用用到RSA加密用openssl生成了一个公私密钥对,在对加密字符串进行数字签名的时候,程序一直 ...
- Xcode打包踩过的那些坑
一.file was built for archive which is not the architecture being linked (armv7s) 项目是基于cocos2d-x绑定lua ...
- [转]php-mobile-detect
转自:http://www.oschina.net/p/php-mobile-detect php-mobile-detect (Mobile_Detect) 是一个 PHP 类,用来通过 User- ...
- [转]Linux常用命令大全
From : http://www.php100.com/html/webkaifa/Linux/2009/1106/3485.html 系统信息 arch 显示机器的处理器架构(1) uname - ...
- RFC 868 -- TIME Protocol
INTERNET STANDARD Errata Exist Network Working Group J. Postel - ISI Request for Comments: 868 K. Ha ...
- 使用idea 在springboot添加本地jar包的方法 部署的时候本地jar没有包含的解决方法
需要添加dependency 和resources 否则发布的时候可能会缺少jar <dependency> <groupId>com.sap</groupId> ...
- cocos2d-js中Chipmunk物理引擎相关(1)
近期看些cocos2d-js的东西.用到当中的Chipmunk的一些东西.由于相关的资料也不是非常具体,所以看到一些东西实用就记录下来. 1. chipmunk是cocos2d的一个一个物理引擎.用来 ...
- lazarus汉化
启动Lazarus IDE,点击菜单栏中的Environment,再点击Options选项 在弹出的IDE选项框内,点选左侧Environment下的Desktop子选项,将Language设为Chi ...
- secureCRT在Windows上面的安装过程
参考这篇文章: https://www.cnblogs.com/yjd_hycf_space/p/7729796.html