3.Median of Two Sorted Arrays Leetcode
难度系数:5星
/***************************************************************************
题目:有两个排好序的数组,要求其中位数,时间复杂度控制在O(log(m+n))
一般化:求两个排序数组的第K大的元素
直观思路1:PA和PB首先指向这两个子数组,对比比较,A小则PA++,m++,B小则
PB++,m++,直至m==k ======>> 时间复杂度O(m+n)
正解思路:
由于数组是有序的,又是有序的,能否通过二分等思路来可能筛掉一些元素
关键是设计一种方案让其收敛到最终位置
尝试二分: 若Amid<Bmid,cutA前一半,B后一半,
能确定的是A前一半小于A后一半以及B后一半。
A------------cut---------------
B------cut--------
那么A的前一半不可能是中位数,筛掉,要是Bmid小呢,只能筛掉B的前一半
这样不稳定
如果以K的一半为标准呢:
A[k/2-1]==B[k/2-1],说明AB的前k/2-1+k/2-1=k-2个元素在topK内
那么 A[k/2-1]便是第K-1和第K大的数(K为偶数)
A[K/2-1]>B[k/2-1] 去除掉A数组前K/2元素(实际最大能去除整个数组元素)
A[K/2-1]<B[k/2-1]
step1:当A或B是空时,直接返回A[K-1]或者B[k-1]
step2:当K=1,返回min[A[0],B[0]]
step3:当A[K/2-1]==B[k/2-1] 返回A[K/2-1]
************************************************************/
class Solution{
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){
int m = nums1.size(),n = nums2.size();
int total = m+n;
cout<<total<<endl;
if(total&0x1)
return find_k(&nums1[0],m,&nums2[0],n,total/2+1);
else
{
return (find_k(&nums1[0],m,&nums2[0],n,total/2)
+find_k(&nums1[0],m,&nums2[0],n,total/2+1))/2.0;
}
}
private:
static int find_k(int A[],int m,int B[],int n,int k)
{
if(m>n)return find_k(B,n,A,m,k);
if(m==0) return B[k-1];
if(k==1)return min(A[0],B[0]);
int ia = min(m,k/2),ib = k-ia;
if(A[ia-1]<B[ib-1]) return find_k(A+ia,m-ia,B,n,ib);
else if(A[ia-1]>B[ib-1])
return find_k(A,m,B+ib,n-ib,k-ib);
else return A[ia-1];
}
};
int main()
{
int a[]={1,3,5,7,9};
int b[]={2,4,6};
Solution S;
vector<int> B(b,b+3),A(a,a+5);
cout<<S.findMedianSortedArrays(A,B)<<endl;
return 0;
}
3.Median of Two Sorted Arrays Leetcode的更多相关文章
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 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(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- BUAA 2020 软件工程 结对项目作业
Author: 17373051 郭骏 3.28添加:4.计算模块接口的设计与实现过程部分,PairCore实现的细节 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) ...
- CSP踩被记
本来想起个清新脱俗的标题,但碍于语文功底不行,于是光明正大嫖了LiBoyi的高端创意,把这篇博客命名为踩被记. Day -6 用假暴力把真正解拍没了,伤心.Rp有点低 Day -4 信息学考,\(py ...
- 多边形——————区间dp
原题链接:https://www.acwing.com/problem/content/285/ 题意简单来说就是:给你一个环,断掉一条边使其成为一个链,用这个链跑dp,求最大得分. 首先这不是一道板 ...
- Windows平台编译器相关的几个预定义宏
WIN32 是在windows.h 中定义的宏,包含winodws.h则定义该宏 _WIN32/_WIN64跟windows平台有关的宏,_WIN32在windows 32位和64位下都有该宏,_ ...
- TCP/IP参考模型(应用层、传输层、网际层、网络接口层)、五层参考模型(应用层、传输层、网络层、数据链路层、物理层)、OSI与TCP/IP参考模型比较
文章转自:https://blog.csdn.net/weixin_43914604/article/details/104597450 学习课程:<2019王道考研计算机网络> 学习目的 ...
- Spring IOC(控制反转)和DI(依赖注入)原理
一.Spring IoC容器和bean简介 Spring Framework实现了控制反转(IoC)原理,IoC也称为依赖注入(DI). 这是一个过程,通过这个过程,对象定义它们的依赖关系,即它们使用 ...
- hdu 5055 Bob and math problem (很简单贪心)
给N个数字(0-9),让你组成一个数. 要求:1.这个数是奇数 2.这个数没有前导0 问这个数最大是多少. 思路&解法: N个数字从大到小排序,将最小的奇数与最后一位交换,把剩下前N-1位从大 ...
- spring-cloud-square源码速读(spring-cloud-square-okhttp篇)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- hivesql笔记
一.常用聚合函数 count():计数 count(distinct 字段) 去重统计 sum():求合 avg():平均 max():最大值 min():最小值 二.hivesql执行顺序 from ...
- Part 37 Difference between $scope and $rootScope
In this video we will discuss the difference between $scope and $rootScope. The main difference is t ...