[NeetCode 150] Find Target in Rotated Sorted Array

Find Target in Rotated Sorted Array

You are given an array of length n which was originally sorted in ascending order. It has now been rotated between 1 and n times. For example, the array nums = [1,2,3,4,5,6] might become:

[3,4,5,6,1,2] if it was rotated 4 times.
[1,2,3,4,5,6] if it was rotated 6 times.
Given the rotated sorted array nums and an integer target, return the index of target within nums, or -1 if it is not present.

You may assume all elements in the sorted rotated array nums are unique,

A solution that runs in O(n) time is trivial, can you write an algorithm that runs in O(log n) time?

Example 1:

Input: nums = [3,4,5,6,1,2], target = 1

Output: 4

Example 2:

Input: nums = [3,5,6,0,1,2], target = 4

Output: -1

Constraints:

1 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
-1000 <= target <= 1000

Solution

The rotated sorted array can be split into 2 parts: 2 sorted sub-arrays (Property 1) and the minimum number in former array is greater than the maximum number in latter array. (Property 2)

Obviously, to achieve O(log⁡n)O(\log n)O(logn) time complexity we need the power of binary search. However, the rotation breaks the monotony, so how can we solve this?

With property 1, we can know that binary search still works on the single sorted sub-array. Besides, when we look at the process of binary search: we divide the array from the middle. So, at least one side of the divided array is monotonical!

Therefore, all we need to do is to determine which side is monotonical and then determine whether target is in the monotonical interval. If so, the problem evolves back to a normal binary search; if not, we can continue dividing the unordered interval until so.

To determine which side is monotonical, we can use if nums[le] <= nums[mid] according to property 2, without the worry that interval [le,mid][le, mid][le,mid] is crossing the unordered part.

Code

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        le = 0
        ri = len(nums)-1
        while le <= ri:
            mid = (le+ri)//2
            if nums[mid] == target:
                return mid
            if nums[le] <= nums[mid]:
                if nums[le] <= target < nums[mid]:
                    ri = mid-1
                else:
                    le = mid+1
            else:
                if nums[mid] < target <= nums[ri]:
                    le = mid+1
                else:
                    ri = mid-1
        return -1
        
        
由于没有提供具体的站内引用内容,以下是关于Rotated 3DMatch的一般性介绍。 3DMatch是一个用于三维点云匹配的数据集和任务。而Rotated 3DMatch可能是在3DMatch基础上引入了旋转因素的扩展或变体。 在三维点云匹配任务中,通常需要找到不同视角下点云之间的对应关系。Rotated 3DMatch可能主要关注在点云存在旋转的情况下,如何更准确地进行匹配。这可能涉及到开发能处理旋转不变性的特征提取算法,以在不同旋转姿态的点云之间建立可靠的对应关系。 例如,在一些实际应用场景中,如机器人导航、三维重建等,点云数据常常是从不同的角度和姿态获取的,这就导致点云存在旋转。Rotated 3DMatch数据集可能提供了一系列带有旋转信息的点云对,用于评估和训练点云匹配算法在旋转情况下的性能。 对于处理Rotated 3DMatch问题的算法,可能会结合深度学习方法,利用神经网络学习旋转不变的特征表示。一些网络架构可能会专门设计用于捕捉点云的几何结构和旋转信息,从而提高在旋转点云匹配上的准确性。 ```python # 这里只是一个简单示例,实际处理Rotated 3DMatch要复杂得多 import numpy as np # 模拟两个旋转后的点云 point_cloud_1 = np.random.rand(100, 3) rotation_matrix = np.array([[np.cos(np.pi/4), -np.sin(np.pi/4), 0], [np.sin(np.pi/4), np.cos(np.pi/4), 0], [0, 0, 1]]) point_cloud_2 = np.dot(point_cloud_1, rotation_matrix.T) # 简单的匹配思路,这里只是示例,不是完整算法 def simple_matching(pc1, pc2): # 简单计算最近邻 distances = np.linalg.norm(pc1[:, np.newaxis] - pc2, axis=2) matches = np.argmin(distances, axis=1) return matches matches = simple_matching(point_cloud_1, point_cloud_2) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值