【LeetCode】解题4:Median of Two Sorted Arrays

大模型LoRA微调调参的实战技巧(持续更新) 本文主要介绍了大模型LoRA微调调参的实战技巧,希望对学习大语言模型的同学们有所帮助。 文章目录 1. 前言 2. 实战技巧 阅读详情

Problem 4: Median of Two Sorted Arrays [Hard]

There are two sorted arrays nums1 and nums2 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)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

来源:LeetCode

解题思路

中位数可以转化为寻找一个数组中的第K小的数或是第K小和第K+1小的数的平均值。
如下图数组1长度为4,数组2长度为10,则中位数为第7小和第8小数之和除以2。
考虑获取第7小数的情况:比较两个数组第k/2小数(即第3小数)的值,更小的一方可以直接去掉3个数(1,2,3),剩下的数(1,3,4,9,4,5,6,7,8,9,10)可以递归寻找第4(7-3=4)小数。
在这里插入图片描述
图源:LeetCode题解

每一轮递归可去掉k/2个数,因此时间复杂度为O(log k),而k=(m+n)/2,因此可保证题目要求复杂度O(log (m+n))。

要点:递归、二分法

Solution (Java)

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int m = nums1.length;
        int n = nums2.length;
        if((m+n)%2 == 1){
            return findKnum(nums1, 0, m-1, nums2, 0, n-1, (m+n)/2+1);
        }
        else{
            return (findKnum(nums1, 0, m-1, nums2, 0, n-1, (m+n)/2) + findKnum(nums1, 0, m-1, nums2, 0, n-1, (m+n)/2+1))/2;
        }
    }
    private double findKnum(int[] nums1, int l1, int r1, int[] nums2, int l2, int r2, int k) {
        int len1 = r1 - l1 + 1;
        int len2 = r2 - l2 + 1;
        if(len1 > len2) return findKnum(nums2, l2, r2, nums1, l1, r1, k);

        if(len1 == 0) return nums2[l2 + k-1];
        if(k == 1) return Math.min(nums1[l1], nums2[l2]);
        int index1 = Math.min(r1, l1 + k/2-1);
        int index2 = l2 + k/2-1;
        if(nums1[index1] < nums2[index2]){
            k = k - (index1 - l1 + 1);
            return findKnum(nums1, index1+1, r1, nums2, l2, r2, k);
        }
        else{
            k = k - (index2 - l2 + 1);
            return findKnum(nums1, l1, r1, nums2, index2+1, r2, k);
        }
    }
}

修改过程

  • 考虑len1>len2的情况下调换数组顺序,便于后续保证只有数组1才有个数为0或个数小于k/2的情况。
【sealos安装指南】基于 Sealos 安装 Kubernetes 1.33.6 高可用集群 本文介绍使用Sealos工具快速搭建Kubernetes集群的详细指南。Sealos简化了K8s部署流程,自动完成系统调优,适合新手学习使用。文章提供了服务器规划、环境准备、在线/离线两种安装方式的具体步骤,包括主机名设置、swap关闭、数据目录配置等。重点讲解了Sealos的安装命令参数说明,以及添加/删除节点等常用操作。推荐使用Calico网络组件确保稳定性,并附有安装成功后的验证截图。该指南能帮助用户快速搭建一个包含master和worker节点的K8s集群,底层基于containerd运行。 阅读详情

相关推荐

Power Automate实战:用SharePoint REST API批量处理列表数据(附JSON解析技巧)

本文深入探讨了如何利用Power Automate调用SharePoint REST API实现列表数据的批量自动化处理。文章详细解析了API的协同工作原理、高效的数据获取与筛选技巧,并重点分享了JSON数据解析的核心难点与实战解决方案,旨在帮助企业用户构建稳定可靠的数据处理流程,提升运营效率。

coffee的博客 784

4. Median of Two Sorted Arrays(两个有序数组的中位数)

