当前位置:网站首页>[binary search - medium] 1855 Maximum distance of subscript alignment (need to review double pointer method)
[binary search - medium] 1855 Maximum distance of subscript alignment (need to review double pointer method)
2022-04-21 15:11:00 【Caicai 2022】
subject
Example 1:
Input :nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
Output :2
explain : The valid subscript pair is (0,0), (2,2), (2,3), (2,4), (3,3), (3,4) and (4,4) .
The maximum distance is 2 , Corresponding subscript pair (2,4) .
Example 2:
Input :nums1 = [2,2,2], nums2 = [10,10,1]
Output :1
explain : The valid subscript pair is (0,0), (0,1) and (1,1) .
The maximum distance is 1 , Corresponding subscript pair (0,1) .
Example 3:
Input :nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
Output :2
explain : The valid subscript pair is (2,2), (2,3), (2,4), (3,3) and (3,4) .
The maximum distance is 2 , Corresponding subscript pair (2,4) .
Tips :
1 <= nums1.length <= 105
1 <= nums2.length <= 105
1 <= nums1[i], nums2[j] <= 105
nums1 and nums2 All are Non-increasing Array
【 Code 1: Two point search 】
Execution time :796 ms, In all Python3 Defeated in submission 10.69% Users of
Memory consumption :28.8 MB, In all Python3 Defeated in submission 73.10% Users of
Pass the test case :32 / 32
class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
maxSpan=0
for idx,item in enumerate(nums1):
# Find from left to right num2 in the last one >=item The subscript of the number
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
【 Method 2: Double pointer 】
Execution time :108 ms, In all Python3 Defeated in submission 99.31% Users of
Memory consumption :28.8 MB, In all Python3 Defeated in submission 51.72% Users of
Pass the test case :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
版权声明
本文为[Caicai 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211507332753.html
边栏推荐
- 【JS】URLSearchParams 对象(以对象的形式上传参数到url)
- SAP ui5 application development tutorial 70 - how to use button controls to trigger page routing jump trial version
- [binary search - simple] 278 First wrong version
- 一个简单标注库的插件化开发实践
- JD cloud has launched cloud computing to create an unbounded office experience for the future
- Dry goods | hidden rules for workplace promotion of testers: workplace advice of Senior Test Manager with 15 years of experience
- android. database. sqlite. SQLiteException: Can't downgrade database from version 2 to 1
- ENSP三层交换机连接二层交换机及路由器的做法
- [JS] urlsearchparams object (upload parameters to URL in the form of object)
- 【二分查找-简单】剑指 Offer II 072. 求平方根
猜你喜欢

一个简单标注库的插件化开发实践

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

Qt网络与通信(TCP聊天室)

Amazon evaluates the autotrophic number. What should sellers do if they want to get a review?

Would like to ask how to find the software registration code from the database

Detailed explanation of spark SQL underlying execution process

【历史上的今天】4 月 21 日:微处理器先驱诞生;Winamp 发布;COPPA 正式生效

MYSQL 第1章 数据库简介

Mysql 执行流程

融云首席科学家任杰:互联网兵无常势,但总有人正年轻
随机推荐
B站可以称为中国的YouTube吗?
优麒麟 22.04 LTS 版本系统将于 4 月 22 日发布,搭载全新 UKUI 3.1
冬天到了,给你的网站下个雪吧
Mysql8.0以上重置初始密码的方法
数字化时代,SaaS软件如何成为国产化替代的轻骑兵?
让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
专题测试04·多元函数微分学【李艳芳全程班】
Spark/Scala - 读取 RcFile && OrcFile
Improvement of ref and struct in C 11
【二分查找-简单】剑指 Offer II 072. 求平方根
Mysql8. Method for resetting initial password above 0
【C语言】C语言标准库大梳理(超全)
lightGBM专题5:pyspark表数据处理之数据合并
干货 | 环境问题还是测试的老大难?两个步骤轻松搞定
Software testing (III) p51-p104 software test case methods and defects
C language preprocessing problem
期货开户找哪家公司?哪家期货公司最安全?
[cloud based co creation] Huawei cloud database - basic knowledge
[introduction to machine learning] - Lesson 6 - Bayesian classifier
[binary search - simple] 441 Arrange coins