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 ...
随机推荐
- Java异常---获取异常的堆栈信息
Java 实例 - 获取异常的堆栈信息 Java 实例 以下实例演示了使用异常类的 printStack() 方法来获取堆栈信息: Main.java 文件 public class Main{ p ...
- rest api上传和下载文件
rest api上传和下载文件 function FileToString(AFileName: string): string; var LMemoryStream: TMemoryStream; ...
- apache路由端口配置
<VirtualHost *:80> ServerName a.com ProxyPreserveHost On ProxyRequests On ProxyPass / http://1 ...
- 获得assets文件夹中文件内容
/** * @param fileName * @return assets中文件的字符串 */ public String getFromAssets(Context context, String ...
- [Web 前端] 使用yarn代替npm作为node.js的模块管理器
cp from : https://www.jianshu.com/p/bfe96f89da0e Fast, reliable, and secure dependency managemen ...
- [Android Pro] 开发一流的 Android SDK:Fabric SDK 的创建经验
cp from : https://academy.realm.io/cn/posts/oredev-ty-smith-building-android-sdks-fabric/ Ty Smith T ...
- Filebeat+Logstash+ElasticSearch+Kibana搭建Apache访问日志解析平台
对于ELK还不太熟悉的同学可以参考我前面的两篇文章ElasticSearch + Logstash + Kibana 搭建笔记.Log stash学习笔记(一),本文搭建了一套专门访问Apache的访 ...
- 深入探索 JUnit 4
开始之前 关于本教程 引入 Java 5 注释为 JUnit 带来了显著改变,使它从一个受广大开发人员了解和喜爱的测试框架转变成了一个更为精简但却不那么为人熟知的框架.在本教程中,我将探讨 JUnit ...
- Table does not have the identity property. Cannot perform SET operation.
Table does not have the identity property. Cannot perform SET operation. 解决: set IDENTITY_INSERT ...
- GraphX中Pregel单源点最短路径(转)
原文链接:GraphX中Pregel单源点最短路径 GraphX中的单源点最短路径例子,使用的是类Pregel的方式. 核心部分是三个函数: 1.节点处理消息的函数 vprog: (VertexId ...