有两个有序数组nums1和nums2分别为m和n。 找到两个有序数组的中位数。 总运行时间复杂度应为O(log(m + n))。 您可以假设nums1和nums2不能都为空。 例 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 例2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 =...

czp_374的博客 531

ICM20948 DMP代码详解(69)

ICM20948 DMP代码详解(69)

phmatthaus的专栏 624

【leetcode-Python】- 二分搜索 - 4. Median of Two Sorted Arrays

题目链接 4. Median of Two Sorted Arrays 题目描述 给定两个非降序数组nums1和nums2,大小分别为m和n,返回两个数组合并后的中位数。要求时间复杂度为O(log(m+n)). 示例 输入:nums1 = [1,3],nums2 = [2] 输出:2.00000 输入:nums1 = [1,2],nums2 = [3,4] 输出:2.50000 输入:nums1= [],nums2 = [1] 输出:1.0000 解决思路一 插一句题外话

gulaixiangjuejue的博客 647

2ED2410-EM的使用

2ED2410-EM的使用

让你爱上电路设计 995

手把手教你学Simulink——基于通信协议与标准的场景实例:卫星通信中的网络安全协议(IPSec)仿真

随着卫星通信在国防、金融、应急指挥、能源管网等关键领域的广泛应用,其安全性已成为系统设计的核心要求。然而,卫星链路具有广播特性、长距离传输、地面站分布广等特点,极易遭受窃听、篡改、重放、中间人攻击等网络威胁。传统的“信任链路层安全”已不足以应对现代网络攻击。为此,国际标准组织(如IETF、ETSI、CCSDS)推荐在卫星通信中部署协议,实现端到端的数据机密性、完整性、身份认证和抗重放保护。本文将手把手教你如何在中构建一个符合标准的仿真系统,涵盖从明文传输到加密隧道的完整安全链路建模。本文完整实现了。

MHD0815的博客 158

leetcode之 median of two sorted arrays

这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉。后来搜了一下leetcode的难度分布表(leetcode难度及面试频率)才发现,该问题是难度为5的问题,真是小看了它!网上搜了很多答案,但是鲜见简明正确的解答,唯有一种寻找第k小值的方法非常好,在此整理一下。        首先对leetcode的

姚光超的专栏 9万+

【Zabbix实战之部署篇】Docker部署Zabbix+grafana监控平台

【Zabbix实战之部署篇】docker部署Zabbix+grafana监控平台

jks212454的博客 2590

LeetCode——Median of Two Sorted Arrays

看到这个题,坑定有很多东西都忘记了吧!没事!进来就行了,你不会的差不多我也不会,我都有解释清楚,进来吧!一块成长进步! 哈哈!做完了,第一次得到那么高的评价! 一下子高兴过头了,回头来发现自己还是那么差 其实还是挺难的,我看半天都没看懂

Blackoutdragon的博客 605

ssd训练自己的数据集

基于SSD实现目标检测(训练自己的数据集)

reset2021的博客 6806

18、近端策略优化(PPO)算法详解

本文详细介绍了近端策略优化(PPO)算法的理论基础、算法流程、实现方法及实际应用。PPO作为当前最受欢迎的策略梯度算法之一,具有易于实现、计算成本低、样本效率高和性能优越等优点。文章对比了基于自适应KL惩罚和基于裁剪目标的两种PPO变体,重点解析了裁剪目标函数的设计原理及其在Actor-Critic框架下的实现方式,并通过Atari Pong游戏展示了完整的训练配置与流程。最后总结了PPO的优势、适用场景及未来发展方向,为强化学习研究者和实践者提供了全面的参考。

mongodb5scout的博客 184

【算法】Median of Two Sorted Arrays

Median of two sorted array.

qq_27198345的博客 551

YOLO26优化:注意力魔改 | SKAttention注意力,自适应地调整其感受野大小,效果优于SENet

SKAttention输入自适应地调整其感受野大小的能力

