leetcode004 Median_of_Two_Sorted_Arrays.py

news/2024/7/8 1:05:34
"""给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可以假设 nums1 和 nums2 不会同时为空。示例 1:nums1 = [1, 3]
nums2 = [2]则中位数是 2.0
示例 2:nums1 = [1, 2]
nums2 = [3, 4]则中位数是 (2 + 3)/2 = 2.5
"""
from typing import List
class Solution:def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:"""Merge two arrays to get the median, O((m+n)/2)Algorithm: Find k-th element in 2 arrayA: A_left A[m/2] A_rightB: B_left B[n/2] A_rightif A[m/2]>B[n/2] and k>m/2+n/2, then disregard B_left and B[n/2]if A[m/2]>B[n/2] and k<=m/2+n/2, then disregard A_right and A[m/2]if A[m/2]<=B[n/2] and k>m/2+n/2, then disregard A_left and A[m/2]if A[m/2]<=B[n/2] and k<=m/2+n/2, then disregard B_right and B[n/2]whether to disregard A[m/2] or B[n/2] takes time to considerT(N) = T(3/4 N) + O(1), thus T(N) = O(lg N) where N = |A|+|B|O(log (m+n)), thus binary search.:param A: list:param B: list:return: float"""m = len(nums1)n = len(nums2)if (m+n) % 2 == 0:return (self.find_kth(nums1,nums2,(m+n)//2) + self.find_kth(nums1,nums2,(m+n)//2-1)) / 2.0else:return self.find_kth(nums1,nums2,(m+n) // 2)def find_kth(self,arr1,arr2,num):if not arr1:return arr2[num]if not arr2:return arr1[num]if num == 0:return min(arr1[0],arr2[0])m,n = len(arr1),len(arr2)if arr1[m//2] >= arr2[n//2]:if num > m//2 + n // 2:return self.find_kth(arr1,arr2[n//2+1:],num-n//2 -1)else:return self.find_kth(arr1[:m//2],arr2,num)else:return self.find_kth(arr2,arr1,num)

http://lihuaxi.xjx100.cn/news/268467.html

相关文章

Unity的三种Interceptor

Unity默认提供了三种拦截器&#xff1a;TransparentProxyInterceptor、InterfaceInterceptor、VirtualMethodInterceptor。 TransparentProxyInterceptor&#xff1a;代理实现基于.NET Remoting技术&#xff0c;它可拦截对象的所有函数。缺点是被拦截类型必须派生于MarshalByRe…

搭建samba服务器

先安装samba服务器&#xff0c; 常规安装 sudo apt-get install samba 这种方式在不同版本ubuntu中&#xff0c;由于库版本支持问题&#xff0c;可能是无法安装的&#xff0c;所以需要降库版本来安装&#xff0c;安装命令为&#xff1a;sudo aptitude install samba ,这样执行后…

C# 多态的实现

C# 多态的实现 封装、继承、多态&#xff0c;面向对象的三大特性&#xff0c;前两项理解相对容易&#xff0c;但要理解多态&#xff0c;特别是深入的了解&#xff0c;对于初学者而言可能就会有一定困难了。我一直认为学习OO的最好方法就是结合实践&#xff0c;封装、继承在实际…

蜡笔小新鸿蒙系统,盘点蜡笔小新最污的四处剧情,网友:当年太纯洁现在终于看懂!...

《蜡笔小新》是童年时的一部经典动漫作品&#xff0c;里面的主角小新给我们带来了许多的乐趣。不过在《蜡笔小新》的剧情中却有很多比较污的剧情让当年的我们没有看懂&#xff0c;而现在再来回顾一下发现当年的这些剧情怎么可以这么污&#xff01;&#xff01;第一处&#xff1…

字符集图标制作

字符集图标&#xff1a; 将网页上常见的icon做成font&#xff08;字符集&#xff09;&#xff0c;以字体的方式插入到网页上&#xff0c;作用是减轻服务器负担&#xff0c;减少宽带。 我最常在这两个网站上下载字体图标&#xff1a; https://icomoon.io/app/#/select https://w…

为什么说机器学习是预防欺诈的最佳工具?

作者 | Giorgi Mikhelidze译者 | 天道酬勤&#xff0c;责编 | 晋兆雨头图 | CSDN付费下载自视觉中国随着现代技术的发展和完善&#xff0c;生活变得越来越舒适。虽然以前人们认为同时进行复杂的操作是不可能的&#xff0c;而如今计算机使这一任务变得很容易了。 与此同时&#…

神经网络 debug 太难了,这里有六个实用技巧

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达 神经网络的 debug 过程着实不容易&#xff0c;这里是一些有所帮助的 tips。基于神经网络的项目瓶颈通常并非对网络的实现。有时候&#xff0c;在编写了所有代码并尝试了一…

正则表达式re模块使用介绍

1. re模块的介绍 在Python中需要通过正则表达式对字符串进行匹配的时候&#xff0c;可以使用一个 re 模块 # 导入re模块 import re# 使用match方法进行匹配操作 result re.match(正则表达式,要匹配的字符串)# 如果上一步匹配到数据的话&#xff0c;可以使用group方法来提取数…