4.Median of Two Sorted Arrays Leetcode Python
来源:互联网 发布:淘宝助理导入csv卡住了 编辑:程序博客网 时间:2024/06/08 09:16
这题的一种O(m+n)的解法是mergesort的一步,依次比较两者的大小,将得到的解放到新的item里面。
最后再取出median
class Solution: # @return a float def findMedianSortedArrays(self, A, B): item=[] while len(A)>0 and len(B)>0: item.append(A.pop(0) if A[0]<=B[0] else B[0]) while len(A)>0: item.append(A.pop(0)) while len(B)>0: item.append(B.pop(0)) if len(item)%2==1: return item[len(item)/2] else: return (item[len(item)/2]+item[len(item)/2-1])/2另外一种解法是来自
http://blog.csdn.net/zxzxy1988/article/details/8587244
主题思想是每次比较A和B的各一半
如果A[half]<B[half] 中值会在 A剩下的一半和B中产生
反之 在A和B剩下的一半产生。
如果相等就刚刚好返回A[half]
Assume that the number of elements in A and B are both larger than k/2, and if we compare the k/2-th smallest element in A(i.e. A[k/2-1]) and the k-th smallest element in B(i.e. B[k/2 - 1]), there are three results:
(Becasue k can be odd or even number, so we assume k is even number here for simplicy. The following is also true when k is an odd number.)
A[k/2-1] = B[k/2-1]
A[k/2-1] > B[k/2-1]
A[k/2-1] < B[k/2-1]
if A[k/2-1] < B[k/2-1], that means all the elements from A[0] to A[k/2-1](i.e. the k/2 smallest elements in A) are in the range of k smallest elements in the union of A and B. Or, in the other word, A[k/2 - 1] can never be larger than the k-th smalleset element in the union of A and B.
Why?
We can use a proof by contradiction. Since A[k/2 - 1] is larger than the k-th smallest element in the union of A and B, then we assume it is the (k+1)-th smallest one. Since it is smaller than B[k/2 - 1], then B[k/2 - 1] should be at least the (k+2)-th smallest one. So there are at most (k/2-1) elements smaller than A[k/2-1] in A, and at most (k/2 - 1) elements smaller than A[k/2-1] in B.So the total number is k/2+k/2-2, which, no matter when k is odd or even, is surly smaller than k(since A[k/2-1] is the (k+1)-th smallest element). So A[k/2-1] can never larger than the k-th smallest element in the union of A and B if A[k/2-1]<B[k/2-1];
Since there is such an important conclusion, we can safely drop the first k/2 element in A, which are definitaly smaller than k-th element in the union of A and B. This is also true for the A[k/2-1] > B[k/2-1] condition, which we should drop the elements in B.
When A[k/2-1] = B[k/2-1], then we have found the k-th smallest element, that is the equal element, we can call it m. There are each (k/2-1) numbers smaller than m in A and B, so m must be the k-th smallest number. So we can call a function recursively, when A[k/2-1] < B[k/2-1], we drop the elements in A, else we drop the elements in B.
We should also consider the edge case, that is, when should we stop?
1. When A or B is empty, we return B[k-1]( or A[k-1]), respectively;
2. When k is 1(when A and B are both not empty), we return the smaller one of A[0] and B[0]
3. When A[k/2-1] = B[k/2-1], we should return one of them
In the code, we check if m is larger than n to garentee that the we always know the smaller array, for coding simplicy.
class Solution: # @return a float 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] def solve(self,A,m,B,n): m=len(A) n=len(B) total=m+n if total%2==1: return self.findk(A,m,B,n,total/2+1) else: return (self.findk(A,m,B,n,total/2+1)+self.findk(A,m,B,n,total/2))/2.0 def findMedianSortedArrays(self, A, B): m=len(A) n=len(B) result=self.solve(A,m,B,n) return result
- Median of Two Sorted Arrays -- leetcode -- Python
- 【Leetcode】【python】Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays Leetcode Python
- [Leetcode]4. Median of Two Sorted Arrays @python
- LeetCode 4.Median of Two Sorted Arrays (Python)
- 【leetcode】4. Median of Two Sorted Arrays(Python & C++)
- [LeetCode]4.Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- LeetCode --- 4. Median of Two Sorted Arrays
- [Leetcode] 4. Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays
- Leetcode-4.Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- leetcode 4. Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- 如果去掉UITableView上的section的headerView和footerView的悬浮效果
- 【转】YUV格式详解
- 修改android中的seekbar控件
- HTTP Error 500.24 / HTTP Error 404.2
- 按钮下自带划线 && 计算Label.text长度
- 4.Median of Two Sorted Arrays Leetcode Python
- JavaScript权威指南_48_第7章_数组_7.0-概述
- cocos studio 各模块使用方法
- Client Feature 简介-1
- 关于新版本struts2.3.16出现乱码问题
- C语言gettimeofday()函数:获取当前时间
- Hadoop学习总结之七:HDFS 操作
- 数据倾斜
- 多线程并发访问可能出现的崩溃问题