leetCode-4-Median of Two Sorted Arrays-Hard

descrition

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)).

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

解析

注意题目对时间复杂度的要求:O(log (m+n)),在加上数组有序的条件,此处应该联想到二分搜索的优化。那要如何用呢?看下面分析。(也可以参考 leetcode 中的 solution,解释得也很好)

定义函数接口:

double findMedianSortedArrays(vector<int>& A, vector<int>& B)

即两个数组 A, B,假设大小分别为 m 和 n

将数组 A 根据任意位置 i 分成左右两部分,同理 B 根据任意位置 j 也分成左右两部分,如下:

left_part                | right_part
A[0], A[1], ..., A[i-1] | A[i], A[i+1], ... , A[m]
B[0], B[1], ..., B[j-1] | B[j], B[j+1], ... , B[n]

由中位数的定义以及数组有序的条件,我们希望以上划分满足:

len(left_part) = len(right_part)
==> i+j = m-i + n-j (or: i+j = m-i + n-j + 1 (奇数的情况,将多的一个放到左边))
==> j = (m+n+1)/2 - i, i=[0,m]
max(left_part) <= min(right_part) ==> A[i-1] <= B[j] && B[j-1] <= A[i]

由此我们只需要在有序数组 A 上进行二分查找,确定 i 的位置,使得以上条件满足即可找到合适的划分。在此基础上我们可以根据划分边界求得 median。

初始条件:imin = 0, imax = m;
当 imin <= imax 时候的一次迭代:
i = (imin + imax) / 2
j = (m+n+1)/2 - i
// 此处讨论暂时不考虑边界情况,即假设 i 和 j 都合法
// 只可能出现以下几种情况:
(1) A[i-1] <= B[j] && B[j-1] <= A[i]
// 划分满足要求,可以停止循环
(2) A[i-1] > B[j]
// A[i-1] 太大,也就是我们要想办法调整 i 的值使得 A[i-1] <= B[j] 成立
// 这个时候如果我们增大 i,只会使得 A[i-1] 更大 (因为数组是非递减有序的)
// 因此我们只能通过减小 i,尝试找到更小的 A[i-1],因此作出如下调整
imax = i-1
(3) B[j-1] > A[i]
// B[j-1] 太大,同理我们应该减小 j
// 从另一个角度,减小 j 就相当于增大 i,因此我们作出以下调整
imin = i+1

几个细节:

  • m <= n 必须成立。因为 j = (m+n+1)/2 - i,如果 m>n,那么 j 将有可能为负数。因此在程序的开始进行检查。
  • 边界条件的讨论。当 i0, j0, im, jn 时,A[i-1], B[j-1], A[i], B[j] 都是不成立的。如果 i 和 j 都满足要求时,我们需要检查 A[i-1] <= B[j] && B[j-1] <= A[i] 是否成立,那么当 i 和 j 到达边界条件时,我们也就不需要检查其中的某一个条件,比如当 i == 0 时, A 数组的左边为空,我们就不需要检查 A[i-1] <= B[j] 这个条件。(参看代码)

code


#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
double findMedianSortedArrays(vector<int>& A, vector<int>& B){
// insure the size of A is less than and equal to the size of B
if(A.size() > B.size())
swap(A, B);
int m = A.size();
int n = B.size();
// m <= n // when m+n is odd, the follow equation will insure the median
// will be assined into left part.
int lenLeft = (m+n+1) / 2; // binary search
int imin = 0, imax = m;
while(imin <= imax){
int i = (imin + imax) / 2;
int j = lenLeft - i;
if( i>0 && A[i-1] > B[j]){
// A[i-1] too large, decreasing i
// i > 0 ==> j<n, because j = (m+n+1) / 2 - i < (m+n+1) / 2 < 2n+1/2 < n
imax = i-1;
}else if ( i<m && B[j-1] > A[i]){
// A[i] too small, increasing i
// i < m ==> j>0, because j = (m+n+1) / 2 - i > (m+n+1) / 2 - m > (2m+1)/2 - m > 0
imin = i+1;
}else{
// perfit
int maxLeft = 0;
if(i == 0){
maxLeft = B[j-1];
}else if (j == 0){
maxLeft = A[i-1];
}else{
maxLeft = max(B[j-1], A[i-1]);
} if( ((n+m)&1) == 1) // odd
return maxLeft; int minRight = 0;
if(i == m){
minRight = B[j];
}else if (j == n){
minRight = A[i];
}else{
minRight = min(B[j], A[i]);
} return (maxLeft + minRight)*1.0 / 2.0;
}
} return 0.0;
}
}; int main()
{
return 0;
}

[array] leetCode-4-Median of Two Sorted Arrays-Hard的更多相关文章

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

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

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

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

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

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

  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 & 归并排序

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

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

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

  7. 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 ...

  8. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  9. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  10. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

随机推荐

  1. bzoj1087 [SCOI2005][状压DP] 互不侵犯King (状压)

    在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包含两个数N,K ( 1 ...

  2. 支持向量机SVM(一)

    [转载请注明出处]http://www.cnblogs.com/jerrylead 1 简介 支持向量机基本上是最好的有监督学习算法了.最开始接触SVM是去年暑假的时候,老师要求交<统计学习理论 ...

  3. [js高手之路]html5 canvas动画教程 - 边界判断与小球粒子模拟喷泉,散弹效果

    备注:本文后面的代码,如果加载了ball.js,那么请使用这篇文章[js高手之路] html5 canvas动画教程 - 匀速运动的ball.js代码. 本文,我们要做点有意思的效果,首先,来一个简单 ...

  4. curl命令用于模拟http浏览器发起动作

    1.模拟http浏览器发起访问百度首页的动作 curl  http://www.baidu.com 2.也可以模拟http浏览器发起POST动作,这个在测试后端程序时非常常见.

  5. vs重装找不到 $(WindowsSdkDir) 配置问题

    vs重装的一个bug,找了一个下午,删了再装vs也没用. 在配置表   HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs ...

  6. 【转载】CSS font关键字属性值的简单研究

    文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=5 ...

  7. 【机器学习实战】第7章 集成方法 ensemble method

    第7章 集成方法 ensemble method 集成方法: ensemble method(元算法: meta algorithm) 概述 概念:是对其他算法进行组合的一种形式. 通俗来说: 当做重 ...

  8. 张高兴的 Xamarin.Android 学习笔记:(一)环境配置

    最近在自学 Xamarin 和 Android ,同时发现国内在做 Xamarin 的不多.我在自学中间遇到了很多问题,而且百度到的很多教程也有些过时,现在打算写点东西稍微总结下,顺便帮后人指指路了. ...

  9. win10 uwp 活动磁贴

    本文翻译:https://mobileprogrammerblog.wordpress.com/2015/12/23/live-tiles-and-notifications-in-universal ...

  10. UVa10791 - Minimum Sum LCM

    分析即为紫薯上的分析. 难点是发现当每个aipi作为一个单独的整数时才最优.. 答案就是将所有不同的 相同因子的积 相加即可 代码: #include<cstdio> #include&l ...