LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1
思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2)
1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得
当要取的是最小值或最大值时,也直接可得
2.明显两个数组总长度为偶数的时候需要取最中间两个元素/2.0,长度为奇数时,只需要求最中间那个.所以只需要分别求出最多两个元素,分别是
(findKthElement(0,t1,0,t2,(t1 + t2)/2) 和 findKthElement(0,t1,0,t2,(t1 + t2)/2 + 1)
(这一步没有想到可以抛弃中位数,直接转化为求第k大的数,导致第一版代码非常难看.)
在这里我们把这个问题拓展为在两个数组中求第k大的数
3.
下面来看一个例子

在以上的nums1,nums2中求第7大数,要知道,去除了最小的六个数,剩下的可用区间中最小的数一定是第7大数,
可是我们怎么知道哪些数才能够被去除呢?
我们当然可以胡乱取个值,比如nums1[0],也就是8,我们发现它恰好比nums2[0]要小,这样一看,nums1[0]是最小的数字,可以去除,但这样效率太慢了.
效率比较高的是哪个数字呢?比如7/2 = 3,
这个时候nums1[2]>nums2[2],也就是说nums2[0-2]一定小于nums1[2],此时可能比它们中某些值大的数字只有3-1 = 2个,也即nums2[0-2]一定在前6个里,我们就可以舍弃nums2[0-2],现在问题转化为在nums1[3,5),nums2[0,4)中取第4小的那个数,怎么样,问题的数据规模是不是变得小了不少......
也就是说,当在以上可用区间取第k大数的时候,设nums1[tmp1],nums2[tmp2]分别是从各自起点出发的第k/2个数(或者是小于第k/2且可取的最后一个数),那么假设nums1[tmp1]<nums2[tmp2],则明显[s1,tmp1]这个区间的都在第k个数之前,所以可以直接把它们去掉,反之可以舍弃[s2,tmp2](别忘了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);
}
};
LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- 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 ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- [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 ...
随机推荐
- 第一代intel核显id:0046的10.9驱动安装详解(转)
一代0046 intel核显hd1000m 10.8的驱动已经失效了,开不了QE/CI的 从tonymac找来的驱动,并完善一下 直接上驱动啦 安装步骤务必按照顺序进行,不然是驱动不起来的 第一步:首 ...
- Element is not currently interactable and may not be manipulated
Element is not currently interactable and may not be manipulated:元素当前不可交互,并且可能无法操作. 解决方法: 调用该方法,智能等待 ...
- MyBatis调用Oracle存储过程
MyBatis调用Oracle存储过程 1.无输入和输出参数的存储过程 2.带有输入和输出参数的存储过程 3.返回游标的存储过程 mybatis中的配置文件代码 <resultMap type= ...
- Centos7安装图形界面
安装好字符操作系统后,使用网络安装(网络安装比较简单,不需要配置yum文件): yum groupinstall "GNOME Desktop" -y startx centos7 ...
- 利用Javascript判断操作系统的类型实现不同操作系统下的兼容性
原文地址 http://www.jb51.net/article/33640.htm 在通过Javascript实现客户端和服务端的交互时,有时候需要对操作系统进行判断,以便实现不同操作系统下的兼容性 ...
- 系统hosts文件的作用
host是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统会 ...
- [SAP ABAP开发技术总结]ABAP调优——Open SQL优化
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Python进阶(三)--global和类属性
global关键字 一句话概括为:告诉python解释器,global声明的变量为全局作用域内定义的变量.解释器就会到全局作用域内寻找global定义的变量. python的类属性 类属性相当于其他O ...
- Exception in thread "main" java.lang.ExceptionInInitializerError
Exception in thread "main" java.lang.ExceptionInInitializerErrorCaused by: java.util.Missi ...
- validate插件深入学习-03validate()方法配置项
validate()方法配置项 rules 定义校验规则 messages 定义提示信息 上面这两个已经介绍过了 rules里隐藏的属性depends,只有符合条件(返回true)的时候才会执行 su ...