Leet Code 4 Median of Two Sorted Arrays

2024-06-15 08:58
文章标签 leet code median two sorted arrays

本文主要是介绍Leet Code 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 sorted arrays. The overall run time complexity should be O(log (m+n)).


【算法思路】

       搜了一下leetcode的难度分布表(leetcode难度及面试频率)才发现,该问题是难度为5的问题,真是小看了它!网上搜了很多答案,但是鲜见简明正确的解答,唯有一种寻找第k小值的方法非常好,在此整理一下。

       首先对leetcode的编译运行吐槽一下:貌似没有超时判断,而且small和large的数据集相差很小。

1. 利用排序将两个数组合并成一个数组,然后返回中位数:

[cpp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. class Solution {  
  2. public:  
  3.     double findMedianSortedArrays(int A[], int m, int B[], int n) {  
  4.         // Start typing your C/C++ solution below  
  5.         // DO NOT write int main() function  
  6.         int *a=new int[m+n];  
  7.           
  8.         memcpy(a,A,sizeof(int)*m);  
  9.         memcpy(a+m,B,sizeof(int)*n);  
  10.           
  11.         sort(a,a+n+m);  
  12.           
  13.         double median=(double) ((n+m)%2? a[(n+m)>>1]:(a[(n+m-1)>>1]+a[(n+m)>>1])/2.0);  
  14.           
  15.         delete a;  
  16.           
  17.         return median;  
  18.     }  
  19. };  

该方法居然也通过测试,但是其复杂度最坏情况为O(nlogn),这说明leetcode只对算法的正确性有要求,时间要求其实不严格。


2. 利用类似merge的操作找到中位数,利用两个分别指向A和B数组头的指针去遍历数组,然后统计元素个数,直到找到中位数,此时算法复杂度为O(n)。


3. 从medianof two sorted arrays中看到了一种非常好的方法。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。

首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。

这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。

证明也很简单,可以采用反证法。假设A[k/2-1]大于合并之后的第k小值,我们不妨假定其为第(k+1)小值。

由于A[k/2-1]小于B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但实际上,在A中至多存在k/2-1个元素小于A[k/2-1],B中也至多存在k/2-1个元素小于A[k/2-1],所以小于A[k/2-1]的元素个数至多有k/2+ k/2-2,小于k,这与A[k/2-1]是第(k+1)的数矛盾。

当A[k/2-1]>B[k/2-1]时存在类似的结论。

当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素,我们将其记为m。由于在A和B中分别有k/2-1个元素小于m,所以m即是第k小的数。(这里可能有人会有疑问,如果k为奇数,则m不是中位数。这里是进行了理想化考虑,在实际代码中略有不同,是先求k/2,然后利用k-k/2获得另一个数。)


通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:

  • 如果A或者B为空,则直接返回B[k-1]或者A[k-1];
  • 如果k为1,我们只需要返回A[0]和B[0]中的较小值;
  • 如果A[k/2-1]=B[k/2-1],返回其中一个;

【CODE】

[cpp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. double findKth(int a[], int m, int b[], int n, int k)  
  2. {  
  3.     //always assume that m is equal or smaller than n  
  4.     if (m > n)  
  5.         return findKth(b, n, a, m, k);  
  6.     if (m == 0)  
  7.         return b[k - 1];  
  8.     if (k == 1)  
  9.         return min(a[0], b[0]);  
  10.     //divide k into two parts  
  11.     int pa = min(k / 2, m), pb = k - pa;  
  12.     if (a[pa - 1] < b[pb - 1])  
  13.         return findKth(a + pa, m - pa, b, n, k - pa);  
  14.     else if (a[pa - 1] > b[pb - 1])  
  15.         return findKth(a, m, b + pb, n - pb, k - pb);  
  16.     else  
  17.         return a[pa - 1];  
  18. }  
  19.   
  20. class Solution  
  21. {  
  22. public:  
  23.     double findMedianSortedArrays(int A[], int m, int B[], int n)  
  24.     {  
  25.         int total = m + n;  
  26.         if (total & 0x1)  
  27.             return findKth(A, m, B, n, total / 2 + 1);  
  28.         else  
  29.             return (findKth(A, m, B, n, total / 2)  
  30.                     + findKth(A, m, B, n, total / 2 + 1)) / 2;  
  31.     }  
  32. };  


代码非常简洁,而且效率也很高。在最好情况下,每次都有k一半的元素被删除,所以算法复杂度为logk,由于求中位数时k为(m+n)/2,所以算法时间复杂度为log(m+n)。


【java 版】

暴力排序的复杂度O(n*log n)

 public static double findMedianLow(int A[], int B[]) {int[] sumArray = ArrayUtils.addAll(A, B);Arrays.sort(sumArray);int length = sumArray.length;if (length % 2 == 0) {double num1 = sumArray[length / 2];double num2 = sumArray[length / 2 - 1];return (num1 + num2) / 2;} else {return sumArray[length / 2];}}



public class Solution {public double findMedianSortedArrays(int A[], int B[]) {int m = A.length;int n = B.length;int total = m + n;//长度为积数取中间,为偶数去中间两个的平均值if ((total & 0x01) != 0) {return findKth(A, m, B, n, total / 2 + 1);} else {return (findKth(A, m, B, n, total / 2) + findKth(A, m, B, n,total / 2 + 1)) / 2.0;}}//二分法,每次都能去除掉一部分范围外数据。需要注意每次去除数据都会改变数组的结构,所以需要特殊处理临界值private static double findKth(int a[], int m, int b[], int n, int k) {if (m > n)  return findKth(b, n, a, m, k);  if (m == 0)  return b[k - 1];  if (k == 1)  return Math.min(a[0], b[0]);  //divide k into two parts  int pa = Math.min(k / 2, m), pb = k - pa;  if (a[pa - 1] < b[pb - 1])  return findKth(Arrays.copyOfRange(a, pa, m), m - pa, b, n, k - pa);  else if (a[pa - 1] > b[pb - 1])  return findKth(a, m, Arrays.copyOfRange(b, pb, n), n - pb, k - pb);  else  return a[pa - 1];  }
}


这篇关于Leet Code 4 Median of Two Sorted Arrays的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062997

相关文章

VS Code SSH 远程连接服务器及坑点解决

背景 Linux服务器重装了一下,IP没有变化,结果VS Code再重连的时候就各种问题,导致把整个流程全部走了一遍,留个经验帖以备查看 SSH 首先确保Windows安装了ssh,通过cmd下ssh命令查看是否安装了。 没安装,跳转安装Windows下的ssh 对应的,也需要Linux安装ssh,本文是Ubuntu系统,使用以下命令安装: sudo apt updatesudo

Google Code Jam 2014(附官方题解)

2014年Google编程挑战赛 Problem A. Magic Trick Confused? Read the quick-start guide. Small input 6 points You have solved this input set. Note: To advance to the next rounds, you will need to s

在Mac OS上使用Visual Studio Code创建C++ Qt的Hello World应用

引言 Qt是一个跨平台的应用程序和用户界面框架,而Visual Studio Code是一个功能强大的编辑器,两者结合可以极大地提升开发效率。本文将指导你在Mac OS上使用Visual Studio Code创建一个简单的Qt 'Hello World'窗口应用。 环境准备 确保你的MacBook OS运行最新的操作系统。安装Homebrew,Mac OS的包管理器。通过Homebrew安装

【C/C++】Code Style

消除重复 PS:机能一次,使用多次 // .hvirtual bool hasFormat(const QString &mimetype) const;//.cppbool QMimeData::hasFormat(const QString &mimeType) const{return formats().contains(mimeType);}bool QMimeData::h

解析Java中1000个常用类:Arrays类,你学会了吗?

推荐一个我自己写的程序员在线工具站: http://cxytools.com 提供一站式在线工具平台,专为程序员设计,包括时间日期、JSON处理、SQL格式化、随机字符串生成、UUID生成、随机数生成、文本Hash等功能,提升开发效率。 以下是正文。 在 Java 编程中,数组是基础且常用的数据结构之一。然而,原生数组的操作往往比较繁琐且易错。为了更方便地操作数组,Java 提供了一个工

Query failed with error code 96 and error message 'Executor error during find command: OperationFail

Query failed with error code 96 and error message 'Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smal

gbase8s之Encoding or code set not supported

如图发生以下错误: 解决办法:在url里加上ifx_use_strenc=true 就可以了 参数解释:

[Codeforces - Gym100801H (NEERC)] Hash Code Hacker (字符串构造)

Codeforces - Gym100801H (NEERC) 给定一个字符串hash,为 ∑i=0len−1str[i]×31len−1−i \displaystyle\sum_{i=0}^{len-1} str[i]\times 31^{len-1-i} 求 K K个长度不超过 1000的字符串,使得他们的 hash值相等 其中每个 hash值是 32位有符号整数,K≤1000K\l

File I/O source code--新建文件 相关方法阅读

虽然我们经常在用java的I/O,但是我们有没有想过,我们是怎样来创建文件的呢 首先我们来新建一个文件: try {File file = new File("c:\\newfile.txt");if (file.createNewFile()){System.out.println("File is created!");}else{System.out.println("File al

ArrayList source code相关方法阅读

1、新增一个对象 /*** Appends the specified element to the end of this list.** @param e element to be appended to this list* @return <tt>true</tt> (as specified by {@link Collection#add})*/public boolean a