首页 > 其他分享 >LeetCode寻找两个正序数组的中位数(vector/二分查找 划分数组)

LeetCode寻找两个正序数组的中位数(vector/二分查找 划分数组)

时间:2023-01-14 22:33:20浏览次数:72  
标签:totalLength 正序 int vector 数组 nums1 nums2 size

原题解

题目

给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 O(log (m+n)) 。

约束

解法

解法一

class Solution {
public:
    int getKthElement(const vector<int>& nums1, const vector<int>& nums2, int k) {
        /* 主要思路:要找到第 k (k>1) 小的元素,那么就取 pivot1 = nums1[k/2-1] 和 pivot2 = nums2[k/2-1] 进行比较
         * 这里的 "/" 表示整除
         * nums1 中小于等于 pivot1 的元素有 nums1[0 .. k/2-2] 共计 k/2-1 个
         * nums2 中小于等于 pivot2 的元素有 nums2[0 .. k/2-2] 共计 k/2-1 个
         * 取 pivot = min(pivot1, pivot2),两个数组中小于等于 pivot 的元素共计不会超过 (k/2-1) + (k/2-1) <= k-2 个
         * 这样 pivot 本身最大也只能是第 k-1 小的元素
         * 如果 pivot = pivot1,那么 nums1[0 .. k/2-1] 都不可能是第 k 小的元素。把这些元素全部 "删除",剩下的作为新的 nums1 数组
         * 如果 pivot = pivot2,那么 nums2[0 .. k/2-1] 都不可能是第 k 小的元素。把这些元素全部 "删除",剩下的作为新的 nums2 数组
         * 由于我们 "删除" 了一些元素(这些元素都比第 k 小的元素要小),因此需要修改 k 的值,减去删除的数的个数
         */

        int m = nums1.size();
        int n = nums2.size();
        int index1 = 0, index2 = 0;

        while (true) {
            // 边界情况
            if (index1 == m) {
                return nums2[index2 + k - 1];
            }
            if (index2 == n) {
                return nums1[index1 + k - 1];
            }
            if (k == 1) {
                return min(nums1[index1], nums2[index2]);
            }

            // 正常情况
            int newIndex1 = min(index1 + k / 2 - 1, m - 1);
            int newIndex2 = min(index2 + k / 2 - 1, n - 1);
            int pivot1 = nums1[newIndex1];
            int pivot2 = nums2[newIndex2];
            if (pivot1 <= pivot2) {
                k -= newIndex1 - index1 + 1;
                index1 = newIndex1 + 1;
            }
            else {
                k -= newIndex2 - index2 + 1;
                index2 = newIndex2 + 1;
            }
        }
    }

    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int totalLength = nums1.size() + nums2.size();
        if (totalLength % 2 == 1) {
            return getKthElement(nums1, nums2, (totalLength + 1) / 2);
        }
        else {
            return (getKthElement(nums1, nums2, totalLength / 2) + getKthElement(nums1, nums2, totalLength / 2 + 1)) / 2.0;
        }
    }
};

解法二

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        if (nums1.size() > nums2.size()) {
            return findMedianSortedArrays(nums2, nums1);
        }
        
        int m = nums1.size();
        int n = nums2.size();
        int left = 0, right = m;
        // median1:前一部分的最大值
        // median2:后一部分的最小值
        int median1 = 0, median2 = 0;

        while (left <= right) {
            // 前一部分包含 nums1[0 .. i-1] 和 nums2[0 .. j-1]
            // 后一部分包含 nums1[i .. m-1] 和 nums2[j .. n-1]
            int i = (left + right) / 2;
            int j = (m + n + 1) / 2 - i;

            // nums_im1, nums_i, nums_jm1, nums_j 分别表示 nums1[i-1], nums1[i], nums2[j-1], nums2[j]
            int nums_im1 = (i == 0 ? INT_MIN : nums1[i - 1]);
            int nums_i = (i == m ? INT_MAX : nums1[i]);
            int nums_jm1 = (j == 0 ? INT_MIN : nums2[j - 1]);
            int nums_j = (j == n ? INT_MAX : nums2[j]);

            if (nums_im1 <= nums_j) {
                median1 = max(nums_im1, nums_jm1);
                median2 = min(nums_i, nums_j);
                left = i + 1;
            } else {
                right = i - 1;
            }
        }

        return (m + n) % 2 == 0 ? (median1 + median2) / 2.0 : median1;
    }
};

标签:totalLength,正序,int,vector,数组,nums1,nums2,size
From: https://www.cnblogs.com/chuixulvcao/p/17052602.html

相关文章

  • 栈和堆的区别以及栈数组和堆数组的区别
    ​ 这里写得很简洁,实际上堆的机制比较复杂,我详细地学习了Windows下的堆管理机制,如果对这部分感兴趣的话,可以参考我的另一篇文章:https://www.cnblogs.com/XiuzhuKirakira/......
  • 遍历打印数组在一行 实例
    一行打印数组packagecom.fqs.demo;publicclassChongZ{//数组遍历遍历显示整个数组显示在一行//publicstaticvoidmain(String[]args){......
  • C++中如何将一行字符串(一行字符串可带空格)输入到string对象中或者字符数组中?
    提供两种方法:①、使用cin的成员函数getline,代码如下:charstr1[20];cin.getline(str1,20);     //第一个参数代表字符数组的指针,第二个参数代表写入的最大长度②、......
  • 数组是高效使用内存的基础
    数组是指多个同样数据类型的数据在内存中连续排列的形式。作为数组元素的各个数据会通过连续的编号被区分开来,这个编号称为索引(index)。指定索引后,就可以对该索引所对应地......
  • Vector 容器类
    Vector底层是用数组实现的,相关的方法都加了同步检查,因此“线程安全,效率低”。比如,indexOf方法就增加了synchronized同步标记。Vector的使用Vector的使用与ArrayL......
  • SA后缀数组
    SA后缀数组sa数组:sa[i]=x表示对于所有的后缀进行排序(字典序)后,得到排名为i的以第x个字符开头的后缀rk数组:rk[x]=i,是对于所有的后缀进行排序(字典序)后,得到以第x个字符......
  • 利用lodash对(对象)数组去重
    使用场景:根据数(对象)组中的id或者其他属性去重,或者对象中的所有属性值相同的去重。传统方法:通过数组的some进行逐项判断;用了lodash之后发现还是很香的。import{isEqual......
  • c++ 数组
              ......
  • 数组元素移动到指定位置
    letdata=[{name:1},{name:2},{name:3}]//arr:原数组,a:某个对象当前位置,b:某个对象想要移动到的位置functionMove(arr,a,b){letarr_temp=[].concat(arr);......
  • php 将二维数组处理成以某一列为key,某一列为value的一维数组
    $list=[0=>['id'=>1001,'name'=>'张三'],1=>['id'=>2091,'name'=>'李四']];array_combine(arr......