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:

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

Example 2:

  1. nums1 = [1, 2]
  2. nums2 = [3, 4]
  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:

  1. /*
  2. Time Complexity: O(m+n)
  3. Space Complexity: O(m+n)
  4. */
  5.  
  6. class Solution {
  7. public double findMedianSortedArrays(int[] nums1, int[] nums2) { // find median
  8. int[] mergedArray = mergeTwoSortedArray(nums1, nums2);
  9. int n = mergedArray.length;
  10. if( n % 2 == 0){
  11. return (mergedArray[(n-1)/2] + mergedArray[n/2])/2.0;
  12. }else{
  13. return mergedArray[n/2];
  14. }
  15. }
  16.  
  17. public int[] mergeTwoSortedArray(int[] nums1, int[] nums2){ // merge sort
  18. int[] merged = new int[nums1.length + nums2.length];
  19. int i = 0, j = 0, k = 0;
  20. // 两个array同时扫
  21. while( i < nums1.length && j < nums2.length){
  22. merged[k++] = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
  23. }
  24. //扫到只剩下较长的那个array, either nums1 or nums2
  25. while ( i < nums1.length ){
  26. merged[k++] = nums1[i++];
  27. }
  28. while(j < nums2.length){
  29. merged[k++] = nums2[j++];
  30. }
  31. return merged;
  32. }
  33. }

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:

  1. /*
  2. Time Complexity: O(log(m+n))
  3. Space Complexity: O(log(m+n))
  4. */
  5.  
  6. class Solution {
  7. public double findMedianSortedArrays(final int[] A, final int[] B) {
  8. int total = A.length + B.length;
  9. if (total %2 == 0){
  10. return (findKth(A, 0, B, 0, total / 2) + findKth(A, 0, B, 0, total / 2 + 1)) / 2.0;
  11. }else{
  12. return findKth(A, 0, B, 0, total / 2 + 1);
  13. }
  14. }
  15.  
  16. private static int findKth(final int[] A, int ai, final int[] B, int bi, int k) {
  17. //always assume that A is shorter than B
  18. if (A.length - ai > B.length - bi) {
  19. return findKth(B, bi, A, ai, k);
  20. }
  21. if (A.length - ai == 0) return B[bi + k - 1];
  22. if (k == 1) return Math.min(A[ai], B[bi]);
  23.  
  24. //divide k into two parts
  25. int k1 = Math.min(k / 2, A.length - ai), k2 = k - k1;
  26. if (A[ai + k1 - 1] < B[bi + k2 - 1])
  27. return findKth(A, ai + k1, B, bi, k - k1);
  28. else if (A[ai + k1 - 1] > B[bi + k2 - 1])
  29. return findKth(A, ai, B, bi + k2, k - k2);
  30. else
  31. return A[ai + k1 - 1];
  32. }
  33. }

[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. linux下insmod模块出现“Invalid parameters"

    在编译一个模块时,会出现WARNING:"函数名" undefined!,这 说明该模块所依赖的模块还没有加载进内核,需要先加载所依赖的模块. 当加载依赖模块后,使用insmod会 ...

  2. 集合总结一(ArrayList的实现原理)

    一.概述 一上来,先来看看源码中的这一段注释,我们可以从中提取到一些关键信息: Resizable-array implementation of the List interface. Implem ...

  3. laravel whereNotIn where子查詢

    子查詢寫法 $stores = Stores::select('id','name')->whereNotIn('id', function ($query) use($id){ $query- ...

  4. vscode编辑Markdown时的贴图工具

    参看 https://www.jianshu.com/p/74b960efb697 说明: 1. 文件-->首选项-->设置-->填入paste,设置Path值为 ${current ...

  5. MongoDB复制集技术

    复制集搭建 没毛病: https://www.cnblogs.com/nicolegxt/p/6841442.html?utm_source=itdadao&utm_medium=referr ...

  6. 层次softmax函数(hierarchical softmax)

    一.h-softmax 在面对label众多的分类问题时,fastText设计了一种hierarchical softmax函数.使其具有以下优势: (1)适合大型数据+高效的训练速度:能够训练模型“ ...

  7. 七、XHTML介绍

    XHTML简介 1.什么是XHTML? XHTML指的是可扩展超文本标记语言 XHTML与HTML4.01几乎是相同的 XHTML是更严格更纯净的HTML版本 XHTML得到所有主流浏览器的支持 2. ...

  8. 使用fdisk进行分区

    fdisk进行分区 1.首先使用fdisk -l 发现待分区磁盘/dev/vdb  大小为1TB 2.fdisk /dev/vdb 对该磁盘进行分区,输入m并回车 3.输入n并回车,n是“new”新建 ...

  9. cookie与session的区别是什么

    cookie与session的区别有:cookie以文本格式存储在浏览器上,存储量有限:而会话存储在服务端,可以无限量存储多个变量并且比cookie更安全 在php中可以指定站点的访问者信息存储在se ...

  10. C#设计模式(3)——工厂方法模式(Factory Method)

    在简单工厂模式中通过工厂Factory获取不同的对象,但是有一个明显的缺点——简单工厂模式系统难以扩展! 一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过于复杂, 可以通过工厂 ...