-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_in_rotated_sorted_array.rb
146 lines (125 loc) · 2.91 KB
/
search_in_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# https://leetcode.com/problems/search-in-rotated-sorted-array/description/
# 33. Search in Rotated Sorted Array
# You must write an algorithm with O(log n) runtime complexity.
# Binary search
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer}
def search(nums, target)
l, r = 0, nums.size - 1
result = -1
while l <= r
mid = (l + r) / 2
if nums[mid] == target
result = mid
break
end
# left half is sorted
if nums[l] < nums[mid]
# target is in sorted left half
if nums[l] <= target && target <= nums[mid]
r = mid - 1
else
l = mid + 1
end
# right half is sorted
else
# target is in sorted right half
if nums[mid] < target && target <= nums[r]
l = mid + 1
else
r = mid - 1
end
end
end
result
end
puts "Search in Rotated Sorted Array"
p search([4,5,6,7,0,1,2], 0)
# Output: 4
p search([4,5,6,7,0,1,2], 3)
# Output: -1
p search([1], 0)
# Output: -1
########################
# https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/
# 81. Search in Rotated Sorted Array II
# @param {Integer[]} nums
# @param {Integer} target
# @return {Boolean}
def search_ii(nums, target)
# nums.include?(target) ;)
search(nums, target) > -1
end
puts "Search in Rotated Sorted Array II"
p search_ii([2,5,6,0,0,1,2], 0)
# Output: true
p search_ii([2,5,6,0,0,1,2], 3)
# Output: false
########################
# https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
# 153. Find Minimum in Rotated Sorted Array
# @param {Integer[]} nums
# @return {Integer}
def find_min(nums)
return nil if nums&.empty? || nums.nil?
# nums.min # ;)
return nums.first if nums.size == 1 || nums.first < nums.last
l, r = 0, nums.size - 1
border = nums.first
output = border
while l <= r
mid = (l + r) / 2
if nums[mid] > border
l = mid + 1
else
r = mid - 1
output = [output, nums[mid]].min
end
end
output
end
puts "Find Minimum in Rotated Sorted Array"
p find_min([3,4,5,1,2])
# Output: 1
p find_min([4,5,6,7,0,1,2])
# Output: 0
p find_min([11,13,15,17])
# Output: 11
p find_min([11])
# Output: 11
########################
# Find Maximum in Rotated Sorted Array
# @param {Integer[]} nums
# @return {Integer}
def find_max(nums)
return nil if nums&.empty? || nums.nil?
# nums.max # ;)
return nums.last if nums.size == 1 || nums.first < nums.last
l, r = 0, nums.size - 1
border = nums.first
output = border
while l <= r
mid = (l + r) / 2
if nums[mid] < border
l = mid + 1
else
r = mid - 1
output = [output, nums[mid]].max
end
end
output
end
puts "Find Maximum in Rotated Sorted Array"
p find_max([3,4,5,1,2])
# Output: 5
p find_max([4,5,6,7,0,1,2])
# Output: 7
p find_max([11,13,15,17])
# Output: 17
p find_max([11])
# Output: 11
p find_max([])
# Output: nil
p find_max(nil)
# Output: nil