①订阅者答疑群聊服务;②FDE:工业缺陷检测项目;③paper/创新算法指导;④免费提供YOLO创新涨点推荐顾问Skill和AI小怪兽数字分身; 260

Leetcode #4 Median of Two Sorted Arrays 解题小节

1 题目理解leetcode原题查看 这道题给定了两个已经排序号的数组的数组,现在要求给出其中位数。 如果简单着去理解的话,我们假设一个长度为n,一个长度为m,我们用两个指针分别指向两个数组,比较着选择小的那一方的指针+1,直到总共比较大约是O(m+n))的复杂度。然而leetcode肯定不会让你那么easy的就ac这道题了,于是他要求 O(log (m+n)).有了一个log,那我们可以很容

MebiuW的专栏 2463

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

LeetCode 4 Median of Two Sorted Arrays 解题报告

原文链接: http://hankerzheng.com/blog/LeetCode-Median-of-Two-Sorted-ArraysProblem DescriptionLeetCode 4 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectively

hanzheng992的博客 435

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

LeetCode - 解题笔记 - 4 - Median of Two Sorted Arrays

概览Median of Two Sorted ArraysSolution 1中位数的定义问题建模边界值的处理搜索策略solution 2 Median of Two Sorted Arrays Solution 1 (这个题的思路稍稍有点复杂,来自官方的解答) 单纯从时间要求和数据量上看,这道题只能是使用对数复杂度的算法(二分、堆……)。从问题已知条件上看,首先中位数一定存在,其次两个数组有序。 中位数的定义 本题用到的一个很关键的思路就是直接在有序数组上反映中位数的数学定义:给定的nnn个数中,存在1个

支锦铭的博客 266

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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 two sorted arrays. The overall run time complexity should be O(log (m+n)). 难度:Hard 解题

wengyunpeng的博客 557

【LeetCode】4. Median of Two Sorted Arrays解题报告(Python)

参考博客 测试代码: class Solution: def getKth(self, A, B, k): k = int(k) lenA = len(A); lenB = len(B) if lenA &amp;amp;amp;gt; lenB: return self.getKth(B, A, k) if lenA == 0: r...

L141210113的专栏 1216

[leetcode] 4. Median of Two Sorted Arrays 解题报告

题目链接: https://leetcode.com/problems/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 sorted arrays. The overall run

小榕流光的专栏 837

【LeetCode】4.Median of Two Sorted Arrays 解题

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 sorted arrays. The overall run time complexity should be O(log (m+n)).

KatherineLeeyq的博客 311

LeetCode解题笔记 - 4. Median of Two Sorted Arrays

There are two sorted arraysnums1andnums2of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [...

a541967687的博客 168

【LeetCode】4. Median of Two Sorted Arrays 解题报告(Python)

【LeetCode】4. Median of Two Sorted Arrays 解题报告(Python)题目描述解法:类二分查找 题目地址:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目描述 There are two sorted arrays nums1 and nums2 of size m and n respe...

ttinch的博客 226

LeetCode #4解题: Median of two sorted arrays(双边剔除法)

原始思路: 合并两个数组, 找到合并之后数组的最小值和最大值, 剔除这两个数, 继续找剩余的数组的最大值和最小值, 继续剔除两个最值, ... 直到最后剩下的1个或2个数, 就可以求出中位数了.

qwaywait的博客 238

4. Median of Two Sorted Arrays - Hard - Leetcode解题报告

There are two sorted arraysnums1andnums2of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assumenums1andn...

weixin_34185560的博客 114

leetcode解题之4. Median of Two Sorted Arrays java (两个排序数组的中位数)

leetcode解题之4. Median of Two Sorted Arrays java (两个排序数组的中位数)

mine_song的博客 1244

[Leetcode] 4. Median of Two Sorted Arrays 解题报告

题目: 思路: 代码:

魔豆(Magicbean)的博客 788
上一篇: 【LeetCode】解题1:Two Sum
下一篇: 【LeetCode】解题11:Container With Most Water
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值