由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - find index of an element in sorted array
相关主题
贡献Google电话面试题问一个经典题
Amazon二面几个Java面试题 (转载)
lc新题,贴个刚写的solutionYelp面经
Google first Phone Interview为什么我写的binary search 比 linear还慢?
Binary search很靠基本功啊一道设计题
热腾腾的 LinkedIn 电面题攒RP听说binary search程序非常难写对,是吗?
发个a**D*n*m*c*的店面,已经废了请教一下怎么写unit test
Fresh PHD 毕业找工作求指导面试题count # of increasing subsequences of String求解
相关话题的讨论汇总
话题: mid话题: int话题: low话题: charset
进入JobHunting版参与讨论
1 (共1页)
a*******y
发帖数: 1040
1
sorted array with repeated elements
for given element, find out its range.
e.g. A A B B B B B C C D D E F G, given B, the output should be [2, 6]
binary search的变种
我感觉这个不是O(logn)啊
code是发现repeat次数的
int binarySearch(char* charset, int start, int end, char target)
{
if (charset == NULL) return 0;
if (start == end)
{
if (charset[start] == target) return 1;
else return 0;
}
int mid = start + (end-start)/2;

int left = 0, right = 0;
left = binarySearch(charset,start,mid-1,target);
right = binarySearch(charset, mid+1, end, target);
return (charset[mid] == target ? left+right+1 : left + right);

}
d****n
发帖数: 1637
2
size_t left, right, found;
size_t found=bsearch(array, start, end, target );
left=right=found;
while (array[left--]==target) left=found;
while (array[right++]==target) right=found;
return left, right
S**I
发帖数: 15689
3
http://www.cplusplus.com/reference/algorithm/equal_range/

【在 a*******y 的大作中提到】
: sorted array with repeated elements
: for given element, find out its range.
: e.g. A A B B B B B C C D D E F G, given B, the output should be [2, 6]
: binary search的变种
: 我感觉这个不是O(logn)啊
: code是发现repeat次数的
: int binarySearch(char* charset, int start, int end, char target)
: {
: if (charset == NULL) return 0;
: if (start == end)

a*******y
发帖数: 1040
4
No , worst case is O(n) for this

【在 d****n 的大作中提到】
: size_t left, right, found;
: size_t found=bsearch(array, start, end, target );
: left=right=found;
: while (array[left--]==target) left=found;
: while (array[right++]==target) right=found;
: return left, right

d****n
发帖数: 1637
5
你也没说要求啊

【在 a*******y 的大作中提到】
: No , worst case is O(n) for this
a*******y
发帖数: 1040
6
要log(n)的
f*******n
发帖数: 12623
7
Functions to find the left and right bounds, both log(n):
// left bound, inclusive
int binarySearch_left(char* arr, int len, char value) {
int low = 0, high = len-1, mid;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= value)
high = mid - 1;
else
low = mid + 1;
}
return low;
}
// right bound, inclusive
int binarySearch_right(char* arr, int len, char value) {
int low = 0, high = len-1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
return low - 1;
}
f*******n
发帖数: 12623
8
那个code是O(n)

【在 a*******y 的大作中提到】
: sorted array with repeated elements
: for given element, find out its range.
: e.g. A A B B B B B C C D D E F G, given B, the output should be [2, 6]
: binary search的变种
: 我感觉这个不是O(logn)啊
: code是发现repeat次数的
: int binarySearch(char* charset, int start, int end, char target)
: {
: if (charset == NULL) return 0;
: if (start == end)

S**I
发帖数: 15689
9
Read the source code of equal_range in a C++ implementation. In both VC and
GCC, implementation of equal_range is based on lower_bound and upper_bound,
both of which have log(n) complexity if the range to be searched is a sorted
array. So equal_range has complexity 2log(n).

【在 a*******y 的大作中提到】
: 要log(n)的
l****c
发帖数: 782
10

you lost one more step in each function. Think about "ABBC". Your approach
will find 0 as the left edge.
Try to add this before return in:
if (array[low] else return low;

【在 f*******n 的大作中提到】
: Functions to find the left and right bounds, both log(n):
: // left bound, inclusive
: int binarySearch_left(char* arr, int len, char value) {
: int low = 0, high = len-1, mid;
: while (low < high) {
: mid = (low + high) / 2;
: if (arr[mid] >= value)
: high = mid - 1;
: else
: low = mid + 1;

a*******y
发帖数: 1040
11
我觉得他应该把第一个那个“<=”改成< 就行了

【在 l****c 的大作中提到】
:
: you lost one more step in each function. Think about "ABBC". Your approach
: will find 0 as the left edge.
: Try to add this before return in:
: if (array[low]: else return low;

a*******y
发帖数: 1040
12
吧这个 if (arr[mid] >= value) 中的大于等于改成《
第二个function return hi
我来贴两个吧,test过了
int binarySearchLower(int A[], int l, int h, int k) {
int lo = l;
int hi = h;
while (lo <= hi) {
int mid = lo + (hi-lo)/2;
if (A[mid] < k)
lo = mid+1;
else
hi = mid-1;
}

return lo;
}
int binarySearchUpper(int A[], int low, int high, int k)
{
int lo = low;
int hi = high;
while (lo <=hi)
{
int mid = lo + (hi-lo)/2;
if (A[mid] > k)
hi = mid - 1;
else
lo = mid + 1;
}
return hi;
}

【在 f*******n 的大作中提到】
: Functions to find the left and right bounds, both log(n):
: // left bound, inclusive
: int binarySearch_left(char* arr, int len, char value) {
: int low = 0, high = len-1, mid;
: while (low < high) {
: mid = (low + high) / 2;
: if (arr[mid] >= value)
: high = mid - 1;
: else
: low = mid + 1;

f*******n
发帖数: 12623
13
Copying mistake
// left bound, inclusive
int binarySearch_left(char* arr, int len, char value) {
int low = 0, high = len-1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] >= value)
high = mid - 1;
else
low = mid + 1;
}
return low;
}
// right bound, inclusive
int binarySearch_right(char* arr, int len, char value) {
int low = 0, high = len-1, mid;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
return low - 1;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
面试题count # of increasing subsequences of String求解Binary search很靠基本功啊
面试题:两个有序数组中的最小差值热腾腾的 LinkedIn 电面题攒RP
coding是不是用接口比较吊啊?发个a**D*n*m*c*的店面,已经废了
求助一道FB的高频题non-overlap jobsFresh PHD 毕业找工作求指导
贡献Google电话面试题问一个经典题
Amazon二面几个Java面试题 (转载)
lc新题,贴个刚写的solutionYelp面经
Google first Phone Interview为什么我写的binary search 比 linear还慢?
相关话题的讨论汇总
话题: mid话题: int话题: low话题: charset