LeetCode - 4. Median of Two Sorted Arrays

简介: 4. Median of Two Sorted Arrays  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定两个数组,求这两个数组的中位数.

4. Median of Two Sorted Arrays 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给定两个数组,求这两个数组的中位数.(要求时间复杂度为O(log(n+m))

analyse:

一开始用归并为一个数组的方法做了一下也AC了,看来lc的时间还是给的很宽的.

将本题转化为求第k大数就简单多了,其中求第k大数使用类似二分的方法来实现,从而将时间复杂度降到O(log(n+m)).

Time complexity: O(log(n+m)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-03-12.07
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

/*Solution 1*/
class Solution
{
    //求A和B数组的第k大数
    int getMedian( int A [], int m , int B [], int n , int k)
    {
        if( m >n)
            return getMedian(B ,n , A , m , k); //默认A为短数组
        if( m == 0)
            return B [ k - 1 ];
        if( k == 1)
            return min( A [ 0 ], B [ 0 ]);
        int pa = min( k / 2 , m);
        int pb = k - pa;
        if( A [ pa - 1 ] < B [pb - 1 ])
        {
            return getMedian( A + pa , m - pa , B , n , k - pa);
        }
        else if( A [ pa - 1 ] > B [pb - 1 ])
        {
            return getMedian( A , m , B +pb , n -pb , k -pb);
        }
        else
        {
            return A [ pa - 1 ];
        }
        return 0;
    }
public :
    double work( int A [], int m , int B [], int n)
    {
        if(( m +n) % 2 == 0)
        {
            return ( getMedian( A , m ,B , n , ( m +n) / 2) + getMedian( A , m ,B , n , ( m +n) / 2 + 1)) / 2.;
        }
        else
        {
            return getMedian( A , m ,B , n , ( m +n) / 2 + 1);
        }
    }
    double findMedianSortedArrays( vector < int >& nums1 , vector < int >& nums2)
    {
        int A [ 10000 ],B [ 10000 ];
        int idx = 0;
        for( auto p: nums1)
        {
            A [ idx ++ ] =p;
        }
        idx = 0;
        for( auto p: nums2)
        {
           B [ idx ++ ] =p;
        }
        int m = nums1 . size();
        int n = nums2 . size();
        double ret = work( A , m ,B ,n);
        return ret;
    }
};

/*Solution 2*/
/*
class Solution
{
public:
   double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
   {
       auto it1=nums1.begin();
       auto it2=nums2.begin();
       vector<int> a;
       while(it1!=nums1.end() || it2!=nums2.end())
       {
           if(it1==nums1.end() && it2!=nums2.end())
           {
               a.push_back((*it2));
               it2++;
           }
           else if(it1!=nums1.end() && it2==nums2.end())
           {
               a.push_back(*it1);
               it1++;
           }
           else if(it1!=nums1.end() && it2!=nums2.end())
           {
               if((*it1)<(*it2))
               {
                   a.push_back(*it1);
                   it1++;
               }
               else
               {
                   a.push_back(*it2);
                   it2++;
               }
           }
       }
       int len=a.size();
       double ans=(len%2)?(double)a[len/2]:(double)(a[len/2-1]+a[len/2])/2.;
       return ans;
   }
};
*/

int main()
{
      int n , m , temp;
      while( cin >>n >> m)
      {
          vector < int > a ,b;
          for( int i = 0; i <n; ++ i)
          {
              cin >> temp;
              a . push_back( temp);
          }
          for( int i = 0; i < m; ++ i)
          {
              cin >> temp;
             b . push_back( temp);
          }
          Solution solution;
          double ans = solution . findMedianSortedArrays( a ,b);
          cout << "===========ans===========" << endl;
          cout << ans << endl;
      }
      return 0;
}

 

目录
相关文章
|
5月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
16 0
|
5月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
16 0
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
58 0
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
|
存储 算法
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. Intersection of Two Arrays II
给定两个数组,编写一个函数来计算它们的交集。
46 0
LeetCode 350. Intersection of Two Arrays II
|
Python
LeetCode 349. Intersection of Two Arrays
给定两个数组,编写一个函数来计算它们的交集。
51 0
LeetCode 349. Intersection of Two Arrays
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
63 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 153. Find Minimum in Rotated Sorted Array
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
78 0
LeetCode 153. Find Minimum in Rotated Sorted Array