当前位置:网站首页>【二分搜索-中等】1855. 下标对中的最大距离(需要复习双指针法)
【二分搜索-中等】1855. 下标对中的最大距离(需要复习双指针法)
2022-04-21 15:08:00 【菜菜2022】
题目
示例 1:
输入:nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
输出:2
解释:有效下标对是 (0,0), (2,2), (2,3), (2,4), (3,3), (3,4) 和 (4,4) 。
最大距离是 2 ,对应下标对 (2,4) 。
示例 2:
输入:nums1 = [2,2,2], nums2 = [10,10,1]
输出:1
解释:有效下标对是 (0,0), (0,1) 和 (1,1) 。
最大距离是 1 ,对应下标对 (0,1) 。
示例 3:
输入:nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
输出:2
解释:有效下标对是 (2,2), (2,3), (2,4), (3,3) 和 (3,4) 。
最大距离是 2 ,对应下标对 (2,4) 。
提示:
1 <= nums1.length <= 105
1 <= nums2.length <= 105
1 <= nums1[i], nums2[j] <= 105
nums1 和 nums2 都是 非递增 数组
【代码1:二分搜索】
执行用时:796 ms, 在所有 Python3 提交中击败了10.69% 的用户
内存消耗:28.8 MB, 在所有 Python3 提交中击败了73.10% 的用户
通过测试用例:32 / 32
class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
maxSpan=0
for idx,item in enumerate(nums1):
#从左到右寻找 num2中 最后一个>=item的数字的下标
left,right,ans=idx,len(nums2)-1,-1
while left<=right:
mid=left+(right-left)//2
if nums2[mid]>=item:
ans=mid
left=mid+1
else:
right=mid-1
# print("cur:",item," ",left,right," ans:",ans)
if ans!=-1:
maxSpan=max(maxSpan,ans-idx)
return maxSpan
【方法2:双指针】
执行用时:108 ms, 在所有 Python3 提交中击败了99.31% 的用户
内存消耗:28.8 MB, 在所有 Python3 提交中击败了51.72% 的用户
通过测试用例:32 / 32
class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
i, j = 0, 0
while i < m and j < n:
if nums1[i] > nums2[j]:
i += 1
j += 1
return j - i - 1 if j - i - 1 > 0 else 0
版权声明
本文为[菜菜2022]所创,转载请带上原文链接,感谢
https://blog.csdn.net/kz_java/article/details/124273492
边栏推荐
- Minimum steps of manufacturing letter ectopic words - C language solution
- Dry goods | app control positioning of mobile app automation
- Mysql数据库(2)
- MySQL 8.0.11安装教程(Windows版)
- Beautiful solution for initialization of golang Gorm framework
- lightGBM专题3:PySpark中的StringIndexer和pipeline功能实现
- Dbaver cannot connect to the database. How to solve it?
- 数据库基础知识详解五:MySQL中的索引和其两种引擎、主从复制以及关系型/非关系型数据库
- How to call uniCloud cloud storage in Nodejs project to upload middle note files
- 阿里P9详解高并发,带你了解淘宝怎么抗住双11等大型秒杀活动
猜你喜欢

QT network and communication (TCP chat room)

Shang Silicon Valley smart campus - 6. Realization of administrator function

数据库基础知识详解五:MySQL中的索引和其两种引擎、主从复制以及关系型/非关系型数据库

怎么去约束代码的统一性

It's been 2 years since the career change test. Give some advice to girls who are still hesitating

pytorch图像分类篇:pytorch官方demo实现一个分类器(LeNet)

elemetn表单控件---未通过校验字段提交时自动定位到该字段位置

数字化时代,企业运维面临现状及挑战分析解读

突然掉电,为啥MySQL也不会丢失数据?(收藏)

ThreadLocal应用及原理解析
随机推荐
lightGBM专题3:PySpark中的StringIndexer和pipeline功能实现
MySQL数据库实际运用(一)
Mysql8. Method for resetting initial password above 0
Amazon evaluates the autotrophic number. What should sellers do if they want to get a review?
lightGBM专题2:基于pyspark在spark平台下lightgbm训练详解
Dbaver cannot connect to the database. How to solve it?
Dry goods | environmental problems or chronic difficulties in testing? It's easy to do it in two steps
【云驻共创】华为云数据库-基础知识
Special test 04 · differential calculus of multivariate functions [Li Yanfang's whole class]
武汉科技大学C语言上机实验题(第一第二第三部分)
OpenHarmony3.1 H264视频播放之路
Service中是如何产生ANR的?
Jetpack Compose使用自定义操作符实现绘制五角星效果
赏金猎人自动交易机器人开发模式分析
C language preprocessing problem
怎么去约束代码的统一性
如何在Nodejs项目中调用uniCloud云存储进行上传文件
Analysis on development mode of bounty hunter automatic Trading Robot
How to provide CPU branch prediction efficiency at the code level
手动调整slf4j的日志等级