[leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/
题意:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
解题思路:这道题要求两个已经排好序的数列的中位数。中位数的定义:如果数列有偶数个数,那么中位数为中间两个数的平均值;如果数列有奇数个数,那么中位数为中间的那个数。比如{1,2,3,4,5}的中位数为3。{1,2,3,4,5,6}的中位数为(3+4)/ 2 = 3.5。那么这题最直接的思路就是将两个数列合并在一起,然后排序,然后找到中位数就行了。可是这样最快也要O((m+n)log(m+n))的时间复杂度,而题目要求O(log(m+n))的时间复杂度。这道题其实考察的是二分查找,是《算法导论》的一道课后习题,难度还是比较大的。
首先我们来看如何找到两个数列的第k小个数,即程序中getKth(A, B , k)函数的实现。用一个例子来说明这个问题:A = {1,3,5,7};B = {2,4,6,8,9,10};如果要求第7个小的数,A数列的元素个数为4,B数列的元素个数为6;k/2 = 7/2 = 3,而A中的第3个数A[2]=5;B中的第3个数B[2]=6;而A[2]<B[2];则A[0],A[1],A[2]中必然不可能有第7个小的数。因为A[2]<B[2],所以比A[2]小的数最多可能为A[0], A[1], B[0], B[1]这四个数,也就是说A[2]最多可能是第5个大的数,由于我们要求的是getKth(A, B, 7);现在就变成了求getKth(A', B, 4);即A' = {7};B不变,求这两个数列的第4个小的数,因为A[0],A[1],A[2]中没有解,所以我们直接删掉它们就可以了。这个可以使用递归来实现。
代码:
class Solution:
# @return a float
# @line20 must multiply 0.5 for return a float else it will return an int
def getKth(self, A, B, k):
lenA = len(A); lenB = len(B)
if lenA > lenB: return self.getKth(B, A, k)
if lenA == 0: return B[k - 1]
if k == 1: return min(A[0], B[0])
pa = min(k/2, lenA); pb = k - pa
if A[pa - 1] <= B[pb - 1]:
return self.getKth(A[pa:], B, pb)
else:
return self.getKth(A, B[pb:], pa) def findMedianSortedArrays(self, A, B):
lenA = len(A); lenB = len(B)
if (lenA + lenB) % 2 == 1:
return self.getKth(A, B, (lenA + lenB)/2 + 1)
else:
return (self.getKth(A, B, (lenA + lenB)/2) + self.getKth(A, B, (lenA + lenB)/2 + 1)) * 0.5
[leetcode]Median of Two Sorted Arrays @ Python的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- [LeetCode] 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 ...
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Leetcode Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- Leetcode: Median of Two Sorted Arrays. java.
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- LeetCode——Median of Two Sorted Arrays
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现
题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...
随机推荐
- 2,EasyNetQ-日志记录
EasyNetQ提供了一个IEasyNetQLogger接口: public interface IEasyNetQLogger { void DebugWrite(string format, pa ...
- Web Service(二):cxf 实现
1. cxf简介 Web Services 的一种实现方式. Apache CXF = Celtix + XFire,后更名为 Apache CXF ,简称为 CXF. CXF 继承了 Celtix ...
- BZOJ.3257.树的难题(树形DP)
题目链接 状态只与黑.白两点的颜色有关,于是用 \(f[x][i][j]\)表示当前以x为根节点,有\(i\)个黑点\(j\)个白点,使得x子树满足该条件的最小花费. 最后答案就是 \(min\{f[ ...
- 【GDKOI 2016】地图 map 类插头DP
Description 对于一个n*m的地图,每个格子有五种可能:平地,障碍物,出口,入口和神器.一个有效的地图必须满足下列条件: 1.入口,出口和神器都有且仅出现一次,并且不在同一个格子内. 2.入 ...
- 吴恩达-coursera-机器学习-week3
六.逻辑回归(Logistic Regression) 6.1 分类问题 6.2 假说表示 6.3 判定边界 6.4 代价函数 6.5 简化的成本函数和梯度下降 6.6 高级优化 6.7 多类别分类: ...
- SlickOne 敏捷开发框架介绍(二) -- 多用户/多租户/SAAS软件基础框架实现
前言:在应用于集团版客户或SAAS平台服务的业务系统中,流程管理系统需要支持多用户组织模型.其中包括角色数据.流程定义数据和流程实例数据的多用户标识绑定.本文旨在全面描述如何基于SlickOne敏捷开 ...
- MikroTik RouterOS电子克隆盘原理收集
终于搞定RouteROS8位电子盘克隆,发个讯息出来分享一下. 不需要付费的免费分享,也没要刻意挡人财路:只是让信息流通一下. 也请看到的人不要用这个方式去赚钱,不然MikroTik还是会再反制的. ...
- CMSIS-DAP for STLink V2.1 and STLink v2.1 mini adapter
CMSIS-DAP V1 V2
- 使用cat读取和echo写内核文件节点的一些问题
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...
- 在ASP.NET MVC中使用Knockout实践05,基本验证
本篇体验View Model验证.Knockout的subscribe方法能为View Model成员注册验证规则. @{ ViewBag.Title = "Index"; Lay ...