LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法 难度:1

思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2)

1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得

当要取的是最小值或最大值时,也直接可得

2.明显两个数组总长度为偶数的时候需要取最中间两个元素/2.0,长度为奇数时,只需要求最中间那个.所以只需要分别求出最多两个元素,这一步没有想到可以抛弃中位数,直接转化为求第k大的数,导致第一版代码非常难看.

3.当需要取第k个数的时候,设nums1[tmp1],nums2[tmp2]分别是从各自起点出发的第k/2个数,那么假设nums1[tmp1]<nums2[tmp2],则明显[s1,tmp1]这个区间的都在第k个数之前,(因为[s1,tmp1],[s2,tmp2]都是各自区间最小的那一部分数,因为max(nums1[s1,tmp1])<min(nums1[tmp1+1,t1),nums2[tmp2,t2))),所以可以直接把它们去掉,别忘了k里面也要划去这个值

class Solution
{
public:
    const int inf = ~0u >> 1;
    vector<int> nums1,nums2;
    double findKthElement(int s1,int t1,int s2,int t2,int k)
    {
       // assert(t1 - s1 + t2 - s2 >= k);
        if(s1 == t1)return nums2[s2 + k - 1];
        else if(s2 == t2)return nums1[s1 + k - 1];
        if(k == 1)return min(nums1[s1],nums2[s2]);
        if(k == t1 - s1 + t2 - s2)return max(nums1[t1 - 1],nums2[t2 - 1]);
        int tmp1 = min(s1 + k/2 - 1,t1 - 1);
        int tmp2 = min(s2 + k/2 - 1,t2 - 1);
        if(nums1[tmp1] < nums2[tmp2])
        {
            return findKthElement(tmp1 + 1,t1,s2,t2,k - tmp1 + s1 - 1);
        }
        else if(nums1[tmp1] > nums2[tmp2])
        {
            return findKthElement(s1,t1,tmp2 + 1,t2,k - tmp2 + s2 - 1);
        }
        else
        {
            if((k & 1) == 0)return nums2[tmp2];
            else return min((tmp1 + 1< t1?nums1[tmp1 + 1]:inf),
                                (tmp2 + 1< t2?nums2[tmp2 + 1]:inf));
        }
    }
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
    {
        this->nums1 = nums1;
        this->nums2 = nums2;
        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());
        int t1 = nums1.size(),t2 = nums2.size();
        return ((t1 + t2) & 1) == 0?
               (findKthElement(0,t1,0,t2,(t1 + t2)/2) + findKthElement(0,t1,0,t2,(t1 + t2)/2 + 1))/2.0
               :findKthElement(0,t1,0,t2,(t1 + t2)/2 + 1);
    }
};

  

时间: 2024-10-18 00:52:13

LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法 难度:1的相关文章

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B 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)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

LeetCode OJ - Median of Two Sorted Arrays

题目: There are two sorted arrays A and B 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)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

[LeetCode][JavaScript]Median of Two Sorted Arrays

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)). https://leetcode.com/problems/median-of-two-sorted

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

4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别.但是代码实现较为困难. 换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分.划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R) 二分搜索这个划分即可.对于A+B的长度为奇数的情

leetcode 2.Median of Two Sorted Arrays

Median of Two Sorted Arrays Problem: There are two sorted arrays A and B 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】Median of Two Sorted Arrays (2 solutions)

Median of Two Sorted Arrays There are two sorted arrays A and B 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)). 解法一:保底做法,O(m+n)复杂度 按照归并排序的思路,数到median,然后计算返回. 需要注意: 如果是m+n

leetcode之Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B 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)). 首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的

[LeetCode][Python]Median of Two Sorted Arrays

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays A and B 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)).