【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 arrays. The overall run time complexity should be O(log (m+n)).
解题思路:
这本身是个很简单的题目,但是题目要求他的复杂度为O(log(m+n)),就很有难度了。不过首先我们还是可以明确我们要用分治法。关键是怎么分治呢?我们有如下的思路(核心思想是求第k小):
class Solution:
def findk(self, A, m, B, n, k):
if m > n:
return self.findk(B, n, A, m, k)
if m == 0:
return B[k-1]
if k == 1:
return min(A[0],B[0])
pa = min(k / 2, m)
pb = k - pa
if A[pa - 1] < B[pb - 1]:
return self.findk(A[pa:], m - pa, B, n, k - pa)
elif A[pa - 1] > B[pb - 1]:
return self.findk(A, m, B[pb:], n - pb, k - pb)
else:
return A[pa - 1]
# @return a float
def findMedianSortedArrays(self, A, B):
la = len(A)
lb = len(B)
l = la + lb
if l % 2 == 0:
return (self.findk(A, la, B, lb, l/2+1) + self.findk(A, la, B, lb, l/2))/2.0
else:
return self.findk(A, la, B, lb, l/2+1)
很容易证明这个想法的复杂度正是O(log(m+n))
【leetcode】Median of Two Sorted Arrays的更多相关文章
- 【leetcode】Median of Two Sorted Arrays(hard)★!!
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【算法之美】求解两个有序数组的中位数 — 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 & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- leetCode之Median of Two Sorted Arrays
[题目描述] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...
- 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 (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
随机推荐
- 使用Python解析JSON数据
使用Python解析百度API返回的JSON格式的数据 # coding:utf-8 # !/usr/bin/env python import matplotlib.pyplot as plt fr ...
- websocket
websocket是一个协议,在单个TCP连接上提供全双工通信. websocket被设计并被实现在 web浏览器和 web 服务器上,但是它可以被用于任何c/s 架构的应用程序中. websock ...
- Adapt适配器
为接口提供所有的空实现,让使用者仅仅覆盖需要的部分: 本思想肯定有问题,慢慢考究 frame.addWindowListener(new WindowAdapter() { @Override pub ...
- YII2-数据库数据查询方法,关联查询with, joinWith区别和分页
一.ActiveRecord 活动记录 1.with关联查询 例如,查询评论 $post = Post::find()->with('comments'); 等价于以下结果集 SELECT * ...
- react+react-router+webpack+express+nodejs
react+react-router+webpack+express+nodejs 做SinglePageApplication 支持热加载+ES6 有开发模式和发布模式 https://gith ...
- sublime text 3 3083 注册码
亲试,可用! —– BEGIN LICENSE —– Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 B ...
- 第4天--linux内核学习
驱动使用方式1.编译到内核中 * make uImage进入到系统后mknod /dev/led c 500 0 创建设备节点 2.编译为模块 M make module进入到系统后 mknod /d ...
- MySQLdb的一些经验
遇到过的几类问题: 如果保持长连接,即使在mysql数据库默认的connection timeout内,也有可能出现"mysql server has gone away".还有另 ...
- webservice客户端开发
一.根据约定条件生成客户端 1.需要axis.jar包 2.调用 import org.apache.axis.client.Call; import org.apache.axis.client.S ...
- Workflow 中做拒绝操作时强制输入拒绝信息
在做AP发票审批驳回时,客户要求必须强制输入拒绝原因,代码如下: PROCEDURE Validate_Response ( Itemtype IN VARCHAR2, Itemkey IN VARC ...