forked from ssitu001/toyProblemPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighLowIndex.js
66 lines (51 loc) · 1.66 KB
/
highLowIndex.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
// Given a sorted array of integers, return the low and high index of the given key. Return -1 if not found. The array length can be in millions with lots of duplicates.
function highLowIndex(arr, target, start, end) {
const indices = [];
const mid = start + Math.floor((end-start)/2);
if(arr[mid] === target) {
//check both side to exit fast;
if(arr[mid-1] !== target && arr[mid+1] !== target) {
indices.push(mid);
return indices;
}
if(arr[mid-1] === target) {
indices.push(binarySearch(arr, target, 0, mid, "left"))
} else {
indices.push(mid);
}
if(arr[mid+1] === target) {
indices.push(binarySearch(arr, target, mid+1, end, "right"));
} else {
indices.push(mid);
}
} else {
if(arr[mid] < target) {
//we only care about the right side
return highLowIndex(arr, target, mid+1, end)
} else {
//we only care about the left side
return highLowIndex(arr, target, 0, mid)
}
}
return indices;
}
function binarySearch(arr, target, start, end, direction) {
const mid2 = start + Math.floor((end-start)/2)
if(arr[mid2] === target) {
if(direction === "right") {
if(arr[mid2] === arr[mid2+1]) {
return binarySearch(arr, target, mid2+1, end, "right");
} else {
return mid2;
}
} else {
if(arr[mid2] === arr[mid2-1]) {
return binarySearch(arr, target, 0, mid2-1, "left");
} else {
return mid2;
}
}
}
}
const arr = [1,2,5,5,5,5,5,5,5,5,10];
highLowIndex(arr, 10, 0, arr.length-1);