-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_range.js
81 lines (68 loc) · 1.64 KB
/
search_range.js
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
/*
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
*/
var searchRange = function(nums, target) {
if (nums.length === 1 && nums[0] === target) {
return [0, 0];
}
var begin = findBeginning(nums, target);
var end = findEnding(nums, target);
if (begin >= 0 && end >= 0) {
return [begin, end];
} else {
return [-1, -1];
}
};
function findBeginning(array, target) {
var left = 0;
var right = array.length - 1;
while (left + 1 < right) {
var mid = Math.floor(left + (right - left) / 2);
if (array[mid] === target) {
if (array[mid - 1] != target) {
return mid;
} else {
right = mid - 1;
}
} else if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (array[left] === target) {
return left;
}
if (array[right] === target) {
return right;
}
}
function findEnding(array, target) {
var left = 0;
var right = array.length - 1;
while (left + 1 < right) {
var mid = Math.floor(left + (right - left) / 2);
if (array[mid] === target) {
if (array[mid + 1] != target) {
return mid;
} else {
left = mid + 1;
}
} else if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (array[right] === target) {
return right;
}
if (array[left] === target) {
return left;
}
}