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 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- dice_game攻防世界进阶区
dice_game XCTF 4th-QCTF-2018 前言,不得不说,虽然是个简单题但是还是要记录一下,来让自己记住这些东西. 考察的知识点是: 1.cdll_loadlibrary加载对应库使得 ...
- 数位dp & 热身训练7
数位dp 数位dp是一种计数用的dp,一般就是要统计一段区间$[L,R]$内,满足一定条件的数的个数,或者各个数位的个数. 数位dp使得暴力枚举变为满足一定状态的记忆化,更加优秀. 数位dp常常会考虑 ...
- Python gpu 显卡小工具 gpu
安装 pip install gpustat 或者 换源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade gpust ...
- InnoDB存储引擎的锁
InnoDB存储引擎的锁 锁的类型 锁的类型包括: 1. 共享锁(S lock),允许事务读取一行数据 2. 排他锁(X lock),允许事务删除或更新一行数据 锁的兼容性a X S ...
- Xtrabackup 全量备份脚本
#!/bin/bash #备份文件的名字为当前主机的IP地址+tar.gz,例如172.16.103.1.tar.gz,且每次备份成功之后都会清空本地的备份目录. #相关目录 mkdir -p /xt ...
- 关于docker中容器可以Ping通外网,真机无法Ping通容器的问题
首先我们要知道整体的框架结构,docker是我们安装在centos7上的,而centos7是安装在vmware上.其中docker中还有若干容器运行. 整体框架图如下: 我们将它分为两部分,一部分是d ...
- yrm的安装和使用
yrm的安装和使用 我们经常下载包的速度很忙有的还会卡住几十分钟,所以我们需要切换镜像,这样我们下载的速度会快很多 而yrm 是一个 yarn源管理器,允许你快速地在源间切换 安装 npm insta ...
- ES6基础知识(Generator 函数)
1.next().throw().return() 的共同点 next().throw().return()这三个方法本质上是同一件事,可以放在一起理解.它们的作用都是让 Generator 函数恢复 ...
- 菜鸡的Java笔记 第六 - java 方法
前提:现在所讲解的方法定义格式,只属于JAVA 方法定义的其中一种组成方式.而完整的组成方式将随着学习逐步渗透. 1.方法的基本定义 方法(Method)在一些书中也会有人将其说是 函数(Funct ...
- windows桌面图标不显示,左右键无法使用的解决方法
问题描述: 日常使用软件中,一返回桌面,桌面图标全部不显示,点击鼠标的左键,右键毫无反应 解决方法: 1. Ctrl+Shift+Esc呼出软仵管理器 2. 右键windows资管理器,点击属性 配图 ...