There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5

Solution1: time complexity is O(m+n). merge two sorted array, then find the median

code:

 /*
Time Complexity: O(m+n)
Space Complexity: O(m+n)
*/ class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) { // find median
int[] mergedArray = mergeTwoSortedArray(nums1, nums2);
int n = mergedArray.length;
if( n % 2 == 0){
return (mergedArray[(n-1)/2] + mergedArray[n/2])/2.0;
}else{
return mergedArray[n/2];
}
} public int[] mergeTwoSortedArray(int[] nums1, int[] nums2){ // merge sort
int[] merged = new int[nums1.length + nums2.length];
int i = 0, j = 0, k = 0;
// 两个array同时扫
while( i < nums1.length && j < nums2.length){
merged[k++] = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
}
//扫到只剩下较长的那个array, either nums1 or nums2
while ( i < nums1.length ){
merged[k++] = nums1[i++];
}
while(j < nums2.length){
merged[k++] = nums2[j++];
}
return merged;
}
}

Solution2: time complexity is O(log (m+n)).

这个题的思路可以是找出2个sorted array 所有元素中,Kth 元素

因此我们可以把问题转化成,

“Given 2 sorted arrays, A, B of sizes m, n respectively, find the numbers which are NOT medians of the two sorted arrays”

如果我们能找到那些NOT medians的数字,删掉,并不断缩小范围,那最终剩下的一定就是actual median

Step1, 确定要找的median是merged之后array中的第几个元素

Step2, 用binary search切开nums1, nums2。发现nums1左半边的长度为4, nums2左半边的长度为2。

Step3, nums1左半边长度+ nums2左半边长度为6

Step4, 所以第7个元素可能在nums1的右半边pivot上,或者在nums2的右半边pivot上

比较发现,第7个元素应该是7

code:

 /*
Time Complexity: O(log(m+n))
Space Complexity: O(log(m+n))
*/ class Solution {
public double findMedianSortedArrays(final int[] A, final int[] B) {
int total = A.length + B.length;
if (total %2 == 0){
return (findKth(A, 0, B, 0, total / 2) + findKth(A, 0, B, 0, total / 2 + 1)) / 2.0;
}else{
return findKth(A, 0, B, 0, total / 2 + 1);
}
} private static int findKth(final int[] A, int ai, final int[] B, int bi, int k) {
//always assume that A is shorter than B
if (A.length - ai > B.length - bi) {
return findKth(B, bi, A, ai, k);
}
if (A.length - ai == 0) return B[bi + k - 1];
if (k == 1) return Math.min(A[ai], B[bi]); //divide k into two parts
int k1 = Math.min(k / 2, A.length - ai), k2 = k - k1;
if (A[ai + k1 - 1] < B[bi + k2 - 1])
return findKth(A, ai + k1, B, bi, k - k1);
else if (A[ai + k1 - 1] > B[bi + k2 - 1])
return findKth(A, ai, B, bi + k2, k - k2);
else
return A[ai + k1 - 1];
}
}

[leetcode]4. Median of Two Sorted Arrays俩有序数组的中位数的更多相关文章

  1. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  2. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  3. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  5. [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  6. Leetcode 5——Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  7. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  8. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  9. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

随机推荐

  1. NIO、BIO、AIO区别

    一.同步阻塞I/O(BIO): 同步阻塞I/O,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,可以通过线程池机 ...

  2. [转]Java泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  3. C语言小程序:除去字符串中间不需要的字符(从小引发大思考)

    #include <stdio.h>#include <conio.h> void fun(char *a, char *h, char *p){ char b[81]; ch ...

  4. MySQL查询不使用索引汇总 + 如何优化sql语句

    不使用索引原文 : http://itlab.idcquan.com/linux/MYSQL/918330.html MySQL查询不使用索引汇总 众所周知,增加索引是提高查询速度的有效途径,但是很多 ...

  5. Nodepad++ 进行数据分析操作

    查找: ^.*大师兄.*$ 替换为:(空)   如果不留空行: 查找: ^.*大师兄.*\r?\n   注意: Notepad++的[全部替换]受[方向]约束,所以如果想“向下”全部替换,要把光标放到 ...

  6. 【剑指offer】合并有序链表

    输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. *思路:假设两个链表的当前结点为n1(list1),n2(list2)比较链表结点值的大小,如果n1.va ...

  7. CSS之padding&margin

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. node项目初始化的一些配置

    1. const port = process.env.PORT || 9001; 本地开发用9001端口 2. package.json中配置几个启动命令 "scripts": ...

  9. Android 开发 框架系列 Google的ORM框架 Room

    目录 简介 导入工程 使用流程概况 一个简单的小Demo 深入学习 @Entity使用 自定义表名 tableName  自定义字段名@ColumnInfo 主键 @PrimaryKey 索引 @In ...

  10. python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器

    一.元组: tuple Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组 tup2 = (111, 22, 33, ...