码迷,mamicode.com
首页 > 其他好文 > 详细

Q4:Median of Two Sorted Arrays

时间:2018-03-27 01:50:00      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:desc   官方   链接   lex   ring   body   简单   target   col   

4. Median of Two Sorted Arrays

官方的链接:4. Median of Two Sorted Arrays

Description :

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

Example1:


nums1 = [1, 3]

nums2 = [2]

The median is 2.0


Example2:


nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5


问题描述

给定长度分别为m和n的两个排序数组nums1和nums2,在时间复杂度O(log (m+n))内算出数组的中位数

思路

两个有序数组的中位数和Top K问题类似。这里从小到大直接定位第k个,简单理解为从nums1中获取第i个,而从nums2中获取第j=k-i个,其中i=0~k。当定位到nums[i-1]<=nums2[j]和nums[j-1]<=nums[i]即完成,当然还有边界问题。

把中位数也当作是top k问题,最后进行奇偶判断。详情可参考这里Share my O(log(min(m,n)) solution with explanation,代码也是借鉴的。

值得注意的是几个边界判断问题,比如i为0或者m,j为0或者n。

[github-here]

 

 1 public class Q4_MedianOfTwoSortedArrays {
 2     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
 3 
 4         int m = nums1.length;
 5         int n = nums2.length;
 6         // make sure that m <= n
 7         if (m > n) {
 8             return findMedianSortedArrays(nums2, nums1);
 9         }
10         // n>=m,i = 0 ~ m,so j = (m + n + 1) / 2 -i > 0.
11         int i = 0, j = 0, imax = m, imin = 0, halfLen = (m + n + 1) / 2;
12         int maxLeft = 0, minRight = 0;
13         while (imin <= imax) {
14             i = (imin + imax) / 2;
15             j = halfLen - i;
16             if (i < m && nums2[j - 1] > nums1[i]) {
17                 imin = i + 1;
18             } else if (i > 0 && nums1[i - 1] > nums2[j]) {
19                 imax = i - 1;
20             } else {
21                 if (i == 0) {
22                     //the target is in nums2
23                     maxLeft = nums2[j - 1];
24                 } else if (j == 0) {
25                     //the target is in nums1
26                     maxLeft =  nums1[i - 1];
27                 } else {
28                     maxLeft = Math.max(nums1[i - 1], nums2[j - 1]);
29                 }
30                 break;
31             }
32         }
33         //odd
34         if ((m + n) % 2 == 1) {
35             return (double)maxLeft;
36         }
37         //even
38         if (i == m) {
39             //nums1 is out of index m
40             minRight = nums2[j];
41         } else if (j == n) {
42             //nums2 is out of index n
43             minRight = nums1[i];
44         } else {
45             //others
46             minRight = Math.min(nums1[i], nums2[j]);
47         }
48         return (double) (maxLeft + minRight) / 2;
49     }
50 
51     public static void main(String[] args) {
52         new Q4_MedianOfTwoSortedArrays().findMedianSortedArrays(new int[] { 1, 3 }, new int[] { 2 });
53 
54     }
55 }

 

Q4:Median of Two Sorted Arrays

标签:desc   官方   链接   lex   ring   body   简单   target   col   

原文地址:https://www.cnblogs.com/wpbxin/p/8654941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!