-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInRotatedSortedArray.kt
56 lines (50 loc) · 1.45 KB
/
SearchInRotatedSortedArray.kt
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
package leetcode
/**
* Problem description on [LeetCode](https://leetcode.com/problems/search-in-rotated-sorted-array/)
*/
class SearchInRotatedSortedArray {
fun search(nums: IntArray, target: Int): Int {
if (nums.isEmpty()) {
return -1
}
// not rotated
if (nums.first() < nums.last()) {
return binarySearch(nums, 0, nums.size, target)
}
// rotated
val k = minIndex(nums)
return if (target in nums[k]..nums.last()) {
binarySearch(nums, k, nums.size, target)
} else {
binarySearch(nums, 0, k, target)
}
}
private fun binarySearch(nums: IntArray, first: Int, last: Int, target: Int): Int {
var left = first
var right = last - 1
while (left <= right) {
val middle = (left + right) / 2
if (target == nums[middle]) {
return middle
} else if (target < nums[middle]) {
right = middle - 1
} else {
left = middle + 1
}
}
return -1
}
private fun minIndex(nums: IntArray): Int {
var left = 0
var right = nums.size - 1
while (left + 1 < right) {
val middle = (left + right) / 2
if (nums[left] < nums[middle]) {
left = middle
} else {
right = middle
}
}
return right
}
}