-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate_array.py
64 lines (48 loc) · 1.83 KB
/
rotate_array.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# https://leetcode.com/problems/search-in-rotated-sorted-array/
# You are given an integer array nums sorted in ascending order (with distinct values), and an integer target.
# Suppose that nums is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
# If target is found in the array return its index, otherwise, return -1.
# Method 1
def search1(nums, target):
if target >= nums[0]:
for i in nums:
if target == i:
return nums.index(i)
if target <= nums[0]:
i = len(nums)-1
while i >=0:
if target == nums[i]:
return i
i -= 1
return (-1)
# Method 2: Find the pivot (O(log n)) => binary search on one of the half
def search(nums, target):
# Find the index of the pivot number
for i in range(len(nums)-1):
if nums[i]>nums[i+1]:
pivot_idx = i+1
return pivot_idx
# if target >= nums[0]:
# # binary search on the first half - nums[0:pivot_idx]
# mid_idx = len(nums[0:pivot_idx])/2
# if target == nums[mid_idx]:
# return mid_idx
# # elif target > nums[mid_idx]:
# # binary search on the second half - nums[pivot_idx:]
def binarysearch(lst, target):
mid_idx = 0
if len(lst)>1:
mid_idx = len(lst)//2
print(mid_idx, lst[mid_idx], target)
if target == lst[mid_idx]:
return mid_idx
elif target > lst[mid_idx]:
binarysearch(lst[mid_idx:], target)
elif target < lst[mid_idx]:
binarysearch(lst[:mid_idx], target)
# [4,5,6,7,0,1,2] #2nd half always smaller
print(search([0,1,2,4,5,6,7],2)) #2
print(search([4,5,6,7,0,1,2],2)) #6
print(search([4,5,6,7,0,1,2],3)) #-1
print(search([4,5,6,7,0,1,2],8)) #-1
print(binarysearch([0,1,2],2))