-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathRotated_Sorted_Array.py
More file actions
37 lines (34 loc) · 1.25 KB
/
Copy pathRotated_Sorted_Array.py
File metadata and controls
37 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Time Complexity : O(log n) - binary search.
# Space Complexity : O(1) - storing only pointers.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
#Your code here along with comments explaining your approach in three sentences only
#Search using binary search check if middle is the target if yes return.
#If not check if left is sorted or right is sorted. If left is sorted check if target is in the range of left else check in right.
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
l , r = 0 , len(nums) - 1
while l <= r:
mid = l + (r - l) // 2
if target == nums[mid]:
return mid
#left sorted
if nums[l] <= nums[mid]:
#right side
if target > nums[mid] or target < nums[l]:
l = mid + 1
else:
r = mid - 1
# Right sorted
else:
#left side
if target < nums[mid] or target > nums[r]:
r = mid - 1
else:
l = mid + 1
return -1