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 ...
随机推荐
- jQuery实践——属性和css篇
属性: attr html:<div>demo1</div> jQuery:$("div").attr("id","demo1 ...
- ACM HDU 2041--超级楼梯题解
超级楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- boldSystemFontOfSize 和 systemFontOfSize 的区别
使用 UIFont 的下列方法: + systemFontOfSize + boldSystemFontOfSize + italicSystemFontOfSize p.p1 { margin: 0 ...
- 利用cytoscape做网络图
首先做出下面的基因间相互关系图 1.准备sif文件 data.sif 网络数据文件 gene1 pp gene2 gene3 gene4 gene5 gene6 gene7 gene8 gene9 n ...
- 读《编写可维护的JavaScript》第11章总结
这周也是拿到了同程的offer,从此走上了前端之路!感谢我的贵人们.再次纪念一下~! 第11章 不是你的对象不要动 11.1 什么是你的 你的对象:当你的代码创建了这些对象或者你有职责维护其他人的代码 ...
- 读《编写可维护的JavaScript》第五章总结
第五章 UI层的松耦合 5.1 什么是松耦合 在Web开发中,用户界面是由三个彼此隔离又相互作用的层定义的: HTML是用来定义页面的数据和语义 CSS用来给页面添加样式 JavaScript用来给页 ...
- 数据源增量处理(Delta Proess)三大属性:Recod Mode、Delta Type、Serialization
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- hdu 4585 Shaolin treap
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- 设置float之后vertical-align失效
相关博文: 关于Vertical-Align你需要知道的事情 https://segmentfault.com/a/1190000002668492
- 转:logBack.xml配置路径
http://blog.csdn.net/z69183787/article/details/30284391 http://www.cppblog.com/fwxjj/archive/2012/08 ...