-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0034. Find First and Last Position of Element in Sorted Array.js
94 lines (77 loc) · 1.85 KB
/
0034. Find First and Last Position of Element in Sorted Array.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
82
83
84
85
86
87
88
89
90
91
92
93
94
// Given an array of integers nums 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 nums.length).
//
// If the target is not found in the array, return [-1, -1].
//
// Example 1:
//
// Input: nums = [5,7,7,8,8,10], target = 8
// Output: [3,4]
//
// Example 2:
//
// Input: nums = [5,7,7,8,8,10], target = 6
// Output: [-1,-1]
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
// 1) Cheating
// Time O(n)
// Space O(1)
const searchRange1 = (nums, target) => {
return [nums.indexOf(target), nums.lastIndexOf(target)];
};
// 2) Brute force / Linear scan
// Time O(n)
// Space O(1)
// 3) Binary search
// Time O(log n)
// Space O(1)
const searchRange3 = (nums, target) => {
let res = [-1, -1];
// find the left
let l = 0;
let r = nums.length - 1;
while (l < r) {
const m = ~~((l + r) / 2);
if (nums[m] < target) l = m + 1;
else r = m;
}
if (nums[l] !== target) return res;
else res[0] = l;
// find the right
r = nums.length - 1; // no need to set l to 0
while (l < r) {
const m = Math.ceil((l + r) / 2); // note using Math.ceil
if (nums[m] > target) r = m - 1;
else l = m;
}
res[1] = r;
return res;
};
// 4) Similar to 3), not optimized, but easier to understand
const searchRange = (nums, target) => {
const res = [-1, -1];
// find the left
let l = 0;
let r = nums.length - 1;
while (l < r) {
const m = ~~((l + r) / 2);
if (nums[m] < target) l = m + 1;
else r = m;
}
if (nums[l] === target) res[0] = l;
// find the right
l = 0;
r = nums.length - 1;
while (l < r) {
const m = Math.ceil((l + r) / 2); // note using Math.ceil
if (nums[m] > target) r = m - 1;
else l = m;
}
if (nums[r] === target) res[1] = r;
return res;
};