[LeetCode]题解(python):004-Median of Two Sorted Arrays
题目来源:
https://leetcode.com/problems/median-of-two-sorted-arrays/
题意分析:
这道题目是输入两个已经排好序的数组(长度为m,n),将这两个数组整合成一个数组,输出新数组的中位数。要求时间复杂度是(log(m + n)。比如如果输入[1,2,3],[3,4,5]。那么得到的新数组为[1,2,3,3,4,5]得到的中位数就是3.
题目思路:
由于题目要求的时间复杂度是(log(m+n)),如果我们直接把两个数组整合一起,那么时间复杂度肯定超过(log(m+n))。所以整理肯定是不行的。那么还有什么方法吗?答案是肯定的。
首先我们要先了解中位数的概念,中位数就是有序数组的中间那个数。那么如果我们将比中位数小的数和比中位数大的数去掉同样的个数,中位数的值也不会变化(数组的个数为偶数的时候另外讨论,因为那时候中位数是中间两个数的平均值,所以中位数旁边两个数不能去掉)。
所以我们不妨试着将数组长度不断缩短。这里不妨提出一个引理。假设有两个有序数组am,bn,他们整合后的有序数组为cn+m。他们的中位数分别是am/2,bn/2,c(m+n)/2。如果am/2 < bn/2,则 a0…m/2 <= c(m+n)/2 <= bn/2…n 。
引理证明:
假设 am/2 > c(m+n)/2 ,那么 bn/2 > c(m+n)/2,所以在数组am里有大于m/2个数大于c(m+n)/2,在数组bn里也有n/2个数大于c(m+n)/2
也就是说在cn+m里有(m+n)/2个数大于c(m+n)/2,此时就c(m+n)/2不再是数组cn+m的中位数。
所以a0…m/2 <= c(m+n)/2。
同理可得c(m+n)/2 <= bn/2…n 。
根据上述引理,我们不妨设m>n,那么我们根据判断两个数组的中位数大小,每个数组每次减少n/2长度,直到n为1。如此,我们通过减少log(n)次可以得到答案。这种方法的时间复杂度是(log(min(m,n)))。
代码(python):
class Solution(object):
def getMedian(self,nums):
size = len(nums)
if size == 0:
return [0,0]
if size % 2 == 1:
return [nums[size // 2],size // 2]
return [(float(nums[size // 2 - 1] + nums[size // 2])) / 2,size // 2] def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
size1 = len(nums1)
size2 = len(nums2)
if size1 < size2:
return self.findMedianSortedArrays(nums2,nums1)
m1 = self.getMedian(nums1)
m2 = self.getMedian(nums2)
if size2 == 0:
return m1[0]
if size2 == 1:
if size1 == 1:
return (float(nums1[0] + nums2[0])) / 2
if size1 % 2 == 0:
if nums2[0] < nums1[size1 //2 - 1]:
return nums1[size1 // 2 - 1]
if nums2[0] > nums1[size1 // 2 ]:
return nums1[size1 // 2]
else:
return nums2[0]
else:
if nums2[0] < nums1[size1 // 2 - 1]:
return (float(nums1[size1 // 2 - 1] + nums1[size1 // 2])) / 2
if nums2[0] > nums1[size1 // 2 + 1]:
return (float(nums1[size1 // 2] + nums1[size1 // 2 + 1])) / 2
else:
return (float(nums2[0] + nums1[size1 // 2])) / 2
if size2 % 2 == 0:
if size1 % 2 == 0:
if nums2[size2 // 2 - 1] < nums1[size1 //2 - 1] and nums2[size2 // 2] > nums1[size1 // 2]:
return m1[0]
if nums1[size1 // 2 - 1] < nums2[size2 // 2 - 1] and nums1[size1 // 2] > nums2[size2 // 2]:
return m2[0]
if m1[0] < m2[0]:
return self.findMedianSortedArrays(nums1[m2[1]:],nums2[:size2 - m2[1]])
if m1[0] > m2[0]:
return self.findMedianSortedArrays(nums1[:size1 - m2[1]],nums2[m2[1]:])
else:
return m1[0]
PS:当两个数组长度都是偶数的时候,由于中位数和中间两个数相关,如果直接删减有可能把中位数的数值发生改变,比如:[1,6],[4,5],这种情况如果用上述算法,那么先得到[1],[4]最后得到2.5,然后最后答案应该是4.5,所以这种情况要另外讨论。
还有,用python3.0要注意语法的不同,因为判断系统是用2.7的算法。3.0的’/’默认是浮点数,而在‘2.7’如果原来是整型的时候是整除。
转载请注明出处:http://www.cnblogs.com/chruny
[LeetCode]题解(python):004-Median of Two Sorted Arrays的更多相关文章
- 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 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- LeetCode--No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【LeetCode】004. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【JAVA、C++】LeetCode 004 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 ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 004 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 ...
- 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 ...
随机推荐
- NOIP2012模拟试题【奶牛晒衣服】
1.奶牛晒衣服(dry) [问题描述] 在熊大妈英明的带领下,时针和它的同伴生下了许多牛宝宝.熊大妈决定给每个宝宝都穿上可爱的婴儿装.于是,为牛宝宝洗晒衣服就成了很不爽的事情. 圣人王担负起了这个重任 ...
- C++输入和输出
本文转载:blog.csdn.net/zhanghaotian2011/article/details/8868577博客 输入和输出并不是C++语言中的正式组成成分。C和C++本身都没有为输入和输出 ...
- Lake Counting--poj2386
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23950 Accepted: 12099 D ...
- 03-树1. List Leaves (25)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- DTS结构及其编译方法
一.主要问题1,需要了解dtsi与dts的关系2,dts的结构模型3,dts是如何被编译的,以及编译后会生成一个什么文件. 二.参考文字1,DTS(device tree source).dts文件是 ...
- 山寨QQ音乐的布局(一)
学了两天IOS趁着还没忘光,巩固一下所学知识想做点东西,由于自己的设计能力有限,所以就山寨一下吧,说到山寨怎么能忘了腾讯呢,今天发现QQ音乐的设计风格是扁平化的,小清新风格,所以就山寨一下它吧.. 由 ...
- shell基础——字符串连接
#!/bin/sh str1="hello" str2="world" echo str1=$str1, str2=$str2 strconn1=$str1$s ...
- Introduction to Big Data with Apache Spark 课程总结
课程主要实用内容: 1.spark实验环境的搭建 2.4个lab的内容 3.常用函数 4.变量共享 1.spark实验环境的搭建(windows) a. 下载,安装visualbox 管理员身 ...
- linux shell自定义函数(定义、返回值、变量作用域)介绍
http://www.jb51.net/article/33899.htm linux shell自定义函数(定义.返回值.变量作用域)介绍 linux shell 可以用户定义函数,然后在shell ...
- c/c++性能优化--- cache优化的一点杂谈
之前写了一篇关于c/c++优化的一点建议,被各种拍砖和吐槽,有赞成的有反对的,还有中立的,网友对那篇博客的的评论和吐槽,我一个都没有删掉,包括一些具有攻击性的言论.笔者有幸阅读过IBM某个项目的框架代 ...