-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsMajorityElement.js
More file actions
32 lines (31 loc) · 951 Bytes
/
IsMajorityElement.js
File metadata and controls
32 lines (31 loc) · 951 Bytes
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
var isMajorityElement = function(nums, target) {
let firstOccurrence = binarySearch(nums, 0, nums.length-1, target);
console.log(firstOccurrence);
let firstPos = firstOccurrence;
let lastPos = firstOccurrence;
let tmp;
while (firstPos !== -1) {
tmp = firstPos;
firstPos = binarySearch(nums, 0, firstPos-1, target);
}
firstPos = tmp;
while(lastPos !== -1) {
tmp = lastPos;
lastPos = binarySearch(nums, lastPos+1, nums.length-1, target);
}
lastPos = tmp;
console.log(firstPos, lastPos)
return (lastPos - firstPos + 1) > (nums.length/2) ? true : false;
};
var binarySearch = function(nums, left, right, target) {
while (left <= right) {
let mid = Math.floor((left+right)/2);
if (nums[mid] === target) return mid;
if (nums[mid] < target) {
left = mid+1;
} else {
right = mid-1;
}
}
return -1;
}