-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_rotated_sorted_array.rb
57 lines (48 loc) · 1.24 KB
/
search_rotated_sorted_array.rb
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
# Suppose an array sorted in ascending order 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).
#
# You are given a target value to search. If found in the array return its index, otherwise return -1.
#
# You may assume no duplicate exists in the array.
#
# ====================
#
# procedure:
# - find index of minimum with binary search
# - now do two more binary search
# - one to the left of pivot
# - one including the pivot and to the right
def search(nums, target)
return -1 if nums.empty?
min_idx = find_min_idx(nums)
find_idx(nums, target, 0, min_idx) ||
find_idx(nums, target, min_idx, nums.length - 1) ||
-1
end
def find_min_idx(nums)
left = 0
right = nums.length - 1
while left + 1 < right
mid = left + (right - left) / 2
if nums[mid] > nums.last
left = mid
else
right = mid
end
end
nums[left] < nums[right] ? left : right
end
def find_idx(nums, target, left, right)
while left + 1 < right
mid = left + (right - left) / 2
if nums[mid] > target
right = mid
else
left = mid
end
end
return left if nums[left] == target
return right if nums[right] == target
end
puts search([4, 5, 6, 7, 0, 1, 2], 6) == 2