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 ...
随机推荐
- http错误码
2xx 成功 200 正常:请求已完成. 201 正常:紧接 POST 命令. 202 正常:已接受用于处理,但处理尚未完成. 203 正常:部分信息 - 返回的信息只是一部分. ...
- GOLANG 基本数据类型 整型
基本数据类型-整型 种类 有符号(负号) int8 int16 int32 int64 无符号(无符号) uint8 uint16 uint32 uint64 架构特定(取决于系统位 ...
- TCP/IP基础概念及通信过程举例
TCP/IP基础概念及通信过程举例 出现 上个世纪60年代,由于中央集中式网络的容灾性较弱,以美国国防部为中心的一家组织研究出分组交换网络.后来为了验证分组交换技术的实用性,ARPANET出现了,并且 ...
- DTP激活时报Overlapping
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Qt qmake 使用(含遗留问题)
网上在介绍编译Qt的教程中,大多数都是这样提到编译的: 1, 运行 VS2012 x86 Native Tools Command Prompt 批处理 2, cd <install qt5.0 ...
- Thread
问题:编写一个能提现多线程的例子?假设有t1,t2两个线程,如何保证t2线程在t1线程执行完后再执行? package cn.changb.thread; public class MyThread ...
- PBOC金融IC卡,卡片与终端交互的13个步骤,简介-第一组(转)
两个PPT结合起来--一些基础介绍--每一步的详细细节还要去研读文档 EMV-全球标准PBOC-国内标准 ----------------------一:必选:应用选择应用选择的方法:目录选择法.AI ...
- L2TP协议
L2TP协议 L2TP(Layer 2 Tunneling Protocol) 第二层隧道协议.该协议是工业标准的Internet隧道协议. L2TP实现的两种方式 LAC (L2TP Access ...
- stm32定时器中断类型分析
一直在用的stm32定时器的中断都是TIM_IT_Update更新中断,也没问为什么,直到碰到有人使用TIM_IT_CC1中断,才想到这定时器的中断类型究竟有什么区别,都怪当时学习stm32的时候不够 ...
- HTTP 源码解读
作用域:调用函数,访问变量的能力有关 作用域分为:局部和全局(在局部作用域里可以访问到全局作用域的变量,但在局部作用域外面就访问不到局部作用里面所设定的变量) 上下文:与this关键字有关 是调用当前 ...