Group_002

4. Median of Two Sorted Arrays

平凡解法:

先将 nums1nums2 归并成一个数组 merged_arr ,然后再对归并后的数组求中位数。代码如下:

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        m, n = len(nums1), len(nums2)
        i, j = 0, 0
        merged_arr = []
        while i <= m-1 and j <= n-1:
            if nums1[i] < nums2[j]:
                merged_arr.append(nums1[i])
                i += 1
            else:
                merged_arr.append(nums2[j])
                j += 1
        if i <= m-1:
            merged_arr.extend(nums1[i:])
        if j <= n-1:
            merged_arr.extend(nums2[j:])
        print(f'merged_arr = {merged_arr}')

        # When the `while` loop finished, we can get a merged array.
        # Then we just need to find the median of the merged array.
        if (m + n) % 2 == 1: # odd
            return merged_arr[(m+n)//2]
        else: # even
            return (merged_arr[(m+n)//2 - 1] + merged_arr[(m+n)//2]) / 2 # Note: accurate division and truncating division, 
                                                                         # see https://blog.youkuaiyun.com/feixingfei/article/details/7081446 for detail.


if __name__ == '__main__':
    sol = Solution()
    nums1 = [1, 2]
    nums2 = [3, 4]
    res = sol.findMedianSortedArrays(nums1, nums2)
    print(f'res = {res}')

这里面要注意三点:

这种做法能够被 AC,但是它的时间复杂度是 O ( m + n ) O(m+n) O(m+n) 。(参见: 归并排序时间复杂度分析

要想让时间复杂度达到 log ⁡ \log log 级别,只能通过二分查找去做。如果用这种方法去做的话,那我们第一步要确定是要找一个数还是要找两个数。注意到 nums1nums2 都是已经排过序的,这就为二分查找创造了条件。至此,这个问题就变成了“在两个数组中的二分查找问题”。这里边的关键是怎么样去压缩我们要查找的范围。为此,这里边要用到四个指针,它们分别指向 nums1nums2 的头部和尾部:

nums1[lo1], nums1[hi1]
nums2[lo2], nums2[hi2]

时间复杂度为 O ( log ⁡ ( min ⁡ ( m , n ) ) O(\log (\min (m, n)) O(log(min(m,n)) 的解法:(强烈推荐)

Python 版本代码如下:

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        # If len(nums1) > len(nums2), then switch them so that nums1 is smaller than nums2.
        # This operation is used to decrease the time complexity so that the final time complexity 
        # will definitely be O(log(min(m, n))). Because once we guarantee nums1 is the smallest length, 
        # we always do binary search in the shortest list.
        if len(nums1) > len(nums2):
            nums1, nums2 = nums2, nums1
        
        x, y = len(nums1), len(nums2)
        lo, hi = 0, x # Note: here `hi` cannot be `x-1`
        while lo <= hi:
            partitionX = (lo + hi) // 2
            partitionY = (x + y + 1) // 2 - partitionX # Note: partitionX + partitionY = (x + y + 1) // 2
            
            # if partitionX is 0 it means nothing is there on left side. Use -INF for maxLeftX
            # if partitionX is the length of input then there is nothing on right side. Use +INF for minRightX
            maxLeftX = float('-inf') if partitionX == 0 else nums1[partitionX - 1]
            minRightX = float('inf') if partitionX == x else nums1[partitionX]
            
            maxLeftY = float('-inf') if partitionY == 0 else nums2[partitionY - 1]
            minRightY = float('inf') if partitionY == y else nums2[partitionY]
            
            if maxLeftX <= minRightY and maxLeftY <= minRightX:
                if (x + y) % 2 == 0:
                    return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
                else:
                    return max(maxLeftX, maxLeftY)
            elif maxLeftX > minRightY: # we are too far on right side for partitionX. Go on left side.
                hi = partitionX - 1
            else: # we are too far on left side for partitionX. Go on right side.
                lo = partitionX + 1

内容概要:本文档主要介绍了Intel Edge Peak (EP) 解决方案,涵盖从零到边缘高峰的软件配置和服务管理。EP解决方案旨在简化客户的入门门槛,提供一系列工具和服务,包括Edge Software Provisioner (ESP),用于构建和缓存操作系统镜像和软件栈;Device Management System (DMS),用于远程集群或本地集群管理;以及Autonomous Clustering for the Edge (ACE),用于自动化边缘集群的创建和管理。文档详细描述了从软件发布、设备制造、运输、安装到最终设备激活的全过程,并强调了在不同应用场景(如公共设施、工业厂房、海上油井和移动医院)下的具体部署步骤和技术细节。此外,文档还探讨了安全设备注册(FDO)、集群管理、密钥轮换和备份等关键操作。 适合人群:具备一定IT基础设施和边缘计算基础知识的技术人员,特别是负责边缘设备部署和管理的系统集成商和运维人员。 使用场景及目标:①帮助系统集成商和客户简化边缘设备的初始配置和后续管理;②确保设备在不同网络环境下的安全启动和注册;③支持大规模边缘设备的自动化集群管理和应用程序编排;④提供详细的密钥管理和集群维护指南,确保系统的长期稳定运行。 其他说明:本文档是详细描述了Edge Peak技术及其应用案例。文档不仅提供了技术实现的指导,还涵盖了策略配置、安全性和扩展性的考虑,帮助用户全面理解和实施Intel的边缘计算解决方案。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值